text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
long double a, b, c, x1, x2, d;
cin >> a >> b >> c;
if (a == 0) {
if (b != 0) {
x1 = -c / b;
cout << 1 << endl;
cout.precision(10);
cout << fixed << x1 << endl;
} else if (c == 0)
cout << -1 << endl;
else
cout << 0;
} else {
d = b * b - 4 * a * c;
if (d < 0)
cout << 0 << endl;
else if (d == 0) {
x1 = -b / (2 * a);
cout << 1 << endl;
cout.precision(10);
cout << fixed << x1 << endl;
} else {
x1 = (-b + sqrt(d)) / (2 * a);
x2 = (-b - sqrt(d)) / (2 * a);
cout << 2 << endl;
cout.precision(10);
cout << fixed << min(x1, x2) << endl;
cout << fixed << max(x1, x2) << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
if (a == 0 && b == 0 && c == 0)
printf("-1\n");
else if (a == 0 && b == 0)
printf("0\n");
else if (a == 0) {
printf("1\n");
printf("%.6lf\n", -1.0 * c / b);
} else if (b * b > 4 * a * c) {
printf("2\n");
double x1 = 1.0 * (-b + sqrt(1.0 * (b * b - 4 * a * c))) / (2 * a);
double x2 = 1.0 * (-b - sqrt(1.0 * (b * b - 4 * a * c))) / (2 * a);
printf("%.6lf\n%.6lf", min(x1, x2), max(x1, x2));
} else if (b * b == 4 * a * c) {
printf("1\n");
double x1 = 1.0 * (-b) / (2 * a);
printf("%.6lf", x1);
} else
printf("0\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c;
scanf("%I64d%I64d%I64d", &a, &b, &c);
if (a == 0) {
if (b == 0)
printf("%d", (c == 0 ? -1 : 0));
else
printf("1\n%.10f", -1.0 * c / b);
} else {
double res[2];
long long det = b * b - 4 * a * c;
if (det < 0)
printf("0");
else if (det == 0)
printf("1\n%.10f", -0.5 * b / a);
else {
res[0] = (-b + sqrt(det)) / (2 * a);
res[1] = (-b - sqrt(det)) / (2 * a);
sort(res, res + 2);
printf("2\n%.10f\n%.10f", res[0], res[1]);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int dx[9] = {0, 1, -1, 0, 0, -1, -1, 1, 1};
const int dy[9] = {0, 0, 0, -1, 1, -1, 1, -1, 1};
const double pi = acos(-1.0);
const int N = 1e6 + 100;
const int MOD = 1e9 + 7;
long long a, b, c;
double temp;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0) {
cout << -1 << endl;
return 0;
}
if (a == 0 && b == 0 && c != 0) {
cout << 0 << endl;
return 0;
}
if (a == 0 && b != 0) {
cout << 1 << endl;
double ans = (-1.0) * c / b;
cout << fixed << setprecision(10) << ans << endl;
return 0;
}
temp = b * b - 4 * a * c;
if (temp < 0) {
cout << 0 << endl;
} else if (temp == 0) {
cout << 1 << endl;
double ans = -1.0 * b / (2 * a);
cout << fixed << setprecision(10) << ans << endl;
} else if (temp > 0) {
cout << 2 << endl;
temp = sqrt(temp);
double ans1 = (-1.0 * b - temp) / (1.0 * 2 * a),
ans2 = (-1.0 * b + temp) / (1.0 * 2 * a);
if (ans1 > ans2) swap(ans1, ans2);
cout << fixed << setprecision(10) << ans1 << endl << ans2 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double a, b, c;
struct node {
double x1, x2;
int n;
} cx;
node solve() {
node ax;
if (a == 0 && b == 0 && c == 0) {
ax.n = -1;
return ax;
}
if (a == 0 && b == 0 && c != 0) {
ax.n = 0;
return ax;
}
if (a == 0 && b != 0 && c != 0) {
ax.n = 1;
ax.x1 = -c / b;
return ax;
}
if (a == 0 && b != 0 && c == 0) {
ax.n = 1;
ax.x1 = 0.0;
return ax;
}
double y = b * b - 4 * a * c;
if (y < 0) {
ax.n = 0;
return ax;
}
if (y == 0) {
ax.n = 1;
ax.x1 = -b / (2 * a);
return ax;
}
ax.n = 2;
ax.x1 = (-b + sqrt(y)) / (2 * a);
ax.x2 = (-b - sqrt(y)) / (2 * a);
return ax;
}
int main() {
while (scanf("%lf %lf %lf", &a, &b, &c) != EOF) {
cx = solve();
if (cx.n == 0 || cx.n == -1) {
printf("%d\n", cx.n);
}
if (cx.n == 1) {
printf("%d\n%.10lf\n", cx.n, cx.x1);
continue;
}
if (cx.n == 2) {
if (cx.x1 < cx.x2) swap(cx.x1, cx.x2);
printf("%d\n%.10lf\n%.10lf\n", cx.n, cx.x2, cx.x1);
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long double a, b, c;
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0) cout << -1;
if (a == 0 && b == 0 && c != 0) cout << 0;
if (a != 0 && b != 0) {
long long int d = b * b - 4 * a * c;
if (d < 0) cout << 0;
if (d > 0) {
cout << 2 << "\n";
int p = -b - sqrt(d);
int q = -b + sqrt(d);
if (a > 0) {
cout << fixed << setprecision(5) << (-b - sqrt(d)) / (2 * a) << "\n";
cout << fixed << setprecision(5) << (-b + sqrt(d)) / (2 * a);
} else {
cout << fixed << setprecision(5) << (-b + sqrt(d)) / (2 * a) << "\n";
cout << fixed << setprecision(5) << (-b - sqrt(d)) / (2 * a);
}
}
if (d == 0) {
cout << 1 << "\n";
cout << fixed << setprecision(5) << -b / (2 * a);
}
}
if (a == 0 && b != 0) {
cout << 1 << "\n";
cout << fixed << setprecision(5) << -c / b;
}
if (a != 0 && b == 0) {
if (c > 0) cout << 0;
if (c == 0)
cout << 1 << "\n"
<< "0.000000000";
if (c < 0) {
cout << 2 << "\n";
cout << fixed << setprecision(5) << -sqrt(c / a) << "\n" << sqrt(c / a);
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
a = double(a);
b = double(b);
c = double(c);
double d = pow(b, 2.0) - 4.0 * a * c;
if (d < 0 || a == 0 && b == 0 && c != 0) {
cout << 0 << "\n";
return 0;
} else if (a == 0 && b == 0 && c == 0) {
cout << -1 << "\n";
return 0;
} else if (a == 0) {
cout << 1 << "\n";
cout << fixed << double(-c / b);
} else if (d == 0) {
cout << 1 << "\n";
cout << fixed << double((-b + double(sqrt(d))) / (2.0 * a));
} else if (d > 0) {
cout << 2 << '\n';
cout << fixed
<< min(double((-b - double(sqrt(d))) / (2.0 * a)),
double((-b + double(sqrt(d))) / (2.0 * a)))
<< "\n";
cout << fixed
<< max(double((-b - double(sqrt(d))) / (2.0 * a)),
double((-b + double(sqrt(d))) / (2.0 * a)))
<< "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long iA, iB, iC, iDelta;
double iX1, iX2;
cin >> iA >> iB >> iC;
if (iA == 0) {
if (iB == 0 && iC == 0) {
cout << -1;
goto endapp;
} else if (iB == 0 && iC != 0) {
cout << 0;
goto endapp;
} else {
iX1 = double(-iC) / double(iB);
cout.setf(ios::fixed);
cout << 1 << setprecision(10) << endl << iX1;
goto endapp;
}
}
iDelta = iB * iB - 4 * iA * iC;
if (iDelta < 0) {
cout << 0;
goto endapp;
}
iX1 = (double(-iB) - sqrt(iDelta)) / (2 * double(iA));
iX2 = (double(-iB) + sqrt(iDelta)) / (2 * double(iA));
if (iX1 > iX2) {
swap(iX1, iX2);
}
if (iDelta != 0) {
cout.setf(ios::fixed);
cout << 2 << '\n' << setprecision(10) << iX1 << '\n' << iX2;
} else {
cout.setf(ios::fixed);
cout << 1 << '\n' << setprecision(10) << iX1;
}
endapp:
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
signed long long tonum(string s) {
stringstream in(s);
signed long long x;
in >> x;
return x;
}
string tostr(signed long long n) {
stringstream in;
in << n;
string x;
in >> x;
return x;
}
signed long long gcd(signed long long a, signed long long b) {
while (1) {
a = a % b;
if (a == 0) return b;
b = b % a;
if (b == 0) return a;
}
}
int main() {
signed long long A, B, C;
cin >> A >> B >> C;
if (A == 0) {
if (B == 0) {
if (C == 0)
printf("-1\n");
else
printf("0\n");
} else {
printf("1\n");
printf("%.9lf\n", double(0 - C) / double(B));
}
} else if (B == 0) {
if (C * A > 0)
printf("0\n");
else if (C == 0) {
printf("1\n");
printf("%.9lf\n", 0.0);
} else {
printf("2\n");
printf("%.9lf\n", sqrt((0.0 - double(C)) / double(A)) * -1);
printf("%.9lf\n", sqrt((0.0 - double(C)) / double(A)));
}
} else {
signed long long D = B * B - 4 * A * C;
if (D < 0)
printf("0\n");
else if (D == 0) {
printf("1\n");
printf("%.9lf\n", double(0.0 - double(B)) / (2.0 * double(A)));
} else {
printf("2\n");
if (A >= 0) {
printf("%.9lf\n",
(double(0.0 - double(B)) - sqrt(double(D))) / (2.0 * double(A)));
printf("%.9lf\n",
(double(0.0 - double(B)) + sqrt(double(D))) / (2.0 * double(A)));
} else {
printf("%.9lf\n",
(double(0.0 - double(B)) + sqrt(double(D))) / (2.0 * double(A)));
printf("%.9lf\n",
(double(0.0 - double(B)) - sqrt(double(D))) / (2.0 * double(A)));
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
double p, q;
while (cin >> a >> b >> c) {
if (a == 0 && b == 0 && c == 0)
cout << -1 << endl;
else if (a == 0 && b == 0 && c != 0)
cout << 0 << endl;
else if (a == 0) {
if (b != 0 && c == 0) {
cout << 1 << endl;
p = 0 * 1.0;
printf("%lf\n", p);
} else {
cout << 1 << endl;
p = 1.0 * (-c / b);
printf("%lf", p);
}
} else {
if (b * b - 4 * a * c == 0) {
cout << 1 << endl;
p = -b / (2 * a) * 1.0;
if (!p) p = 0;
printf("%lf\n", p);
} else if (b * b - 4 * a * c > 0) {
cout << 2 << endl;
p = (-b - sqrt(b * b - 4 * a * c)) / (2 * a) * 1.0;
q = (-b + sqrt(b * b - 4 * a * c)) / (2 * a) * 1.0;
if (p > q) swap(p, q);
printf("%lf\n%lf\n", p, q);
} else
cout << 0;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long A, B, C;
cin >> A >> B >> C;
if (!A) {
if (!B) {
if (!C)
cout << -1;
else
cout << 0;
return 0;
}
cout << 1 << '\n';
cout << fixed << setprecision(10) << (long double)-C / B;
return 0;
}
long long D = B * B - 4 * A * C;
if (D < 0) {
cout << 0;
return 0;
}
if (!D) {
cout << 1 << '\n';
cout << fixed << setprecision(10) << (long double)-B / (2 * A);
return 0;
}
cout << 2 << '\n';
if (A < 0) {
cout << fixed << setprecision(10) << (long double)(-B + sqrt(D)) / (2 * A)
<< '\n';
cout << fixed << setprecision(10) << (long double)(-B - sqrt(D)) / (2 * A);
} else {
cout << fixed << setprecision(10) << (long double)(-B - sqrt(D)) / (2 * A)
<< '\n';
cout << fixed << setprecision(10) << (long double)(-B + sqrt(D)) / (2 * A);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, d, x1, x2;
cin >> a >> b >> c;
d = (b * b) - 4 * a * c;
if (a == 0 && b == 0 && c == 0)
cout << "-1\n";
else if (a == 0 && b == 0)
cout << "0\n";
else if (d < 0)
cout << "0\n";
else if (a == 0) {
cout << "1\n";
printf("%0.7lf\n", -c / b);
} else if (d == 0) {
cout << "1\n";
printf("%0.7lf\n", -b / (2 * a));
} else {
d = sqrt(d);
x1 = (-b - d) / (2 * a);
x2 = (-b + d) / (2 * a);
cout << "2\n";
printf("%0.7lf\n%0.7lf\n", min(x1, x2), max(x1, x2));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, d, x1, x2;
cin >> a >> b >> c;
if ((a == 0) && (b == 0) && (c == 0)) {
cout << -1;
goto l;
}
if ((a == 0) && (b == 0)) {
cout << 0;
goto l;
}
if (a == 0) {
cout << "1\n";
printf("%.6f", -c / b);
goto l;
}
if (b * b - 4 * a * c < 0) {
cout << 0;
goto l;
}
d = sqrt(b * b - 4 * a * c);
x1 = (-b - d) / (2 * a);
x2 = (-b + d) / (2 * a);
if (d == 0) {
cout << "1\n";
printf("%.6f", x1);
goto l;
}
cout << "2\n";
printf("%.6f", min(x1, x2));
printf("\n%.6f", max(x2, x1));
l:;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
double d = (b * b) - (4 * a * c);
if (a == 0 && b == 0 && c == 0) {
cout << -1;
return 0;
}
if (a == 0 && b == 0) {
cout << 0;
return 0;
}
if (a == 0) {
cout << 1 << endl << fixed << setprecision(5) << (-1 * c / b);
return 0;
}
if (d < 0) {
cout << 0;
return 0;
}
if (d == 0) {
cout << 1 << endl << fixed << setprecision(5) << -1 * (b / (2 * a));
return 0;
}
double e = (-b - (sqrt(d))) / (2 * a), f = (-b + (sqrt(d))) / (2 * a);
cout << 2 << endl
<< fixed << setprecision(9) << min(e, f) << endl
<< fixed << setprecision(9) << max(e, f);
}
|
#include <bits/stdc++.h>
const double eps = 1e-6;
int main() {
double a, b, c;
while (std::cin >> a >> b >> c) {
if (fabs(a) < eps) {
if (fabs(b) < eps) {
if (fabs(c) > eps) {
puts("0");
} else {
puts("-1");
}
} else {
double ans = -c / b;
puts("1");
printf("%.6lf\n", ans);
}
} else {
double delta = b * b - 4 * a * c;
if (delta < 0) {
puts("0");
} else if (fabs(delta) == 0) {
double ans = -b / (2 * a);
puts("1");
printf("%.6lf\n", ans);
} else {
double ans1 = (-b - sqrt(delta)) / (2 * a);
double ans2 = (-b + sqrt(delta)) / (2 * a);
if (ans1 > ans2) {
std::swap(ans1, ans2);
}
puts("2");
printf("%.6lf %.6lf\n", ans1, ans2);
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a;
double b;
double c;
cin >> a;
cin >> b;
cin >> c;
double D = (b * b) - (4 * a * c);
double x = (sqrt(D) - b) / (2 * a);
double y = (-(sqrt(D)) - b) / (2 * a);
double z = ((-1) * c) / b;
if (a == 0 && b == 0 && c == 0) {
cout << "-1" << endl;
} else if (a == 0 && b != 0) {
cout << "1" << endl;
printf("%.10f\n", z);
} else if (D < 0 || (a == 0 && b == 0)) {
cout << "0" << endl;
} else if (D == 0) {
cout << "1" << endl;
printf("%.10f\n", x);
} else {
cout << "2" << endl;
if (x > y) {
printf("%.10f\n", y);
printf("%.10f\n", x);
} else {
printf("%.10f\n", x);
printf("%.10f\n", y);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << fixed << setprecision(9);
long long a, b, c;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
cout << (c == 0 ? -1 : 0) << '\n';
return 0;
}
cout << "1\n";
cout << (double)(-c) / b << '\n';
return 0;
}
long long d = b * b - 4LL * a * c;
if (d < 0) {
cout << "0\n";
return 0;
}
if (d == 0) {
cout << "1\n";
cout << (double)(-b) / (2.0 * a) << '\n';
return 0;
}
cout << "2\n";
vector<double> sols;
sols.push_back((double)(-b - sqrtl(d)) / (2.0 * a));
sols.push_back((double)(-b + sqrtl(d)) / (2.0 * a));
sort(sols.begin(), sols.end());
cout << sols[0] << '\n' << sols[1] << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
double d = pow(b, 2.0) - 4.0 * a * c;
if (d < 0 || a == 0 && b == 0 && c != 0) {
cout << 0 << "\n";
return 0;
} else if (a == 0 && b == 0 && c == 0) {
cout << -1 << "\n";
return 0;
} else if (a == 0) {
cout << 1 << "\n";
cout << fixed << -c / b;
} else if (d == 0) {
cout << 1 << "\n";
cout << fixed << double((-b + double(sqrt(d))) / (2.0 * a));
} else if (d > 0) {
cout << 2 << '\n';
cout << fixed
<< min(double((-b - double(sqrt(d))) / (2.0 * a)),
double((-b + double(sqrt(d))) / (2.0 * a)))
<< "\n";
cout << fixed
<< max(double((-b - double(sqrt(d))) / (2.0 * a)),
double((-b + double(sqrt(d))) / (2.0 * a)))
<< "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, d, r1, r2;
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0)
cout << "-1";
else if (a == 0 && b == 0)
cout << "0";
else if (a == 0) {
cout << "1" << endl;
r1 = -c / b;
printf("%5f", r1);
} else {
d = b * b - 4 * a * c;
if (d < 0)
cout << "0";
else if (d == 0) {
cout << "1" << endl;
r1 = -b / (2 * a);
printf("%5f", r1);
} else {
r1 = (-b + sqrt(d)) / (2 * a);
r2 = (-b - sqrt(d)) / (2 * a);
cout << "2" << endl;
if (r1 < r2)
printf("%5f %5f", r1, r2);
else
printf("%5f %5f", r2, r1);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double min(double a, double b) { return a < b ? a : b; }
double max(double a, double b) { return a > b ? a : b; }
int main() {
double a, b, c;
scanf("%lf%lf%lf", &a, &b, &c);
double t = b * b - 4 * a * c;
if (t < -1e-9) {
puts("0");
return 0;
} else if (t < 1e-9) {
if (a == 0 && b == 0 && c == 0) {
printf("-1\n");
return 0;
} else if (a == 0 && b == 0 && (c != 0)) {
puts("0");
return 0;
} else {
puts("1");
printf("%.10lf\n", -b / (2 * a));
return 0;
}
}
double s = sqrt(b * b - 4 * a * c);
if (a == 0) {
puts("1");
printf("%.10lf\n", -c / b);
return 0;
}
puts("2");
double aa = (-b - s) / (2 * a);
double bb = (-b + s) / (2 * a);
printf("%.10lf\n%.10lf\n", min(aa, bb), (max(aa, bb)));
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n, line;
double A, B, C, x1, x2, mid;
cin >> A >> B >> C;
mid = (B * B) - (4 * A * C);
if (A == 0 && B == 0 && C == 0) {
printf("-1\n");
} else if (A == 0 && B == 0)
printf("0\n");
else if (mid < 0.0)
printf("0\n");
else if (A == 0) {
x1 = -C / B;
printf("1\n%lf\n", x1);
} else {
mid = sqrt(mid);
x1 = -B + mid;
x1 = (x1 / (2.0 * A));
x2 = -B - mid;
x2 = (x2 / (2.0 * A));
if (x1 == x2)
printf("1\n%lf\n", x1);
else if (x1 < x2)
printf("2\n%lf\n%lf\n", x1, x2);
else
printf("2\n%lf\n%lf\n", x2, x1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long a, b, c, d;
long double k, x1, x2;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0)
c == 0 ? cout << -1 : cout << 0;
else
cout << "1\n" << fixed << setprecision(10) << (double)(-c) / b;
} else {
d = b * b - 4 * a * c;
if (d > 0) {
k = sqrt(d);
x1 = (double)(-b - k) / (2 * a);
x2 = (double)(k - b) / (2 * a);
cout << "2\n"
<< fixed << setprecision(10) << min(x1, x2) << "\n"
<< max(x1, x2);
} else
d < 0 ? cout << 0
: cout << "1\n"
<< fixed << setprecision(10) << (double)(-b) / (2 * a);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, x, y, r1, r2, m, n;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
if (c == 0)
printf("-1");
else
printf("0");
} else {
x = -1 * (c / b);
printf("1\n%.5lf", x);
}
} else {
y = b * b - 4 * a * c;
if (y < 0) printf("0"), exit(0);
r1 = (-b + sqrt(y)) / 2 / a, r2 = (-b - sqrt(y)) / 2 / a;
if (r1 == r2) {
printf("1\n%.5lf", r1);
} else {
m = min(r1, r2);
n = max(r1, r2);
printf("2\n%.5lf\n%.5lf", m, n);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long a, b, c;
cin >> a >> b >> c;
if (!a && !b && !c) {
cout << -1;
return 0;
}
if (!a && !b && c) {
cout << 0;
return 0;
}
if (!a) {
cout << 1 << '\n';
cout << setprecision(12) << ((double)(-c) / b);
return 0;
}
if (b * b - 4 * a * c < 0) {
cout << 0;
return 0;
}
if (b * b == 4 * a * c) {
cout << 1 << '\n';
cout << setprecision(12) << (double)(-b) / (2 * a);
return 0;
}
cout << 2 << '\n';
double r1 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a),
r2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
if (r1 > r2) swap(r1, r2);
cout << setprecision(12) << r1 << '\n';
cout << setprecision(12) << r2 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c, numberOfRoots, temp;
double r1, r2;
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0) {
cout << -1 << endl;
return 0;
} else if (a == 0 && b == 0) {
cout << 0 << endl;
return 0;
} else if (a == 0) {
r1 = double(-c) / b;
cout << 1 << endl << fixed << setprecision(5) << r1 << endl;
return 0;
}
temp = b * b - 4 * a * c;
if (temp == 0)
numberOfRoots = 1;
else if (temp > 0)
numberOfRoots = 2;
else if (temp < 0) {
cout << 0 << endl;
return 0;
}
r1 = (-b + sqrt(temp)) / (2 * a);
r2 = (-b - sqrt(temp)) / (2 * a);
if (r1 > r2)
cout << numberOfRoots << endl
<< fixed << setprecision(5) << r2 << endl
<< fixed << setprecision(5) << r1 << endl;
else if (r1 < r2)
cout << numberOfRoots << endl
<< fixed << setprecision(5) << r1 << endl
<< fixed << setprecision(5) << r2 << endl;
else
cout << numberOfRoots << endl << fixed << setprecision(5) << r2 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
double d = b * b - 4 * a * c;
if (a == 0) {
if (b == 0) {
if (c == 0)
cout << -1 << endl;
else
cout << 0 << endl;
} else
cout << 1 << endl << fixed << setprecision(10) << (double)-c / b << endl;
} else {
if (d == 0) {
cout << 1 << endl;
cout << fixed << setprecision(10) << (double)(-b / (2 * a)) << endl;
} else {
if (d < 0)
cout << 0 << endl;
else {
cout << 2 << endl;
d = sqrt(d);
double x = (-b - d) / (2 * a);
double xx[2];
xx[0] = x;
xx[1] = (double)(-b + d) / (2 * a);
sort(xx, xx + 2);
cout << fixed << setprecision(10) << xx[0] << endl;
cout << fixed << setprecision(10) << xx[1] << endl;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c, d;
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0) {
cout << "-1" << endl;
} else {
if (a == 0) {
if (b != 0) {
cout << "1" << endl;
cout << fixed << setprecision(5) << (0 - c) / (1.0 * b) << endl;
} else
cout << "0" << endl;
} else {
d = b * b - 4 * a * c;
if (d > 0) {
double x1, x2;
cout << "2" << endl;
x1 = (0 - b + sqrt(d)) / (2.0 * a);
x2 = (0 - b - sqrt(d)) / (2.0 * a);
if (x1 > x2) swap(x1, x2);
cout << fixed << setprecision(5) << x1 << endl;
cout << fixed << setprecision(5) << x2 << endl;
} else if (d == 0) {
cout << "1" << endl;
cout << fixed << setprecision(5) << (0 - b) / (2.0 * a) << endl;
} else
cout << "0" << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, d, e, f, g;
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0) {
cout << "-1";
return 0;
}
if (a == 0 && b == 0) {
cout << "0" << endl;
return 0;
}
if (a == 0) {
cout << "1" << endl;
cout << std ::fixed;
cout << setprecision(7) << (-c) / b << endl;
return 0;
}
d = (b * b) - (4 * a * c);
e = (-b + sqrt(d)) / (2 * a);
f = (-b - sqrt(d)) / (2 * a);
g = (-b) / (2 * a);
if (d == 0) {
cout << "1" << endl;
cout << std ::fixed;
cout << setprecision(7) << g << endl;
}
if (d < 0) {
cout << 0 << endl;
}
if (d > 0) {
cout << "2" << endl;
if (e > f) {
cout << std ::fixed;
cout << setprecision(7) << f << endl;
cout << std ::fixed;
cout << setprecision(7) << e << endl;
} else if (e < f) {
cout << std ::fixed;
cout << setprecision(7) << e << endl;
cout << std ::fixed;
cout << setprecision(7) << f << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void roots(double a, double b, double c) {
double x1, x2;
double p = b * b - 4 * a * c;
if (a == 0 && b == 0 && c == 0)
cout << -1 << endl;
else if (p < 0 || a == 0 && b == 0)
cout << 0 << endl;
else if (a == 0) {
cout << 1 << endl;
cout << fixed << setprecision(6) << -1 * (c / b) << endl;
} else {
x1 = (-b + sqrt(p)) / (2 * a);
x2 = (-b - sqrt(p)) / (2 * a);
if (x1 > x2) {
cout << 2 << endl;
cout << fixed << setprecision(6) << x2 << endl << x1 << endl;
} else if (x2 > x1) {
cout << 2 << endl;
cout << fixed << setprecision(6) << x1 << endl << x2 << endl;
} else {
cout << 1 << endl;
cout << fixed << setprecision(6) << x1 << endl;
}
}
}
int main() {
double a, b, c;
cin >> a >> b >> c;
roots(a, b, c);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
while (cin >> a >> b >> c) {
if (a != 0) {
if (b * b - 4 * a * c < 0)
cout << 0 << endl;
else if (b * b - 4 * a * c == 0) {
printf("%d\n", 1);
printf("%.10lf\n", -b / (2 * a));
} else if (b * b - 4 * a * c > 0) {
cout << 2 << endl;
if ((-b + sqrt(b * b - 4 * a * c)) / 2 * a <
(-b - sqrt(b * b - 4 * a * c)) / (2 * a)) {
printf("%.10lf\n", (-b + sqrt(b * b - 4 * a * c)) / (2 * a));
printf("%.10lf\n", (-b - sqrt(b * b - 4 * a * c)) / (2 * a));
} else {
printf("%.10lf\n", (-b - sqrt(b * b - 4 * a * c)) / (2 * a));
printf("%.10lf\n", (-b + sqrt(b * b - 4 * a * c)) / (2 * a));
}
}
} else if (b != 0) {
cout << 1 << endl;
printf("%.10lf\n", -c / b);
} else if (c != 0) {
cout << 0 << endl;
} else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c;
cin >> a >> b >> c;
double ad = a;
double bd = b;
double cd = c;
if (a == 0 && b == 0 && c == 0) {
cout << -1 << endl;
return 0;
}
if (a == 0) {
if (b == 0) {
cout << 0 << endl;
return 0;
}
printf("1\n%.9lf\n", -cd / bd);
} else {
long long D = b * b - 4LL * a * c;
if (D < 0) {
cout << 0 << endl;
return 0;
}
if (D == 0) {
cout << 1 << endl;
printf("%.9lf\n", -bd / (2.0 * ad));
return 0;
}
vector<double> v;
v.push_back((-bd + sqrt((double)D)) / (2.0 * ad));
v.push_back((-bd - sqrt((double)D)) / (2.0 * ad));
sort(v.begin(), v.end());
cout << 2 << endl;
for (int i = 0; i < 2; i++) printf("%.9lf\n", v[i]);
}
}
|
#include <bits/stdc++.h>
using namespace std;
double n, m, k, a, b, c;
string s;
vector<double> v;
int main() {
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0 && b == 0 && c == 0) {
printf("-1\n");
return 0;
}
if (a == 0 && b == 0 && c != 0) {
printf("0\n");
return 0;
}
if (b * b - 4 * a * c < 0) {
printf("0\n");
return 0;
}
if (a == 0) {
printf("1\n");
double cev = -c / b;
printf("%lf", cev);
return 0;
}
if (b * b - 4 * a * c == 0) {
printf("1\n");
double cev = -b / (2 * a);
printf("%lf", cev);
return 0;
}
printf("2\n");
double cev = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
double cevap = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
v.push_back(cev);
v.push_back(cevap);
sort(v.begin(), v.end());
printf("%lf\n", v[0]);
printf("%lf\n", v[1]);
return 0;
}
|
#include <bits/stdc++.h>
int a, b, c;
void solver() {
if (a == 0 && b != 0) {
printf("1\n%.5f\n", (-c / (float)b));
return;
}
if (a == 0 && b == 0 && c == 0) {
printf("-1\n");
return;
}
if ((a == 0 && b == 0 && c != 0) || (a == b && b == c) ||
(b == 0 && a * c > 0)) {
printf("0\n");
return;
}
if (a * b * 4 == b * b && c != 0) {
printf("1\n%.5f\n", (-b / (float)(2 * a)));
return;
}
double discriminante = sqrt((double)b * b - (double)4 * a * c);
double v1 = (-b - discriminante) / (double)(2 * a);
double v2 = (-b + discriminante) / (double)(2 * a);
if (v1 < v2)
printf("2\n%.5lf\n%.5lf\n", v1, v2);
else {
if (v1 != v2)
printf("2\n%.5lf\n%.5lf\n", v2, v1);
else
printf("1\n%.5lf\n", v1);
}
}
int main() {
while (scanf("%d %d %d", &a, &b, &c) != EOF) solver();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX1 = 0x7f7f7f7f;
const int MAX = 1e5 + 10;
double exgcd(long long l, long long r, double &x, double &y) {
if (r == 0) {
x = 1;
y = 0;
return l;
} else {
double d = exgcd(r, l % r, y, x);
y -= l / r * x;
return d;
}
}
int main() {
int n, m;
int i, j, k;
int ans;
double a, b, c, res1, res2;
double num[20];
memset(num, 0, sizeof(num));
scanf("%lf%lf%lf", &a, &b, &c);
double flag = b * b - 4 * (a * c);
if (a == 0 && c == 0 && b == 0)
printf("-1\n");
else if (a == 0 && b != 0) {
ans = 1;
res1 = (-c) * 1.0 / b;
printf("%d\n%.10lf\n", ans, res1);
} else if (a == 0 && b == 0 && c != 0)
printf("0\n");
else {
if (b * b - 4 * (a * c) < 0) {
printf("0\n");
} else if (b * b - 4 * (a * c) == 0) {
ans = 1;
res1 = -b / (2 * a);
printf("%d\n%.10lf\n", ans, res1);
} else {
ans = 2;
res1 = (-b - sqrt(flag * 1.0)) / (2 * a);
res2 = (-b + sqrt(flag * 1.0)) / (2 * a);
if (res1 > res2) {
flag = res1;
res1 = res2;
res2 = flag;
}
printf("%d\n%.10lf\n%.10lf\n", ans, res1, res2);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0)
cout << -1;
else if (a == 0 && b == 0 && c != 0)
cout << 0;
else if (b * b - 4 * a * c < 0)
cout << 0;
else if (a == 0) {
cout << 1 << endl;
double x = -(c / b);
printf("%.10f", x);
} else {
double x = ((-b) + sqrt(b * b - 4 * a * c)) / (2 * a);
double y = ((-b) - sqrt(b * b - 4 * a * c)) / (2 * a);
if (x == y) {
cout << 1 << endl;
printf("%.10f", x);
} else {
cout << 2 << endl;
if (x > y) swap(x, y);
printf("%.10f", x);
cout << endl;
printf("%.10f", y);
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int const uu[4] = {1, -1, 0, 0};
int const vv[4] = {0, 0, 1, -1};
int const inf = 0x3f3f3f3f;
long long const INF = 0x7fffffffffffffffll;
double eps = 1e-10;
double pi = acos(-1.0);
double a, b, c;
int main() {
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
if (c == 0) {
puts("-1");
return 0;
} else {
puts("0");
return 0;
}
} else {
printf("1\n%.10lf\n", (-c) / b);
return 0;
}
}
double delta = b * b - 4 * a * c;
if (delta < 0.0) {
puts("0");
return 0;
}
delta = sqrt(delta);
double x1 = (-b - delta) / (2 * a);
double x2 = (-b + delta) / (2 * a);
if (fabs(x1 - x2) < eps) {
puts("1");
printf("%.10lf\n", x1);
return 0;
}
if (x1 > x2) swap(x1, x2);
puts("2");
printf("%.10lf\n", x1);
printf("%.10lf\n", x2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
if (c == 0)
cout << -1 << endl;
else
cout << 0 << endl;
} else {
cout << 1 << endl;
printf("%.9lf\n", -c / b);
}
} else {
if (b * b - 4 * a * c < 0) {
cout << 0 << endl;
return 0;
}
double root1, root2;
root1 = (-b + pow(b * b - 4 * a * c, 0.5)) / (2 * a);
root2 = (-b - pow(b * b - 4 * a * c, 0.5)) / (2 * a);
if (root1 == root2) {
cout << 1 << endl;
printf("%.9lf\n", root1);
} else {
cout << 2 << endl;
if (root1 > root2) swap(root1, root2);
printf("%.9lf\n", root1);
printf("%.9lf\n", root2);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
if (a == 0 && b == 0 && c == 0)
printf("%d", -1);
else if (a == 0 && b == 0 && c != 0)
printf("%d", 0);
else if (a == 0 && b != 0)
printf("1\n%.10lf", -double(c) / b);
else {
if (b * b - 4 * a * c < 0)
printf("0");
else if (b * b - 4 * a * c == 0) {
printf("1\n");
double x = double(-b) / 2 / a;
printf("%.10lf", x);
} else {
printf("2\n");
double x1 = double(-b + sqrt(b * b - 4 * a * c)) / 2 / a;
double x2 = double(-b - sqrt(b * b - 4 * a * c)) / 2 / a;
double t;
if (x1 > x2) t = x1, x1 = x2, x2 = t;
printf("%.10lf\n%.10lf", x1, x2);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c;
float dir, x1, x2;
int count;
while (cin >> a >> b >> c) {
if (a == 0 && b == 0 && c == 0)
cout << "-1" << endl;
else if (a == 0) {
if (b == 0)
cout << "0" << endl;
else {
cout << "1" << endl;
printf("%.5f\n", -(float)c / (float)b);
}
} else {
dir = b * b - 4 * a * c;
if (dir < 0)
cout << "0" << endl;
else {
if (dir > 0) count = 2;
if (dir == 0) count = 1;
dir = sqrt(dir);
x1 = (-b + dir) / (2 * a);
x2 = (-b - dir) / (2 * a);
if (x1 > x2) swap(x1, x2);
if (count == 1) printf("%d\n%.5f\n", count, x1);
if (count == 2) printf("%d\n%.5f\n%.5f\n", count, x1, x2);
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
if (c == 0)
cout << -1;
else
cout << 0;
} else
cout << fixed << setprecision(5) << "1\n" << -c / b;
} else {
double denta = b * b - 4 * a * c;
if (denta < 0)
cout << 0;
else if (denta == 0)
cout << "1\n" << fixed << setprecision(5) << -b / 2 / a;
else {
double x1 = (-b - sqrt(denta)) / 2 / a, x2 = (-b + sqrt(denta)) / 2 / a;
if (x1 > x2) {
double temp = x1;
x1 = x2;
x2 = temp;
}
cout << "2\n"
<< fixed << setprecision(5) << x1 << "\n"
<< fixed << setprecision(5) << x2;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
double a, b, c;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0 && c == 0) {
cout << "-1" << endl;
return 0;
}
if (b == 0 && c != 0) {
cout << "0" << endl;
return 0;
}
cout << "1" << endl;
cout << fixed << setprecision(10) << -c / b << endl;
} else {
if (b * b - 4 * a * c < 0) {
cout << "0" << endl;
return 0;
}
if (b * b - 4 * a * c == 0) {
cout << "1" << endl;
cout << fixed << setprecision(10) << -b / 2 / a << endl;
}
if (b * b - 4 * a * c > 0) {
cout << "2" << endl;
cout << fixed << setprecision(10);
cout << min((-b - sqrt(b * b - 4 * a * c)) / 2 / a,
(-b + sqrt(b * b - 4 * a * c)) / 2 / a)
<< endl;
cout << max((-b - sqrt(b * b - 4 * a * c)) / 2 / a,
(-b + sqrt(b * b - 4 * a * c)) / 2 / a)
<< endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
double d = b * b - 4 * a * c;
if (a == 0.0) {
if (b == 0.0) {
if (c == 0.0)
cout << "-1" << endl;
else
cout << "0" << endl;
return 0;
}
cout << "1" << endl;
cout << fixed << setprecision(20) << -c / b << endl;
return 0;
}
if (d < 0.0) return cout << "0" << endl, 0;
if (d == 0.0) {
cout << "1" << endl;
cout << fixed << setprecision(20) << -1 * b / a / 2.0 << endl;
return 0;
}
double k = sqrt(d);
cout << "2" << endl;
if (a > 0.0) {
cout << fixed << setprecision(20) << (-b - k) / (2 * a) << endl;
cout << fixed << setprecision(20) << (-b + k) / (2 * a) << endl;
} else {
cout << fixed << setprecision(20) << (-b + k) / (2 * a) << endl;
cout << fixed << setprecision(20) << (-b - k) / (2 * a) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double a, b, c, a1, a2, mini;
int n, i, j;
int main() {
cin >> a >> b >> c;
if (!a && !b) {
if (!c)
cout << "-1";
else
cout << "0";
return 0;
}
if (!a) {
a1 = (-c) / b;
printf("1\n%lf", a1);
return 0;
}
if ((b * b < (4 * a * c))) {
cout << "0";
return 0;
}
a1 = (-b + sqrt(b * b - (4 * c * a))) / (2 * a);
a2 = (-b - sqrt(b * b - (4 * c * a))) / (2 * a);
if (a1 == a2)
printf("1\n%lf", a1);
else {
mini = a1 > a2 ? a2 : a1;
a1 = a1 + a2 - mini;
printf("2\n%lf\n%lf", mini, a1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double a, b, c;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
scanf("%lf%lf%lf", &a, &b, &c);
if (a == 0) {
if (b == 0 && c == 0)
cout << -1;
else if (b == 0)
cout << 0 << endl;
else
printf("1\n%.10lf", -c / b);
} else {
double d = b * b - 4 * a * c;
if (d < 0)
cout << 0;
else if (d > 0) {
double d1 = (-b + sqrt(d)) / (2 * a);
double d2 = (-b - sqrt(d)) / (2 * a);
cout << 2 << endl;
if (d1 > d2)
printf("%.10lf\n%.10lf", d2, d1);
else
printf("%.10lf\n%.10lf", d1, d2);
} else {
printf("1\n%.10lf", -b / (2 * a));
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
float a, b, c;
cin >> a >> b >> c;
float d = (b * b - 4 * a * c);
if (a == 0) {
if (b == 0) {
if (c == 0)
cout << -1 << endl;
else
cout << 0 << endl;
} else
cout << 1 << endl << fixed << setprecision(6) << -c / b << endl;
} else {
if (d == 0) {
cout << 1 << endl;
cout << fixed << setprecision(6) << -b / (2 * a) << endl;
} else {
if (d < 0)
cout << 0 << endl;
else {
d = sqrt(b * b - 4 * a * c);
cout << 2 << endl;
float x = (-b - d) / (2 * a);
float y = (-b + d) / (2 * a);
cout << fixed << setprecision(6) << min(x, y) << endl;
cout << fixed << setprecision(6) << max(x, y) << endl;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, d;
double x1, x2;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
if (c == 0)
cout << -1 << endl;
else
cout << 0 << endl;
} else
printf("1\n%f\n", -1 * c / b);
} else {
d = b * b - 4 * a * c;
if (d < 0)
cout << 0 << endl;
else {
x1 = (-1 * b - sqrt(d)) / (2.0 * a);
x2 = (-1 * b + sqrt(d)) / (2.0 * a);
if (x1 > x2) {
double temp = x1;
x1 = x2;
x2 = temp;
}
if (x1 != x2)
printf("2\n%f\n%f\n", x1, x2);
else
printf("1\n%f\n", x1);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long double x = 0, y = 0, l = 0;
long long int a = 0, b, c;
int main() {
cin >> a >> b >> c;
x = (b * b) - (4 * a * c);
if (a == 0 && b == 0 && c == 0) {
cout << -1;
return 0;
}
if (a == 0 && b == 0) {
cout << 0;
return 0;
}
cout.precision(6);
cout << fixed;
if (a == 0) {
x = -c;
x /= b;
cout << 1 << endl;
cout << x;
return 0;
}
if (x < 0) {
cout << 0;
return 0;
}
if (x == 0) {
cout << 1 << endl;
x = -b;
x /= 2 * a;
cout << x << endl;
return 0;
} else {
cout << 2 << endl;
y = -b + sqrt(x);
y /= (2 * a);
l = -b - sqrt(x);
l /= (2 * a);
if (y > l) swap(y, l);
cout << y << endl;
cout << l << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c;
cin >> a >> b >> c;
long long DIS = (b * b) - (4 * a * c);
if ((a == 0 and b == 0 and c == 0))
cout << -1 << endl;
else if (DIS < 0 or (a == 0 and b == 0))
cout << 0 << endl;
else if (a == 0)
printf("1\n%.10lf\n", (double)-c / b);
else {
double x = (double)(-b + (double)sqrt(DIS)) / (2 * a);
double y = (double)(-b - (double)sqrt(DIS)) / (2 * a);
if (y == 0 and x == 0)
printf("1\n%.10lf\n", 0.0);
else {
set<double> st;
st.insert(x);
st.insert(y);
cout << st.size() << endl;
for (set<double>::iterator it = st.begin(); it != st.end(); it++)
printf("%.10lf\n", *it);
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
double a, b, c;
int main() {
while (scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
double jd = b * b - 4 * a * c;
if (a == 0) {
if (b == 0)
printf("%d\n", c == 0 ? -1 : 0);
else
printf("1\n%.10lf\n", (double)-c / (double)b);
} else if (jd < 0)
printf("0\n");
else if (jd == 0)
printf("1\n%.10lf\n", (double)-b / (double)(2 * a));
else {
double sq = sqrt(jd);
double f1 = (-b - sq) / (2 * a);
double f2 = (-b + sq) / (2 * a);
if (f1 > f2)
printf("2\n%.10lf\n%.10lf\n", f2, f1);
else
printf("2\n%.10lf\n%.10lf\n", f1, f2);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
while (~scanf("%lf%lf%lf", &a, &b, &c)) {
if (a == 0 && b == 0)
if (c == 0)
puts("-1");
else
puts("0");
else if (a == 0 && c == 0)
puts("1\n0");
else if (b == 0 && c == 0)
puts("1\n0");
else if (a == 0)
printf("1\n%.10lf\n", -c / b);
else {
double tem = b * b - 4 * a * c;
if (tem < 0)
printf("0\n");
else if (tem == 0)
printf("1\n%.10lf\n", -b / (a * 2));
else {
double x1, x2;
x1 = (-b - sqrt(tem)) / (a * 2);
x2 = (-b + sqrt(tem)) / (a * 2);
if (x1 > x2) swap(x1, x2);
printf("2\n%.10lf\n%.10lf\n", x1, x2);
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool SR(int &x) { return scanf("%d", &x) == 1; }
bool SR(long long &x) { return scanf("%lld", &x) == 1; }
bool SR(double &x) { return scanf("%lf", &x) == 1; }
bool SR(char *s) { return scanf("%s", s) == 1; }
bool RI() { return true; }
template <typename I, typename... T>
bool RI(I &x, T &...tail) {
return SR(x) && RI(tail...);
}
void SP(const int x) { printf("%d", x); }
void SP(const long long x) { printf("%lld", x); }
void SP(const double x) { printf("%.16lf", x); }
void SP(const char *s) { printf("%s", s); }
void PL() { puts(""); }
template <typename I, typename... T>
void PL(const I x, const T... tail) {
SP(x);
if (sizeof...(tail)) putchar(' ');
PL(tail...);
}
int ar[3];
void read() {
for (int i = 0; i < int(3); i++) RI(ar[i]);
}
void build() {}
void sol0(int a) {
if (a == 0)
PL(-1);
else
PL(0);
}
void sol1(int a, int b) {
PL(1);
PL((0.0 - b) / a);
}
void sol2(long long a, long long b, long long c) {
long long d = b * b - 4 * a * c;
if (d < 0)
PL(0);
else if (d == 0) {
PL(1);
PL((0.0 - b) / (2 * a));
} else {
double p = sqrt(d);
vector<double> v;
v.push_back((-b + p) / (2 * a));
v.push_back((-b - p) / (2 * a));
sort(begin(v), end(v));
PL(2);
for (auto x : v) PL(x);
}
}
void sol() {
if (ar[0] == 0 && ar[1] == 0)
sol0(ar[2]);
else if (ar[0] == 0)
sol1(ar[1], ar[2]);
else
sol2(ar[0], ar[1], ar[2]);
}
int main() {
read();
build();
sol();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int INF32 = INT_MAX;
const ll INF64 = ((ll)INF32 << 29ll);
void reflex() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
}
signed main() {
reflex();
using ld = long double;
ld a, b, c;
cin >> a >> b >> c;
cout << setprecision(10) << fixed;
if (!a && !b && !c) cout << -1, exit(false);
if (!a) {
if (!b)
cout << 0;
else
cout << "1\n" << (ld)(-c / b);
return false;
}
ld d = (b * 1ll * b) - (4 * 1ll * a * c);
if (d < 0) cout << 0, exit(false);
d = (ld)sqrt(d);
ld x = (-b + d) / (a * 2ll);
ld y = (-b - d) / (a * 2ll);
if (d > 0) {
if (x > y) swap(x, y);
cout << "2\n" << x << "\n" << y;
} else
cout << "1\n" << x;
return false;
}
|
#include <bits/stdc++.h>
using namespace std;
double a, b, c;
int main() {
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0) {
cout << "-1" << endl;
return 0;
}
if (a == 0) {
if (b == 0)
cout << "0" << endl;
else {
cout << "1" << endl;
double x = -c / b;
cout.precision(6);
cout << fixed << x << endl;
}
} else {
double d = b * b - 4 * a * c;
if (d < 0)
cout << '0';
else if (d == 0) {
cout << '1' << endl;
double x11 = -b / (2 * a);
cout.precision(6);
cout << fixed << x11 << endl;
} else {
cout << '2' << endl;
double x11 = (-sqrt(d) - b) / (2 * a);
double x22 = (sqrt(d) - b) / (2 * a);
cout.precision(6);
cout << fixed << min(x11, x22) << endl;
cout.precision(6);
cout << fixed << max(x11, x22) << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, det, x1, x2;
while (scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
if (a == 0 && b == 0) {
if (c == 0)
puts("-1");
else
puts("0");
continue;
}
if (a == 0) {
x1 = -1.0 * c / b;
if (x1 == -0.0) x1 = 0.0;
printf("1\n%lf\n", x1);
continue;
}
det = b * b - 4.0 * a * c;
if (det < 0) {
puts("0");
continue;
}
if (det == 0) {
x1 = -0.5 * b / a;
printf("1\n%lf\n", x1);
continue;
}
if (det > 0) {
x1 = 0.5 * (-b + sqrt(det)) / a;
x2 = 0.5 * (-b - sqrt(det)) / a;
if (x1 > x2) swap(x1, x2);
printf("2\n%lf\n%lf\n", x1, x2);
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long int max(long long int a, long long int b, long long int c) {
return max(max(a, b), c);
}
inline long long int min(long long int a, long long int b, long long int c) {
return min(min(a, b), c);
}
inline long long int max(long long int a, long long int b) {
return (a > b) ? a : b;
}
inline long long int min(long long int a, long long int b) {
return (a < b) ? a : b;
}
inline long long int add(long long int x, long long int y, long long int mod_) {
return (x + y >= mod_) ? x + y - mod_ : x + y;
}
inline long long int mul(long long int x, long long int y, long long int mod_) {
return ((x % mod_) * 1LL * (y % mod_)) % mod_;
}
long long int power(long long int a, long long int n) {
long long int p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
}
n >>= 1;
a *= a;
}
return p;
}
long long int powm(long long int a, long long int n, long long int mod_) {
long long int p = 1;
while (n) {
if (n % 2) {
p = mul(p, a, mod_);
}
n >>= 1;
a = mul(a, a, mod_);
}
return p % mod_;
}
long long int powi(long long int a, long long int mod_) {
return powm(a, mod_ - 2, mod_);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
cout << setprecision(15);
long long int mn, mx;
long long int n, m, t, k, i, j, sum = 0, flag = 0, cnt = 0;
long long int x = 0, y = 0, z, l, r, q;
int TC = 1;
while (TC--) {
double a, b, c;
cin >> a >> b >> c;
if (a != 0) {
if (b * b - 4 * a * c < 0)
cout << 0;
else if (b * b - 4 * a * c == 0)
cout << 1 << '\n' << (-b) / (2.0 * a);
else {
cout << "2\n";
double D = sqrtl(b * b - 4 * a * c);
double x = (-b + D) / (2.0 * a);
double y = (-b - D) / (2.0 * a);
if (x < y) {
cout << x << '\n' << y;
} else
cout << y << '\n' << x;
}
} else if (b != 0) {
cout << 1 << '\n' << (-c / b);
} else {
if (c != 0)
cout << 0;
else
cout << -1;
}
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
double d = pow(b, 2.0) - 4.0 * a * c;
if (d < 0 || a == 0 && b == 0 && c != 0) {
cout << 0 << "\n";
return 0;
} else if (a == 0 && b == 0 && c == 0) {
cout << -1 << "\n";
return 0;
} else if (a == 0) {
cout << 1 << "\n";
cout << fixed << -c / b;
} else if (d == 0) {
cout << 1 << "\n";
cout << fixed << (-b + (sqrt(d))) / (2.0 * a);
} else if (d > 0) {
cout << 2 << '\n';
cout << fixed
<< min(((-b - sqrt(d)) / (2.0 * a)), ((-b + sqrt(d)) / (2.0 * a)))
<< "\n";
cout << fixed
<< max(((-b - sqrt(d)) / (2.0 * a)), ((-b + sqrt(d)) / (2.0 * a)))
<< "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
double a, b, c;
cin >> a >> b >> c;
if (a == 0 && b == 0) {
if (c == 0) {
cout << -1 << endl;
} else {
cout << 0 << endl;
}
return 0;
}
if (a == 0) {
cout << 1 << endl;
printf("%.6f\n", 0 - c / b);
return 0;
}
double d = b * b - 4 * a * c;
if (d < 0) {
cout << 0 << endl;
} else if (d == 0) {
cout << 1 << endl;
printf("%.6f\n", 0 - b / (2 * a));
} else {
cout << 2 << endl;
double res[2];
res[0] = (0 - b - sqrt(d)) / (2 * a);
res[1] = (0 - b + sqrt(d)) / (2 * a);
sort(res, res + 2);
printf("%.6f\n", res[0]);
printf("%.6f\n", res[1]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int TestMillerRabin[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
long long mulmod(long long a, long long b, long long p) {
long long x = 0, y = a % p;
while (b > 0) {
if (b % 2 == 1) x = (x + y) % p;
y = (1LL * y * 2) % p;
b = b / 2;
}
return x % p;
}
long long fastexp(long long x, long long y, long long p) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (1LL * ans * x) % p;
y = y >> 1;
x = (1LL * x * x) % p;
}
return ans % p;
}
long long fastexpp(long long x, long long y, long long p) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = mulmod(ans, x, p);
y = y >> 1;
x = mulmod(x, x, p);
}
return ans % p;
}
long long invmod(long long x, long long p) { return fastexp(x, p - 2, p) % p; }
bool check_composite(long long n, long long a, long long d, int s) {
long long x = fastexp(a, d, n);
if (x == 1 || x == n - 1) return false;
for (int r = 1; r < s; r++) {
x = (1LL * x * x) % n;
if (x == n - 1) return false;
}
return true;
}
bool isPrime(long long p) {
if (p <= 1) return false;
int r = 0;
long long d = p - 1;
while (!(d & 1)) {
d >>= 1;
r++;
}
for (int i = 0; i < 12; i++) {
int value = TestMillerRabin[i];
if (p == value) return true;
if (check_composite(p, value, d, r)) return false;
}
return true;
}
int main() {
int A, B, C;
cin >> A >> B >> C;
if (A == 0) {
if (B == 0) {
if (C == 0)
cout << "-1\n";
else
cout << "0\n";
} else {
cout << 1 << '\n';
double ans = -1 * (double)C / (double)B;
printf("%.5lf\n", ans);
}
} else {
long long det = 1LL * B * B - 4LL * A * C;
if (det > 0) {
cout << "2\n";
double ans1 = (-B + sqrt((double)det)) / (2 * (double)A);
double ans2 = (-B - sqrt((double)det)) / (2 * (double)A);
if (ans1 > ans2) {
double aux = ans1;
ans1 = ans2;
ans2 = aux;
}
printf("%.8lf\n%.8lf\n", ans1, ans2);
return 0;
}
if (det == 0) {
cout << "1\n";
double ans = -1 * (double)B / (2 * (double)A);
printf("%.8lf\n", ans);
} else {
cout << "0\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
if (c == 0) {
printf("-1");
} else {
printf("0");
}
} else {
printf("1\n%f", -c / b);
}
} else {
if (b * b - 4 * a * c < -1E-6) {
printf("0");
} else {
if (fabs(b * b - 4 * a * c) < 1E-6) {
printf("1\n%f", -b / (2 * a));
} else {
if (a > 0) {
printf("2\n%f\n%f", (-b - sqrt(b * b - 4 * a * c)) / (2 * a),
(-b + sqrt(b * b - 4 * a * c)) / (2 * a));
} else {
printf("2\n%f\n%f", (-b + sqrt(b * b - 4 * a * c)) / (2 * a),
(-b - sqrt(b * b - 4 * a * c)) / (2 * a));
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
double x[3];
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0) {
cout << "-1" << endl;
return 0;
}
if (a == 0 && b == 0) {
cout << "0" << endl;
return 0;
}
if (a == 0) {
cout << "1" << endl;
cout << fixed << setprecision(10) << double(-c / b) << endl;
return 0;
}
if (b * b - 4 * a * c < 0) {
cout << "0" << endl;
return 0;
}
x[0] = double(-b + sqrt(b * b - 4 * a * c)) / double(2 * a);
x[1] = double(-b - sqrt(b * b - 4 * a * c)) / double(2 * a);
if (x[0] == x[1]) {
cout << "1" << endl;
cout << fixed << setprecision(10) << x[0] << endl;
return 0;
} else {
cout << "2" << endl;
cout << fixed << setprecision(10) << min(x[0], x[1]) << endl;
cout << fixed << setprecision(10) << max(x[0], x[1]) << endl;
return 0;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, det, x1, x2;
cout.precision(15);
cin >> a >> b >> c;
det = b * b - 4 * a * c;
if (a == 0 && b == 0 && c == 0)
cout << -1;
else if (a == 0 && b == 0 || det < 0)
cout << 0;
else if (a == 0)
cout << 1 << endl << -c / b;
else {
if (det == 0)
cout << 1 << endl << fixed << -1 * b / (2 * a);
else {
x1 = (-b - sqrt(det)) / (2 * a);
x2 = (-b + sqrt(det)) / (2 * a);
if (x1 > x2) swap(x1, x2);
cout << 2 << endl << fixed << x1 << endl << fixed << x2;
}
}
}
|
#include <bits/stdc++.h>
int main() {
double a, b, c;
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
if (b == 0) {
puts(c ? "0" : "-1");
} else {
printf("1\n%.6lf\n", -c / b);
}
} else {
if (a < 0) {
a = -a;
b = -b;
c = -c;
}
double d = b * b - 4 * a * c;
if (d < 0) {
puts("0");
} else if (d == 0) {
printf("1\n%.6lf\n", -b / (2 * a));
} else if (d > 0) {
printf("2\n%.6lf\n%.6lf\n", (-b - sqrt(d)) / (2 * a),
(-b + sqrt(d)) / (2 * a));
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
double f;
if (a == 0 && b == 0) {
if (c == 0)
cout << "-1" << endl;
else
cout << "0" << endl;
} else if (a == 0 && c == 0) {
cout << "1" << endl;
cout << fixed << setprecision(8) << 0.0 << endl;
} else if (a == 0) {
cout << "1" << endl;
cout << fixed << setprecision(8) << -c / b << endl;
} else {
double delta = (b * b) - (4 * a * c);
double r = sqrt(delta);
if (delta < 0)
cout << "0" << endl;
else if (delta == 0) {
cout << "1" << endl;
cout << fixed << setprecision(8) << -b / (2 * a) << endl;
} else {
cout << "2" << endl;
double x = (double)(-b - r) / (2 * a), y = (-b + r) / (2 * a);
if (x > y) {
cout << fixed << setprecision(8) << y << endl;
cout << setprecision(8) << x << endl;
} else {
cout << fixed << setprecision(8) << x << endl;
cout << setprecision(8) << y << endl;
}
}
}
}
|
#include <bits/stdc++.h>
int main() {
long long a, b, c, d;
scanf("%lld %lld %lld", &a, &b, &c);
if (a == 0 && b == 0 && c == 0) {
printf("-1");
return 0;
}
if (a == 0 && b == 0) {
printf("0");
return 0;
}
d = b * b - 4 * a * c;
if (d < 0) {
printf("0");
return 0;
}
if (a == 0) {
printf("1\n%.7f", -1.0 * c / b);
return 0;
}
float a1 = (-1 * b + sqrt(d)) / (2.0 * a);
float a2 = (-1 * b - sqrt(d)) / (2.0 * a);
if (a1 == a2) {
printf("1\n%.7f", a1);
return 0;
}
if (a1 > a2) {
float temp = a2;
a2 = a1;
a1 = temp;
}
printf("2\n%.7f\n%.7f", a1, a2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
double a, b, c;
cin >> a >> b >> c;
if (a == 0 && b == 0 && c == 0) {
cout << -1;
return 0;
} else if (a == 0 && b == 0) {
cout << 0;
return 0;
}
if (a == 0) {
cout << 1 << endl;
cout << fixed << setprecision(10) << -c / b << endl;
return 0;
}
double x = b * b - 4 * a * c;
if (x < 0) {
cout << 0;
return 0;
}
vector<double> ans;
ans.push_back((-b + sqrt(x)) / (2 * a));
ans.push_back((-b - sqrt(x)) / (2 * a));
cout << fixed << setprecision(10);
sort(ans.begin(), ans.end());
if (ans[0] != ans[1]) {
cout << 2 << endl;
cout << ans[0] << endl << ans[1];
} else {
cout << 1 << endl;
cout << ans[0];
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
float a, b, c, zero = 0, count = 0;
cin >> a >> b >> c;
if (a != 0) {
float z = (b * b) - (4 * a * c);
if (z > -1) {
if (b == 0 && c == 0) {
cout << 1 << endl << setprecision(10) << fixed << 0 << endl;
return 0;
} else if (c == 0) {
cout << 2 << endl;
cout << setprecision(10) << fixed << min(-b / a, zero) << endl
<< max(-b / a, -zero) << endl;
return 0;
} else if (z == 0) {
cout << 1 << endl << setprecision(10) << fixed << -b / (2 * a) << endl;
return 0;
} else {
cout << 2 << endl
<< setprecision(10) << fixed
<< min(((-b - sqrt(z)) / (2 * a)), ((-b + sqrt(z)) / (2 * a)))
<< endl
<< setprecision(10) << fixed
<< max(((-b - sqrt(z)) / (2 * a)), ((-b + sqrt(z)) / (2 * a)))
<< endl;
return 0;
}
} else {
cout << 0 << endl;
return 0;
}
} else if (b != 0) {
cout << 1 << endl << setprecision(10) << fixed << -c / b << endl;
return 0;
} else if (c != 0) {
cout << 0 << endl;
return 0;
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes(long long n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (long long p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (long long i = p * p; i <= n; i += p) prime[i] = false;
}
}
}
long long power(long long x, long long y, long long m) {
if (y == 0) return 1;
long long p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
void print(vector<long long> a) {
for (long long i = 0; i < a.size(); i++) cout << a[i] << " ";
cout << "\n";
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
bool cmp(long long a, long long b) { return a > b; }
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long a, b, c;
cin >> a >> b >> c;
if (a) {
if (b) {
long long d = b * b - 4 * a * c;
if (d >= 0) {
double f = sqrt(d);
double r1 = -b + f;
r1 = double(r1) / double(2 * a);
double r2 = -b - f;
r2 = double(r2) / double(2 * a);
if (r1 != r2) {
if (r2 > r1) swap(r1, r2);
cout << 2 << "\n" << fixed << setprecision(8) << r2 << "\n" << r1;
} else
cout << 1 << "\n" << fixed << setprecision(8) << r2;
} else {
cout << 0;
}
} else {
double d = -c;
d = d / double(a);
if (d >= 0) {
cout << 1 << "\n" << fixed << setprecision(8) << sqrt(d);
} else {
cout << 0;
}
}
} else {
if (b) {
double d = double(-c) / double(b);
cout << 1 << "\n" << fixed << setprecision(8) << d;
} else {
if (c)
cout << 0;
else
cout << -1;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
double a, b, c;
int main() {
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
if (c == 0)
cout << -1;
else
cout << 0;
} else {
cout << 1 << endl;
double x = -c / b;
printf("%.8f", x);
}
} else {
double D = b * b - 4 * a * c;
if (D < 0) {
cout << 0 << endl;
} else if (D == 0) {
cout << 1 << endl;
printf("%.8f", -b / (2 * a));
} else {
cout << 2 << endl;
printf("%.8f", min((-b - sqrt(D)) / (2 * a), (-b + sqrt(D)) / (2 * a)));
cout << endl;
printf("%.8f", max((-b - sqrt(D)) / (2 * a), (-b + sqrt(D)) / (2 * a)));
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
double f = 0, g = 0, i = 0;
cin >> a >> b >> c;
f = ((-1 * b + sqrt((b * b) - (4 * a * c))) / (2 * a));
g = ((-1 * b - sqrt((b * b) - (4 * a * c))) / (2 * a));
i = ((-1 * c) / b);
if (a == 0 && b != 0) {
cout << 1 << endl << fixed << setprecision(10) << i;
} else if (a == 0 && b == 0 && c == 0) {
cout << -1;
} else if ((b * b) < (4 * a * c) || (a == 0 && b == 0)) {
cout << 0;
} else if (g == f) {
cout << 1 << endl << fixed << setprecision(10) << g;
} else {
if (g < f) {
cout << 2 << endl << fixed << setprecision(10) << g << endl << f;
} else if (g > f) {
cout << 2 << endl << fixed << setprecision(10) << f << endl << g;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double A, B, C;
double d, x1, x2;
cin >> A >> B >> C;
cout << fixed << setprecision(10);
if (A == 0) {
if (B == 0) {
if (C == 0) {
cout << -1;
} else {
cout << 0;
}
} else {
cout << 1 << endl;
cout << -C / B;
}
} else {
d = B * B - 4 * A * C;
if (d == 0) {
cout << 1 << endl << -B / (2 * A);
} else if (d < 0)
cout << 0;
else {
cout << 2 << endl;
x1 = (-B - sqrt(d)) / (2 * A);
x2 = (-B + sqrt(d)) / (2 * A);
cout << min(x1, x2) << endl;
cout << max(x1, x2);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
double A, B, C;
double eps = 1e-9;
cin >> A >> B >> C;
double D = B * B - 4.0 * A * C;
if (fabs(A) < eps) {
if (fabs(B) < eps) {
if (fabs(C) < eps)
cout << "-1";
else
cout << "0";
} else {
cout << "1\n";
double x = -C / B;
printf("%.9f", x);
}
} else if (D < -eps) {
cout << "0";
} else {
double x1 = (-B + sqrt(D)) / (2.0 * A);
double x2 = (-B - sqrt(D)) / (2.0 * A);
if (fabs(D) < eps) {
cout << "1\n";
printf("%.9f", x1);
} else {
cout << "2\n";
if (x1 > x2) swap(x1, x2);
printf("%.9f\n", x1);
printf("%.9f", x2);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long delta(long long a, long long b, long long c) {
return b * b - 4 * a * c;
}
int main() {
long long a, b, c;
scanf("%I64d%I64d%I64d", &a, &b, &c);
if (a == 0 && b == 0 && c == 0) {
puts("-1");
return 0;
} else if (a == 0 && b == 0 && c != 0) {
puts("0");
return 0;
} else if (a == 0 && b != 0) {
puts("1");
double x = (b * c > 0) ? -(sqrt(c * c) / sqrt(b * b))
: (sqrt(c * c) / sqrt(b * b));
printf("%8lf", x);
return 0;
}
int s;
if (delta(a, b, c) < 0) {
puts("0");
return 0;
} else if (delta(a, b, c) == 0) {
puts("1");
double x = -b / (2 * a);
printf("%8lf", x);
return 0;
} else {
puts("2");
double x1 = (-b - sqrt(delta(a, b, c))) / (2 * a),
x2 = (-b + sqrt(delta(a, b, c))) / (2 * a);
printf("%8lf", min(x1, x2));
printf("\n");
printf("%8lf", max(x1, x2));
return 0;
}
}
|
#include <bits/stdc++.h>
using namespace std;
double delta(long long a, long long b, long long c) {
long long res = 0;
res = b * b - 4 * a * c;
return res;
}
int main() {
int n;
double a, b, c;
double r1, r2;
cin >> a >> b >> c;
if (a == 0) {
n = (b == 0) ? 0 : 1;
r1 = (b == 0) ? 0 : -c / b;
if (b == 0 and c == 0) n = -1;
} else {
double d = delta(a, b, c);
if (d > 0) {
n = 2;
r1 = (-b + sqrt(d)) / (2 * a);
r2 = (-b - sqrt(d)) / (2 * a);
} else if (d == 0) {
n = 1;
r1 = -b / (2 * a);
} else {
n = 0;
}
}
cout << n << endl;
if (n == 1) {
printf("%.6f\n", r1);
}
if (n == 2) {
if (r1 > r2) {
printf("%.6f\n", r2);
printf("%.6f\n", r1);
} else {
printf("%.6f\n", r1);
printf("%.6f\n", r2);
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
scanf("%lf%lf%lf", &a, &b, &c);
if (a == 0 && b == 0 && c == 0)
printf("-1\n");
else if (a == 0 && b == 0 && c)
printf("0\n");
else if (a == 0)
printf("1\n%.10lf\n", -c / b);
else if (b == 0) {
if (c < 0) printf("2\n%.10lf\n%.10lf\n", -sqrt(c / a), sqrt(c / a));
if (c == 0)
printf("1\n%.10lf\n", 0);
else
printf("0\n");
} else {
if (1ll * b * b - 4 * a * c < 0)
printf("0\n");
else {
double x, y;
x = (-b - sqrt(1ll * b * b - 4 * a * c)) / (2 * a);
y = (-b + sqrt(1ll * b * b - 4 * a * c)) / (2 * a);
if (y + 1e-9 < x)
printf("2\n%.10lf\n%.10lf\n", y, x);
else if (x + 1e-9 < y)
printf("2\n%.10lf\n%.10lf\n", x, y);
else
printf("1\n%.10lf\n", x);
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long double EPS = (long double)1e-7;
const long double PI = acos(0) * 2;
bool isZero(const long double& x) { return abs(x) <= EPS; }
int sign(const long double& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); }
long long gcd(long long a, long long b) {
for (; b; a %= b, swap(a, b)) {
}
return abs(a);
}
pair<long long, long long> operator+(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return pair<long long, long long>(a.first + b.first, a.second + b.second);
}
pair<long long, long long> operator-(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return pair<long long, long long>(a.first - b.first, a.second - b.second);
}
pair<long long, long long> operator*(const pair<long long, long long>& a,
const long long& b) {
return pair<long long, long long>(a.first * b, a.second * b);
}
long long operator*(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return a.first * b.second - b.first * a.second;
}
long long ccw(const pair<long long, long long>& a,
const pair<long long, long long>& b,
const pair<long long, long long>& c) {
return a * b + b * c + c * a;
}
void fg(vector<int> G[], int a, int b) {
G[a].emplace_back(b);
G[b].emplace_back(a);
}
void fg(vector<pair<int, int> > G[], int a, int b, int c) {
G[a].emplace_back(b, c);
G[b].emplace_back(a, c);
}
long long A, B, C;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> A >> B >> C;
if (!A && !B) {
puts(C ? "0" : "-1");
exit(0);
}
if (!A) {
printf("1\n%.10lf\n", double(-C) / B);
exit(0);
}
long long D = B * B - 4 * A * C;
if (D < 0) {
puts("0");
exit(0);
}
if (!D) {
printf("1\n%.10lf\n", double(-B) / A / 2);
exit(0);
}
if (A < 0) {
A = -A;
B = -B;
C = -C;
}
printf("2\n%.10lf\n%.10lf\n", double(-B - sqrt(D)) / A / 2,
double(-B + sqrt(D)) / A / 2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c;
double res1, res2;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
if (c == 0)
cout << "-1\n";
else
cout << "0\n";
} else {
cout << "1\n";
cout << fixed << setprecision(10) << (-c / b) << endl;
}
} else {
if (b * b - 4 * a * c < 0)
cout << "0\n";
else if (b * b - 4 * a * c == 0) {
res1 = (-b) * 1.0 / (2 * a);
cout << "1\n";
cout << fixed << setprecision(10) << res1 << endl;
} else {
res1 = (-b + sqrt(b * b - 4 * a * c)) * 1.0 / (2 * a);
res2 = (-b - sqrt(b * b - 4 * a * c)) * 1.0 / (2 * a);
cout << "2\n";
if (res1 > res2) {
cout << fixed << setprecision(10) << res2 << endl;
cout << fixed << setprecision(10) << res1 << endl;
} else {
cout << fixed << setprecision(10) << res1 << endl;
cout << fixed << setprecision(10) << res2 << endl;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
struct edge {
int to;
edge* next;
} E[6010], *ne = E, *first[3010];
void link(int a, int b) {
*ne = (edge){b, first[a]};
first[a] = ne++;
}
int C[3010], cnt;
bool tag[3010];
int dfs1(int i, int f) {
int t;
tag[i] = 1;
for (edge* e = first[i]; e; e = e->next)
if (e->to != f) {
if (tag[e->to])
return C[cnt++] = i, e->to;
else if ((t = dfs1(e->to, i)) > -1)
return (C[cnt++] = i) == t ? -1 : t;
else if (t == -1)
return tag[i] = 0, -1;
}
tag[i] = 0;
return -2;
}
double dfs2(int i, int f, int d, int l, int r) {
double s = 1.0 / (d + l) + 1.0 / (d + r) - 1.0 / (d + l + r);
for (edge* e = first[i]; e; e = e->next)
e->to != f && (!tag[i] || !tag[e->to]) ? s += dfs2(e->to, i, d + 1, l, r)
: 1;
if (tag[i] && (f == -1 || !tag[f])) {
int k = 0;
while (C[k] != i) k++;
r = cnt - 2;
for (int j = (k + 1) % cnt; j != k; j = (j + 1) % cnt, l++, r--)
s += dfs2(C[j], i, d + 1, l, r);
}
return s;
}
int main() {
int n, a, b;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d%d", &a, &b), link(a, b), link(b, a);
dfs1(0, -1);
double ans = 0;
for (int i = 0; i < n; i++) ans += dfs2(i, -1, 1, 0, 0);
printf("%.12lf\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
int vi[3005], cir;
int ori[3005][3005];
int sccl[3005][3005];
vector<int> s[3005], scc[3005], g[3005];
stack<int> sta;
int dfs_cnt = 0, pre[3005] = {0}, sccno[3005] = {0}, scc_cnt = 0;
int dfs(int u, int fa) {
int lowu, lowv, i;
lowu = pre[u] = ++dfs_cnt;
sta.push(u);
for (i = 0; i < s[u].size(); i++) {
int v = s[u][i];
if (v == fa) continue;
if (pre[v] == 0) {
lowv = dfs(v, u);
lowu = min(lowu, lowv);
} else if (pre[v] < pre[u] && sccno[v] == 0) {
lowu = min(lowu, pre[v]);
}
}
if (lowu == pre[u]) {
++scc_cnt;
while (sta.size()) {
int x = sta.top();
sta.pop();
sccno[x] = scc_cnt;
scc[scc_cnt].push_back(x);
if (x == u) break;
}
cir = max(cir, (int)scc[scc_cnt].size());
}
return lowu;
}
void dfs1(int u, int add, int st) {
ori[st][u] = add;
for (int i = 0; i < s[u].size(); i++) {
int v = s[u][i];
if (vi[v] == st) continue;
vi[v] = st;
dfs1(v, add + 1, st);
}
}
void dfs2(int u, int add, int st) {
sccl[st][u] = add;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (vi[v] == st) continue;
vi[v] = st;
dfs2(v, add + 1, st);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
s[x].push_back(y);
s[y].push_back(x);
}
dfs(0, -1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < s[i].size(); j++) {
int x = sccno[i];
int y = sccno[s[i][j]];
if (x == y) continue;
g[x].push_back(y);
g[y].push_back(x);
}
}
memset(vi, -1, sizeof(vi));
for (int i = 0; i < n; i++) {
vi[i] = i;
dfs1(i, 0, i);
}
memset(vi, -1, sizeof(vi));
for (int i = 1; i <= scc_cnt; i++) {
vi[i] = i;
dfs2(i, 0, i);
}
double ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
ans += 1;
continue;
}
int x = sccl[sccno[i]][sccno[j]];
int y = ori[i][j] - x;
int z = cir - y;
if (y && z)
ans += 1.0 / (x + y + 1) + 1.0 / (x + z + 1) - 1.0 / (x + y + z);
else
ans += 1.0 / (x + 1);
}
}
printf("%.12f\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = ~0U >> 1;
const long long INF = ~0ULL >> 1;
;
int n, x, y, cnt[4000], vis[4000], circle[4000], Cnt, top;
double ans;
vector<int> E[4000];
void check(int S, int u, int dis) {
vis[u] = 1;
for (int i = (0); i < (E[u].size()); ++i)
if (!vis[E[u][i]])
check(S, E[u][i], dis + 1);
else if (E[u][i] == S && dis > 1) {
circle[S] = 1;
++Cnt;
return;
}
vis[u] = 0;
}
void dfs(int S, int u, int c) {
if (circle[u]) ++top;
vis[u] = 1;
if (top > 1) {
int L1 = top;
int L2 = Cnt - L1 + min(2, top);
ans += 0.5 * (1. / c + 1. / (c - L1 + L2) - 1. / (c + L2 - 2));
} else
ans += 1. / c;
for (int i = (0); i < (E[u].size()); ++i)
if (!vis[E[u][i]]) dfs(S, E[u][i], c + 1);
vis[u] = 0;
if (circle[u]) --top;
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n); ++i)
scanf("%d%d", &x, &y), E[x].push_back(y), E[y].push_back(x);
for (int i = (0); i < (n); ++i) {
memset(vis, 0, sizeof(vis));
check(i, i, 0);
}
for (int i = (0); i < (n); ++i) {
memset(vis, 0, sizeof(vis));
dfs(i, i, 1);
}
printf("%.10lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double ans;
int d[3005], lp[3005], bl[3005], vst[3005], lc[3005][3005], dep[3005], cnt,
fa[3005], pos[3005];
vector<int> son[3005];
queue<int> q;
void dfs(int now) {
vst[lp[pos[now] = ++cnt] = now] = 2;
for (int T, i = 0; i < son[now].size(); ++i)
if (!vst[T = son[now][i]]) dfs(T);
}
void Dfs(int cc, int now, int fat) {
bl[now] = cc;
dep[now] = dep[fat] + 1;
fa[now] = fat;
for (int T, i = 0; i < son[now].size(); ++i)
if ((T = son[now][i]) != fat && vst[T] == 1) Dfs(cc, T, now);
}
int solve(int x, int y) {
if (lc[x][y]) return lc[x][y];
if (dep[x] > dep[y])
lc[x][y] = solve(fa[x], y);
else
lc[x][y] = solve(fa[y], x);
return lc[x][y];
}
int n, i, x, y, T, j, Pos, f1, f2;
int main() {
for (cin >> n, i = 1; i <= n; ++i)
cin >> x >> y, son[++x].push_back(++y), son[y].push_back(x), ++d[x], ++d[y];
for (i = 1; i <= n; lc[i][i] = i, ++i)
if (d[i] == 1) q.push(i);
while (q.size()) {
x = q.front();
q.pop();
vst[x] = 1;
for (i = 0; i < son[x].size(); ++i)
if (!vst[T = son[x][i]] && (--d[T]) == 1) q.push(T);
}
for (i = 1; i <= n; ++i)
if (!vst[i]) {
dfs(i);
break;
}
for (i = 1; i <= cnt; ++i) Dfs(i, lp[i], 0);
for (i = 2; i <= n; ++i) {
for (j = 1; j < i; ++j) {
if (bl[i] == bl[j]) {
Pos = dep[i] + dep[j] - (dep[solve(i, j)] << 1) + 1;
ans += 2.0 / Pos;
} else {
Pos = dep[i] + dep[j];
f1 = abs(bl[i] - bl[j]) - 1;
f2 = cnt - f1 - 2;
ans += 2.0 / (Pos + f1) + 2.0 / (Pos + f2) - 2.0 / (Pos + f1 + f2);
}
}
++ans;
}
printf("%.10lf", ans + 1);
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void in(T &x) {
x = 0;
short f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x *= f;
}
template <class T>
inline void out(T x, const char c = '\n') {
static short st[30];
short m = 0;
if (x < 0) putchar('-'), x = -x;
do st[++m] = x % 10, x /= 10;
while (x);
while (m) putchar(st[m--] | '0');
putchar(c);
}
template <class T, class... Args>
inline void in(T &x, Args &...args) {
in(x);
in(args...);
}
template <class T, class... Args>
inline void out(const T &x, const Args &...args) {
out(x, ' ');
out(args...);
}
template <class T>
inline void prt(T a[], int n) {
for (register int i = 0; i < n; ++i) out(a[i], ' ');
putchar('\n');
}
template <class T>
inline void clr(T a[], int n) {
memset(a, 0, sizeof(T) * n);
}
template <class T>
inline void clr(T *a, T *b) {
memset(a, 0, sizeof(T) * (b - a));
}
template <class T>
inline bool ckmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
inline bool ckmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
namespace MOD_CALC {
const int md = 998244353;
inline int add(const int a, const int b) {
return a + b >= md ? a + b - md : a + b;
}
inline int sub(const int a, const int b) {
return a - b < 0 ? a - b + md : a - b;
}
inline int mul(const int a, const int b) { return (long long)a * b % md; }
inline void inc(int &a, const int b) { (a += b) >= md ? a -= md : 0; }
inline void dec(int &a, const int b) { (a -= b) < 0 ? a += md : 0; }
inline int qpow(int a, int b) {
int r = 1;
for (; b; b >>= 1, a = mul(a, a))
if (b & 1) r = mul(r, a);
return r;
}
inline int mdinv(const int a) { return qpow(a, md - 2); }
} // namespace MOD_CALC
using namespace MOD_CALC;
namespace i207M {
int n;
vector<int> E[3005];
int du[3005];
int sz;
void topo() {
static int q[3005];
int hd = 1, tl = 0;
for (register int i = 1; i <= n; ++i)
if (du[i] == 1) q[++tl] = i;
while (hd <= tl) {
int x = q[hd++];
for (const auto &v : E[x])
if (--du[v] == 1) q[++tl] = v;
}
for (register int i = 1; i <= n; ++i)
if (du[i] == 2) ++sz;
}
double ans;
bool vis[3005];
void dfs(int x, int d, int h) {
if (vis[x]) return;
vis[x] = 1;
h += (du[x] == 2);
if (h >= 2)
ans += 1.0 / d + 1.0 / (d - h + 2 + sz - h) - 1.0 / (d + sz - h);
else
ans += 1.0 / d;
for (const auto &v : E[x]) dfs(v, d + 1, h);
}
signed main() {
in(n);
for (register int i = 1, a, b; i <= n; ++i) {
in(a, b);
++a, ++b;
E[a].push_back(b), E[b].push_back(a);
++du[a], ++du[b];
}
topo();
for (register int i = 1; i <= n; ++i) {
memset(vis, 0, sizeof(vis));
dfs(i, 1, 0);
}
printf("%.10f\n", ans);
return 0;
}
} // namespace i207M
signed main() {
i207M::main();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3005;
vector<int> to[N];
int f[N][15], st[N], top, cir[N], dep[N], col[N], n, cnt, siz, vis[N], oncir[N];
void add(int a, int b) {
to[a].push_back(b);
to[b].push_back(a);
}
int find(int now, int fa) {
if (vis[now]) {
int v;
do {
v = st[top--];
cir[++siz] = v;
oncir[v] = 1;
} while (v != now);
return 1;
}
vis[st[++top] = now] = 1;
for (auto v : to[now])
if (v != fa && find(v, now)) return 1;
vis[st[top--]] = 0;
return 0;
}
void dfs(int now, int fa, int se) {
col[now] = se;
f[now][0] = fa;
dep[now] = dep[fa] + 1;
for (auto v : to[now])
if (v != fa && !oncir[v]) dfs(v, now, se);
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 14; i >= 0; i--)
if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
for (int i = 14; i >= 0; i--)
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
int dis(int x, int y) { return dep[x] + dep[y] - 2 * dep[lca(x, y)] + 1; }
int main() {
scanf("%d", &n);
for (int i = 1, x, y; i <= n; i++) {
scanf("%d%d", &x, &y);
add(x + 1, y + 1);
}
find(1, 0);
for (int i = 1; i <= siz; i++) dfs(cir[i], 0, i);
for (int i = 1; (1 << i) <= n; i++)
for (int j = 1; j <= n; j++) f[j][i] = f[f[j][i - 1]][i - 1];
double ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (col[i] == col[j])
ans += 1.0 / dis(i, j);
else {
int x = dep[i] + dep[j];
int y = abs(col[i] - col[j]) - 1;
int z = siz - 2 - y;
ans += 1.0 / (x + y) + 1.0 / (x + z) - 1.0 / (x + y + z);
}
}
printf("%.10lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
namespace _buff {
const size_t BUFF = 1 << 19;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
if (ib == ie) {
ib = ibuf;
ie = ibuf + fread(ibuf, 1, BUFF, stdin);
}
return ib == ie ? -1 : *ib++;
}
} // namespace _buff
LL read() {
using namespace _buff;
LL ret = 0;
bool pos = true;
char c = getc();
for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
assert(~c);
}
if (c == '-') {
pos = false;
c = getc();
}
for (; c >= '0' && c <= '9'; c = getc()) {
ret = (ret << 3) + (ret << 1) + (c ^ 48);
}
return pos ? ret : -ret;
}
const size_t N = 3e3 + 5;
double ans;
int a[N], b[N];
vector<int> g[N], gg[N], sub[N], cyc;
bool vis[N], oncyc[N];
int par[N], dep[N];
void dfs1(int u = 1, int p = 0, int pe = -1) {
vis[u] = true;
par[u] = p;
for (int i : gg[u]) {
if (i == pe) continue;
int v = a[i] ^ b[i] ^ u;
if (vis[v]) {
if (cyc.empty()) {
for (int x = u;; x = par[x]) {
cyc.emplace_back(x);
oncyc[x] = true;
if (x == v) break;
}
}
} else {
dfs1(v, u, i);
}
}
}
void dfs2(int rt, int u, int p = 0, int d = 0) {
sub[par[u] = rt].emplace_back(u);
dep[u] = d;
for (int v : g[u]) {
if (v != p && !oncyc[v]) {
dfs2(rt, v, u, d + 1);
}
}
}
void bfs(int s) {
static int dis[N];
memset(dis, -1, sizeof dis);
queue<int> que;
que.emplace(s);
dis[s] = 1;
while (!que.empty()) {
int u = que.front();
que.pop();
ans += 1. / dis[u];
for (int v : g[u]) {
if (!~dis[v] && par[u] == par[v]) {
dis[v] = dis[u] + 1;
que.emplace(v);
}
}
}
}
int main() {
int n = read();
for (int i = 0; i < n; ++i) {
int u = a[i] = read() + 1, v = b[i] = read() + 1;
g[u].emplace_back(v);
g[v].emplace_back(u);
gg[u].emplace_back(i);
gg[v].emplace_back(i);
}
dfs1();
for (int u : cyc) {
dfs2(u, u);
}
for (unsigned i = 0; i < cyc.size(); ++i) {
for (unsigned j = 0; j < i; ++j) {
int l1 = i - j + 1, l2 = cyc.size() + 2 - l1;
for (int u : sub[cyc[i]]) {
for (int v : sub[cyc[j]]) {
ans += ((double)(cyc.size() - l1) / (l1 + dep[u] + dep[v]) +
(double)(cyc.size() - l2) / (l2 + dep[u] + dep[v]) + 1) /
(cyc.size() + dep[u] + dep[v]);
}
}
}
}
ans *= 2;
for (int i = 1; i <= n; ++i) {
bfs(i);
}
printf("%.19f", ans);
return 0;
}
|
#include <bits/stdc++.h>
const int maxn = 3005;
const int maxm = 6005;
const int inf = 999999999;
struct edge {
int to, next;
};
int n;
edge e[maxm];
int head[maxn], tot;
int stn[maxn], stk[maxn], top = 1;
int cir[maxn], circ, ind[maxn];
int cirr[maxn];
bool vis[maxn];
int dis[maxn][maxn];
bool vis2[maxn][maxn];
void dfs2(int u, int p) {
vis[u] = true;
for (int t = head[u]; t; t = e[t].next) {
int v = e[t].to;
if (v == p || vis[v]) continue;
cirr[v] = cirr[u];
dfs2(v, u);
}
}
void dfs3(int u, int p, int r) {
vis2[r][u] = true;
for (int t = head[u]; t; t = e[t].next) {
int v = e[t].to;
if (v == p || vis2[r][v]) continue;
dis[r][v] = dis[r][u] + 1;
dfs3(v, u, r);
}
}
bool dfs1(int u, int p) {
stk[top] = u;
stn[u] = top++;
for (int t = head[u]; t; t = e[t].next) {
int v = e[t].to;
if (v == p) continue;
if (stn[v]) {
for (int i = stn[v]; i < top; i++)
cir[circ] = stk[i], ind[stk[i]] = circ++, vis[stk[i]] = true;
return true;
}
if (dfs1(v, u)) return true;
}
top--;
return false;
}
void add_edge(int from, int to) {
e[++tot] = (edge){to, head[from]};
head[from] = tot;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%d%d", &x, &y);
x++, y++;
add_edge(x, y);
add_edge(y, x);
}
dfs1(1, 0);
for (int i = 0; i < circ; i++) cirr[cir[i]] = cir[i], dfs2(cir[i], 0);
for (int i = 1; i <= n; i++) dis[i][i] = 0, dfs3(i, 0, i);
double ans = 0.0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (cirr[i] == cirr[j])
ans += 1.0 / (dis[i][j] + 1);
else
ans += 1.0 / (dis[i][j] + 1) +
1.0 / (dis[i][j] + circ - dis[cirr[i]][cirr[j]] * 2 + 1) -
1.0 / (dis[i][j] + circ - dis[cirr[i]][cirr[j]]);
}
printf("%0.8lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
static const int maxn = 3000 + 5;
static const int maxm = 6000 + 5;
static int n, c;
static int suf[maxn], pre[maxm], tar[maxm], e;
static int vis[maxn], dep[maxn], dis[maxn];
static int deg[maxn], que[maxn], head, tail;
static double ans;
inline int read() {
int k = 0, f = 1;
char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') f = -1;
c = getchar();
}
while (c <= '9' && c >= '0') {
k = k * 10 + c - '0';
c = getchar();
}
return k * f;
}
void dfs(int u) {
vis[u] = 1;
for (int i = suf[u]; i; i = pre[i])
if (!vis[tar[i]]) {
dep[tar[i]] = dep[u] + 1;
if (!dis[tar[i]]) {
dis[tar[i]] = dis[u] + 1;
ans += 1.0 / dis[tar[i]];
} else
ans += 1.0 / dep[tar[i]] - 2.0 / (dis[tar[i]] + dep[tar[i]] + c - 2);
dfs(tar[i]);
}
vis[u] = 0;
}
int main() {
n = read();
for (int u, v, i = 1; i <= n; ++i) {
++deg[u = read() + 1];
++deg[v = read() + 1];
pre[++e] = suf[u], suf[u] = e, tar[e] = v;
pre[++e] = suf[v], suf[v] = e, tar[e] = u;
}
for (int i = 1; i <= n; ++i)
if (deg[i] == 1) que[tail++] = i;
while (head != tail)
for (int i = suf[que[head++]]; i; i = pre[i])
if (--deg[tar[i]] == 1) que[tail++] = tar[i];
c = n - tail;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) dis[j] = dep[j] = 0;
dis[i] = dep[i] = 1;
dfs(i);
}
printf("%.10lf", ans + n);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[3005];
bool vis[3005], cir[3005];
int q, s, n;
int Find_Cir(int x, int y) {
vis[x] = 1;
for (int i = 0; i < v[x].size(); i++)
if (!vis[v[x][i]]) {
int t = Find_Cir(v[x][i], x);
if (t) {
if (t > 0) {
cir[x] = 1;
s++;
}
return t == x ? -1 : t;
}
} else if (v[x][i] != y) {
cir[x] = s = 1;
return v[x][i];
}
return 0;
}
double ans;
void Dfs(int x) {
vis[x] = 1;
q += cir[x];
n++;
if (q > 1) {
int c = n - q + s, a = q - 2, b = s - q;
ans += (double)a / c / (c - a) + (double)b / c / (c - b) + 1.0 / c;
} else
ans += 1.0 / n;
for (int i = 0; i < v[x].size(); i++)
if (!vis[v[x][i]]) Dfs(v[x][i]);
n--;
q -= cir[x];
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1, x, y; i <= n; i++)
scanf("%d%d", &x, &y), v[x + 1].push_back(y + 1), v[y + 1].push_back(x + 1);
Find_Cir(1, 0);
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
Dfs(i);
}
printf("%.7lf\n", ans);
}
|
#include <bits/stdc++.h>
const int N = 3e3 + 10;
int n, vis[N], px, py, dep[N], fa[12][N], id[N], flag[N], lg[N];
std::vector<int> e[N], x, y;
int main() {
scanf("%d", &n);
for (int i(2); i < N; ++i) {
lg[i] = lg[i / 2] + 1;
}
for (int i(1), x, y; i <= n; ++i) {
scanf("%d %d", &x, &y);
++x;
++y;
e[x].push_back(y);
e[y].push_back(x);
}
std::function<void(int, int)> dfs = [&](int u, int f) {
vis[u] = 1;
dep[u] = dep[fa[0][u] = f] + 1;
for (int i : e[u]) {
if (i ^ f) {
if (vis[i]) {
px = i;
py = u;
} else {
dfs(i, u);
}
}
}
return;
};
dfs(1, 0);
while (dep[px] > dep[py]) {
x.push_back(px);
px = fa[0][px];
}
while (px != py) {
x.push_back(px);
px = fa[0][px];
y.push_back(py);
py = fa[0][py];
}
reverse(y.begin(), y.end());
x.push_back(px);
for (int &i : y) {
x.push_back(i);
}
int cir = x.size();
for (int i(0); i < x.size(); ++i) {
flag[x[i]] = 1;
id[x[i]] = i + 1;
}
std::function<void(int, int)> dfs1 = [&](int u, int f) {
dep[u] = dep[fa[0][u] = f] + 1;
if (f) {
id[u] = id[f];
}
for (int &to : e[u]) {
if (to != f && !id[to]) {
dfs1(to, u);
}
}
};
for (int i(0); i < x.size(); ++i) {
dfs1(x[i], 0);
}
for (int i(1); i < 12; ++i) {
for (int j(1); j <= n; ++j) {
fa[i][j] = fa[i - 1][fa[i - 1][j]];
}
}
std::function<int(int, int)> LCA = [&](int a, int b) {
if (dep[a] < dep[b]) {
std::swap(a, b);
}
while (dep[a] > dep[b]) {
a = fa[lg[dep[a] - dep[b]]][a];
}
if (a == b) {
return a;
}
for (int i(11); ~i; --i) {
if (fa[i][a] != fa[i][b]) {
a = fa[i][a];
b = fa[i][b];
}
}
return fa[0][a];
};
double ans(0);
for (int i(1); i <= n; ++i) {
for (int j(1); j <= n; ++j) {
if (id[i] == id[j]) {
int l = LCA(i, j), len = dep[i] + dep[j] - 2 * dep[l];
ans += 1.0 / (len + 1);
} else {
int x = dep[i] + dep[j],
y = std::max(id[i], id[j]) - std::min(id[i], id[j]) - 1,
z = cir - 2 - y;
ans += 1.0 / (x + y) + 1.0 / (z + x) - 1.0 / (x + y + z);
}
}
}
printf("%.10lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3000 + 3;
struct Edge {
int to, nxt;
} e[N << 1];
int n, cnt;
int hd[N], top, q[N], tail, cur[N], c[N], pos[N], belong[N], dep[N], dis[N];
bool vis[N], used[N];
long double ans;
inline void addedge(int x, int y) {
e[++top].to = y;
e[top].nxt = hd[x];
hd[x] = top;
e[++top].to = x;
e[top].nxt = hd[y];
hd[y] = top;
}
inline void find_cycle(void) {
int tail, now;
for (int i = 1; i <= n; ++i) cur[i] = hd[i];
for (vis[q[tail = 1] = 1] = 1; tail;)
for (int& p = cur[now = q[tail]]; ~p; p = e[p].nxt)
if (!used[p >> 1]) {
used[p >> 1] = 1;
if (vis[e[p].to]) {
do c[++cnt] = q[tail--];
while (c[cnt] != e[p].to);
return;
}
vis[q[++tail] = e[p].to] = 1;
p = e[p].nxt;
break;
} else if (!~e[p].nxt) {
--tail;
break;
}
}
inline void init(void) {
find_cycle();
memset(vis, 0, sizeof vis);
for (int i = 1; i <= cnt; ++i) vis[c[i]] = 1;
for (int i = 1, l, r, now, p; i <= cnt; ++i) {
pos[c[i]] = i;
for (q[l = r = 1] = c[i]; l <= r; ++l) {
belong[now = q[l]] = c[i];
for (p = hd[now]; ~p; p = e[p].nxt)
if (!vis[e[p].to])
vis[q[++r] = e[p].to] = 1, dep[e[p].to] = dep[now] + 1;
}
}
}
inline void solve(int x) {
memset(vis, 0, sizeof vis);
memset(dis, 0, sizeof dis);
int l, r, now, p;
for (vis[q[l = r = 1] = x] = 1; l <= r; ++l) {
now = q[l];
ans += 1.0 / (dis[now] + 1);
for (p = hd[now]; ~p; p = e[p].nxt)
if (!(pos[now] && pos[e[p].to]))
if (!vis[e[p].to])
vis[q[++r] = e[p].to] = 1, dis[e[p].to] = dis[now] + 1;
}
}
inline void calc(int x, int y) {
int a = dep[x] + dep[y], b = abs(pos[belong[x]] - pos[belong[y]]) - 1,
c = cnt - 2 - b;
ans += 1.0 / (a + b + 2) + 1.0 / (a + c + 2) - 1.0 / (a + cnt);
}
int main(int argc, char** argv) {
int x, y;
memset(hd, -1, sizeof hd);
top = -1;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d%d", &x, &y), addedge(x + 1, y + 1);
init();
for (int i = 1; i <= n; ++i) {
solve(i);
for (int j = 1; j <= n; ++j)
if (belong[i] != belong[j]) calc(i, j);
}
printf("%.10lf\n", (double)ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline bool SR(int &x) { return scanf("%d", &x) == 1; }
inline bool SR(long long &x) { return scanf("%lld", &x) == 1; }
inline bool SR(double &x) { return scanf("%lf", &x) == 1; }
inline bool SR(char *s) { return scanf("%s", s) == 1; }
inline bool RI() { return true; }
template <typename I, typename... T>
inline bool RI(I &x, T &...tail) {
return SR(x) && RI(tail...);
}
inline void SP(const int x) { printf("%d", x); }
inline void SP(const long long x) { printf("%lld", x); }
inline void SP(const double x) { printf("%.16lf", x); }
inline void SP(const char *s) { printf("%s", s); }
inline void PL() { puts(""); }
template <typename I, typename... T>
inline void PL(const I x, const T... tail) {
SP(x);
if (sizeof...(tail)) putchar(' ');
PL(tail...);
}
template <typename I>
void _DOING(const char *s, I &&x) {
cerr << s << " = " << x << endl;
}
template <typename I, typename... T>
void _DOING(const char *s, I &&x, T &&...tail) {
int c = 0;
while (*s != ',' || c != 0) {
if (*s == '(' || *s == '[' || *s == '{') c++;
if (*s == ')' || *s == ']' || *s == '}') c--;
cerr << *s++;
}
cerr << " = " << x << " , ";
_DOING(s + 1, tail...);
}
inline int RAND() {
static int x = 880301;
return (x = x * 0xdefaced + 1) & 0x7fffffff;
}
const int MAX_N = 3000 + 10;
double ans;
int cycle_cnt;
int deg[MAX_N];
bool vis[MAX_N];
bool tag[MAX_N];
vector<int> G[MAX_N];
void dfs(int u, int a, int b) {
vis[u] = true;
if (b <= 1)
ans += 1.0 / a;
else
ans +=
1.0 / a - 1.0 / (a - b + cycle_cnt) + 1.0 / (a - b + cycle_cnt - b + 2);
for (int v : G[u])
if (!vis[v]) dfs(v, a + 1, b + tag[v]);
}
int main() {
int n;
RI(n);
for (int i = 0; i < int(n); i++) {
int a, b;
RI(a, b);
G[a].push_back(b);
G[b].push_back(a);
deg[a]++;
deg[b]++;
}
vector<int> vec;
for (int i = 0; i < int(n); i++)
if (deg[i] == 1) vec.push_back(i);
for (int i = 0; i < int(((int)vec.size())); i++) {
int u = vec[i];
for (int v : G[u]) {
if (--deg[v] == 1) vec.push_back(v);
}
}
cycle_cnt = n - ((int)vec.size());
for (int i = 0; i < int(n); i++)
if (deg[i] > 1) tag[i] = 1;
for (int i = 0; i < int(n); i++) {
memset(vis, (0), sizeof(vis));
dfs(i, 1, tag[i]);
}
PL(ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int nxt, to;
} e[6005];
int hd[3005], e_cnt;
void add(int u, int v) {
e[++e_cnt] = (Edge){hd[u], v};
hd[u] = e_cnt;
}
int n, dia[3005], dia_cnt, rec[3005];
double ans;
bool vis[3005];
void dfs1(int u, int r, int dep) {
vis[u] = true;
rec[dep] = u;
for (int i = hd[u]; i; i = e[i].nxt)
if (e[i].to != r) {
if (vis[e[i].to]) {
for (int j = 1; j <= dep; j++)
if (rec[j] == e[i].to) {
while (j <= dep) {
dia[++dia_cnt] = rec[j];
j++;
}
break;
}
return;
}
dfs1(e[i].to, u, dep + 1);
if (dia_cnt) return;
}
}
void dfs2(int u, int r, int dep, vector<int> &vec) {
vec.push_back(dep);
for (int i = hd[u]; i; i = e[i].nxt)
if (e[i].to != r && !vis[e[i].to]) {
dfs2(e[i].to, u, dep + 1, vec);
}
}
void dfs4(int u, int r, int dep, int exception) {
ans += 1.0 / dep;
for (int i = hd[u]; i; i = e[i].nxt)
if (e[i].to != r && (!vis[e[i].to] || e[i].to == exception))
dfs4(e[i].to, u, dep + 1, exception);
}
void dfs3(int u, int r, int exception) {
dfs4(u, 0, 1, exception);
for (int i = hd[u]; i; i = e[i].nxt)
if (e[i].to != r && !vis[e[i].to]) dfs3(e[i].to, u, exception);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
a++;
b++;
add(a, b);
add(b, a);
}
dfs1(1, 0, 1);
memset(vis, 0, sizeof vis);
for (int i = 1; i <= dia_cnt; i++) vis[dia[i]] = true;
for (int i = 1; i <= dia_cnt; i++)
for (int j = i + 1; j <= dia_cnt; j++) {
vector<int> v1, v2;
dfs2(dia[i], 0, 0, v1);
dfs2(dia[j], 0, 0, v2);
int d1 = j - i, d2 = dia_cnt - d1;
for (int x : v1)
for (int y : v2) {
ans += 1.0 / (x + y + d1 + 1) + 1.0 / (x + y + d2 + 1) -
1.0 / (x + y + dia_cnt);
}
}
ans *= 2;
for (int i = 1; i <= dia_cnt; i++) dfs3(dia[i], 0, dia[i]);
printf("%.8lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
struct Edge {
int to, next;
};
const int maxn = 6060;
Edge e[maxn];
int st[maxn], tot = -1;
void add(int u, int v) {
e[++tot].to = v;
e[tot].next = st[u];
st[u] = tot;
}
bool vis[3030], cir[maxn];
int fa[maxn], dep[maxn], C;
void prep(int now) {
for (int i = st[now]; i != -1; i = e[i].next)
if (e[i].to != fa[now]) {
fa[e[i].to] = now;
dep[e[i].to] = dep[now] + 1;
prep(e[i].to);
}
}
int f[maxn];
int find(int x) {
if (f[x] == x) return x;
return f[x] = find(f[x]);
}
void Union(int a, int b) { f[find(a)] = find(b); }
void init() {
memset(st, -1, sizeof(st));
cin >> n;
int U, V;
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= n; i++) {
int u, v;
cin >> u >> v;
u++;
v++;
if (find(u) == find(v)) {
U = u, V = v;
continue;
}
add(u, v);
add(v, u);
Union(u, v);
}
dep[1] = 1;
prep(1);
add(U, V);
add(V, U);
if (dep[U] > dep[V]) swap(U, V);
while (dep[V] > dep[U]) {
cir[V] = true;
V = fa[V];
}
while (fa[V] != fa[U]) {
cir[V] = true;
cir[U] = true;
V = fa[V];
U = fa[U];
}
if (V == U)
cir[V] = true;
else
cir[V] = cir[fa[V]] = true;
for (int i = 1; i <= n; i++)
if (cir[i]) C++;
}
int cnt, cy;
double ans = 0;
int Start;
void dfs(int now) {
vis[now] = true;
cnt++;
cy += cir[now];
if (cy > 1) {
double a = cnt, b = cnt + C - 2 * cy + 2, c = cnt - cy + C;
if (a != 0 && b != 0 && c != 0) ans += 1.0 / a + 1.0 / b - 1.0 / c;
} else
ans += 1.0 / cnt;
for (int i = st[now]; i != -1; i = e[i].next)
if (!vis[e[i].to]) dfs(e[i].to);
cy -= cir[now];
cnt--;
}
void work() {
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
Start = i;
dfs(i);
}
cout << fixed << setprecision(9) << ans << endl;
}
void debug() {
for (int i = 1; i <= n; i++)
if (cir[i]) cout << i << " ";
}
int main() {
init();
work();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
const int N = 3100;
struct node {
int nex, to;
} a[N << 1];
int n, m, tot, ccol, top, cnt;
int id[N], acc[N], fa[N], lca[N][N], maxid;
int dfn[N], low[N], col[N], vis[N], sta[N], num[N];
int head[N], dep[N];
void add(int f, int t) {
a[++tot].nex = head[f];
a[tot].to = t;
head[f] = tot;
}
void tarjan(int now, int fa) {
dfn[now] = low[now] = ++cnt;
sta[++top] = now;
vis[now] = 1;
for (int i = head[now], to; i; i = a[i].nex) {
to = a[i].to;
if (to == fa) continue;
if (!dfn[to]) {
tarjan(to, now);
low[now] = min(low[now], low[to]);
} else if (vis[i])
low[now] = min(low[now], dfn[to]);
}
if (dfn[now] == low[now]) {
ccol++;
int t;
do {
t = sta[top];
vis[t] = 0;
col[t] = ccol;
num[ccol]++;
} while (sta[top--] != now);
}
}
int find(int x) { return x == fa[x] ? fa[x] : fa[x] = find(fa[x]); }
void Dfs(int now, int fa) {
for (int i = head[now], to; i; i = a[i].nex) {
to = a[i].to;
if (to == fa) continue;
dep[to] = dep[now] + 1;
Dfs(to, now);
}
}
void work(int now, int ac, int f) {
acc[now] = ac;
fa[now] = now;
for (int i = head[now], to; i; i = a[i].nex) {
to = a[i].to;
if (to != f && num[col[to]] == 1) {
dep[to] = dep[now] + 1;
work(to, ac, now);
if (find(to) != find(now)) fa[find(to)] = find(now);
}
}
for (int i = 1; i <= n; i++)
if (acc[i] == ac) lca[now][i] = lca[i][now] = find(i);
}
void dfs(int now, int fa) {
id[now] = id[fa] + 1;
dep[now] = 1;
acc[now] = now;
work(now, now, now);
for (int i = head[now], to; i; i = a[i].nex) {
to = a[i].to;
if (to != fa && !id[to] && num[col[to]] > 1) dfs(to, now);
}
}
int main() {
n = read();
m = n;
for (int i = 1, x, y; i <= m; i++) {
x = read() + 1;
y = read() + 1;
add(x, y);
add(y, x);
}
if (m == n - 1) {
double res = 0;
for (int i = 1; i <= n; i++) {
dep[i] = 1;
Dfs(i, 0);
for (int j = 1; j <= n; j++) res += (double)1 / (double)dep[j];
}
printf("%.5f\n", res);
} else {
double Ans = 0;
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, 0);
for (int i = 1; i <= n; i++)
if (num[col[i]] > 1) {
id[i] = 1;
maxid = num[col[i]];
dfs(i, 0);
break;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (acc[i] == acc[j])
Ans += 1.0 / (dep[i] + dep[j] - 2 * dep[lca[i][j]] + 1);
else {
int x = dep[i] + dep[j], y = abs(id[acc[i]] - id[acc[j]]) - 1,
z = maxid - y - 2;
Ans += 1.0 / (x + y) + 1.0 / (x + z) - 1.0 / (x + y + z);
}
}
printf("%.5f\n", Ans);
}
fclose(stdin);
fclose(stdout);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int to, next;
} e[6060];
int point[3030], te = 1;
void addE(int st, int ed) {
te++;
e[te].to = ed;
e[te].next = point[st];
point[st] = te;
}
int ind[3030];
queue<int> q;
int ter, cir;
int vis[3030], dis[3030], dis2[3030];
double ans;
void dfs(int now) {
vis[now] = 1;
for (int i = point[now]; i; i = e[i].next) {
int who = e[i].to;
if (vis[who]) continue;
dis2[who] = dis2[now] + 1;
if (!dis[who]) {
dis[who] = dis[now] + 1;
ans += 1. / dis[who];
} else {
ans += 1. / dis2[who] - 2. / (dis[who] + dis2[who] + cir - 2);
}
dfs(who);
}
vis[now] = 0;
}
int n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
int u, v;
cin >> u >> v;
addE(u, v);
addE(v, u);
ind[u]++, ind[v]++;
}
for (int i = 0; i < n; i++) {
if (ind[i] == 1) q.push(i);
}
ter = q.size();
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = point[now]; i; i = e[i].next) {
int who = e[i].to;
ind[who]--;
if (ind[who] == 1) q.push(who), ter++;
}
}
cir = n - ter;
for (int i = 0; i < n; i++) {
memset(vis, 0, sizeof(vis));
memset(dis, 0, sizeof(dis));
memset(dis2, 0, sizeof(dis2));
dis[i] = dis2[i] = 1;
dfs(i);
}
cout << fixed << setprecision(10) << ans + n << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read(register T& t) {
register T f = 1;
register char ch = getchar();
t = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') f = -f;
ch = getchar();
}
while (ch >= '0' && ch <= '9') t = t * 10 + ch - '0', ch = getchar();
t *= f;
return t;
}
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
read(t);
read(args...);
}
const long long p = 998244353;
inline long long power(register long long x, register long long k = p - 2) {
register long long re = 1;
for (; k; k >>= 1, x = x * x % p)
if (k & 1) re = re * x % p;
return re;
}
int n;
int head[3005], to[6006], ne[6006], total = 1;
inline void add(int a, int b) {
total++;
to[total] = b;
ne[total] = head[a];
head[a] = total;
}
vector<int> col;
bool vis[3005];
int st[3005], st_top;
int del_e;
void dfs(int now, int fa) {
if (col.size()) return;
st[++st_top] = now;
vis[now] = 1;
for (int i = head[now]; i; i = ne[i])
if (to[i] != fa) {
if (vis[to[i]]) {
while (st_top > 0 && st[st_top] != to[i])
col.push_back(st[st_top]), st_top--;
col.push_back(to[i]);
del_e = i;
return;
}
dfs(to[i], now);
if (col.size()) return;
}
st_top--;
}
int fa[3005], son[3005];
int deep[3005], size[3005];
int top[3005];
void dfs1(int now) {
deep[now] = deep[fa[now]] + 1;
size[now] = 1;
for (int i = head[now]; i; i = ne[i])
if (to[i] != fa[now]) {
if (i == del_e || (i ^ 1) == del_e) continue;
fa[to[i]] = now;
dfs1(to[i]);
size[now] += size[to[i]];
if (size[to[i]] > size[son[now]]) son[now] = to[i];
}
}
void dfs2(int now, int topf) {
top[now] = topf;
if (son[now]) dfs2(son[now], topf);
for (int i = head[now]; i; i = ne[i])
if (to[i] != fa[now] && to[i] != son[now])
if (i != del_e && (i ^ 1) != del_e) dfs2(to[i], to[i]);
}
bool inc[3005];
vector<int> sol[3005];
void dfs3(int now, int fa, int id) {
sol[id].push_back(now);
for (int i = head[now]; i; i = ne[i])
if (to[i] != fa && inc[to[i]] == 0) dfs3(to[i], now, id);
}
double ans = 0;
inline int lca(int x, int y) {
while (top[x] != top[y]) {
if (deep[top[x]] < deep[top[y]]) swap(x, y);
x = fa[top[x]];
}
if (deep[x] < deep[y]) return x;
return y;
}
inline int dis(int x, int y) {
return deep[x] + deep[y] - deep[lca(x, y)] * 2 + 1;
}
int bel[3005];
int disoc[3005], disoc_top;
int main() {
read(n);
for (int i = 1, x, y; i <= n; i++) read(x, y), x++, y++, add(x, y), add(y, x);
dfs(1, 1);
dfs1(1);
dfs2(1, 1);
for (int i : col) inc[i] = 1;
for (int i : col) dfs3(i, i, i);
for (int i : col)
for (int j : sol[i]) bel[j] = i;
for (int i : col) disoc[i] = ++disoc_top;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (bel[i] == bel[j]) {
ans += 1.0 / dis(i, j);
continue;
}
int x = dis(i, bel[i]) + dis(j, bel[j]);
int y = abs(disoc[bel[i]] - disoc[bel[j]]) - 1;
int z = col.size() - y - 2;
ans += 1.0 / (x + y) + 1.0 / (x + z) - 1.0 / (x + y + z);
}
printf("%.10lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x20202020;
const int mod = 1000000007;
template <class T>
inline void read(T& x) {
bool fu = 0;
char c;
for (c = getchar(); c <= 32; c = getchar())
;
if (c == '-') fu = 1, c = getchar();
for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
if (fu) x = -x;
};
template <class T>
inline void read(T& x, T& y) {
read(x);
read(y);
}
template <class T>
inline void read(T& x, T& y, T& z) {
read(x);
read(y);
read(z);
}
template <class T>
inline void read(T& x, T& y, T& z, T& q) {
read(x);
read(y);
read(z);
read(q);
}
const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1};
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long powmod(long long a, long long b, long long mod) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int N = 111111, M = 111111;
int l, m, n, t, C, ctot;
int vis[N], cir[N], pt[N], dis[N], fa[N][22];
vector<int> a[N];
int dfs(int x, int f) {
if (vis[x]) return x;
vis[x] = 1;
int t;
for (int _tmp = ((int)(a[x]).size()) - 1, i = 0; i <= _tmp; ++i)
if (a[x][i] != f) {
if (t = dfs(a[x][i], x)) {
cir[++ctot] = x;
if (x == t) return 0;
return t;
}
} else
f = 0;
return 0;
}
void dfs(int x) {
for (int _tmp = ((int)(a[x]).size()) - 1, i = 0; i <= _tmp; ++i)
if (!pt[a[x][i]]) {
dis[a[x][i]] = dis[x] + 1;
pt[a[x][i]] = pt[x];
fa[a[x][i]][0] = x;
dfs(a[x][i]);
}
}
int lca(int x, int y) {
if (dis[x] > dis[y]) swap(x, y);
for (int _tmp = 0, i = 20; i >= _tmp; --i)
if (fa[y][i] && dis[fa[y][i]] >= dis[x]) y = fa[y][i];
if (x == y) return x;
for (int _tmp = 0, i = 20; i >= _tmp; --i)
if (fa[y][i] != fa[x][i]) y = fa[y][i], x = fa[x][i];
return fa[y][0];
}
int main() {
scanf("%d", &n);
for (int _tmp = n, i = 1; i <= _tmp; ++i)
scanf("%d%d", &l, &t), ++l, ++t, a[l].push_back(t), a[t].push_back(l);
dfs(1, 0);
for (int _tmp = ctot, i = 1; i <= _tmp; ++i) pt[cir[i]] = i;
for (int _tmp = ctot, i = 1; i <= _tmp; ++i) dfs(cir[i]);
for (int _tmp = 20, i = 1; i <= _tmp; ++i)
for (int _tmp = n, j = 1; j <= _tmp; ++j)
fa[j][i] = fa[fa[j][i - 1]][i - 1];
double ans = 0;
for (int _tmp = n, i = 1; i <= _tmp; ++i)
for (int _tmp = n, j = 1; j <= _tmp; ++j)
if (i != j) {
if (pt[i] == pt[j]) {
int p = dis[i] + dis[j] - 2 * dis[lca(i, j)] + 1;
ans += 1.0 / p;
} else {
int p1 = dis[i] + dis[j], p2 = abs(pt[i] - pt[j]),
p3 = ctot - abs(pt[i] - pt[j]);
ans +=
1.0 / (p1 + p2 + 1) + 1.0 / (p1 + p3 + 1) - 1.0 / (p1 + p2 + p3);
}
}
printf("%.9lf\n", ans + n);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class t>
ostream &operator<<(ostream &tout, const vector<t> &s) {
tout << '[';
for (int i = 0; i < s.size(); i++)
if (i + 1 == s.size())
tout << s[i];
else
tout << s[i] << ',';
tout << ']';
return (tout);
}
template <class a, class b>
ostream &operator<<(ostream &tout, const pair<a, b> &c) {
return (tout << '(' << c.first << ',' << c.second << ')');
}
template <class T>
struct __set__print {
__set__print(ostream &out) : tout(out), count(0) {}
void operator()(T x) {
if (count > 0) tout << ',';
tout << x;
++count;
}
ostream &tout;
int count;
};
template <class T>
ostream &operator<<(ostream &tout, const set<T> &s) {
tout << '{';
for_each(s.begin(), s.end(), __set__print<T>(tout));
return (tout << '}');
}
template <class T, class Q>
struct print_map {
print_map(ostream &out) : tout(out), count(0) {}
void operator()(const pair<T, Q> &x) {
if (count > 0) tout << ',';
tout << '(' << x.first << " => " << x.second << ')';
++count;
}
ostream &tout;
int count;
};
template <class T, class Q>
ostream &operator<<(ostream &tout, map<T, Q> s) {
tout << '{';
for_each(s.begin(), s.end(), print_map<T, Q>(tout));
return (tout << '}');
}
template <class T>
string to_string(T s) {
stringstream tin;
tin << s;
string res;
getline(tin, res);
return (res);
}
template <class T>
vector<T> to_vector(T *s, int n) {
vector<T> result;
for (int i = 0; i < n; i++) result.push_back(s[i]);
return (result);
}
const int MAX_N = 3000 + 20;
int n;
vector<int> e[MAX_N];
set<pair<int, int> > cycle;
int dep[MAX_N];
bool mark[MAX_N];
int find_depth(int s, int t) {
dep[s] = t;
int v = 0;
for (int i = 0; i < e[s].size(); i++)
if (dep[e[s][i]]) {
if (dep[e[s][i]] < t - 1) {
v = e[s][i];
cycle.insert(pair<int, int>(s, e[s][i]));
cycle.insert(pair<int, int>(e[s][i], s));
}
} else {
int q = find_depth(e[s][i], t + 1);
if (q) {
cycle.insert(pair<int, int>(s, e[s][i]));
cycle.insert(pair<int, int>(e[s][i], s));
}
if (q && q != s) v = q;
}
return (v);
}
double ans = 0;
double f(pair<int, int> s) {
if (s.second == 0)
return (1.0 / (s.first + 1));
else {
int nw = cycle.size() / 2 - s.second - 1;
int q = s.first + cycle.size() / 2 - s.second * 2;
return (1.0 / (s.first + 1) + 1.0 / (q + 1) - 1.0 / (s.first + nw + 1));
}
}
void dfs(int s, pair<int, int> t) {
mark[s] = true;
ans += f(t);
for (int i = 0; i < e[s].size(); i++)
if (!mark[e[s][i]]) {
pair<int, int> q(t.first + 1, t.second);
if (cycle.find(pair<int, int>(s, e[s][i])) != cycle.end()) q.second++;
dfs(e[s][i], q);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
a++;
b++;
e[a].push_back(b);
e[b].push_back(a);
}
find_depth(1, 3);
ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) mark[j] = false;
dfs(i, pair<int, int>(0, 0));
}
cout << fixed;
cout.precision(12);
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T min(T &a, T &b) {
return a < b ? a : b;
}
template <class T>
inline T max(T &a, T &b) {
return a > b ? a : b;
}
template <class T>
void read(T &x) {
char ch;
while ((ch = getchar()) && !isdigit(ch))
;
x = ch - '0';
while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0';
}
long long Pow(long long a, long long b, long long Mod) {
long long ans = 1;
a %= Mod;
for (; b; b >>= 1) ans = b & 1 ? (ans * a % Mod) : ans, a = a * a % Mod;
return ans;
}
const int N = 3010;
queue<int> Q;
vector<int> E[N];
int n, Tree[N], dis[N], vis[N], rd[N], Ring[N], Rcnt;
double Ans[N][N];
void dfs(int w, int Fa, int id) {
if (Fa) Tree[w] = id;
for (int i = 0; i < E[w].size(); i++)
if (!Ring[E[w][i]] && E[w][i] != Fa) dfs(E[w][i], w, id);
}
void spfa(int S) {
for (int i = 1; i <= n; i++) dis[i] = int(1e8), vis[i] = 0;
dis[S] = 1;
Q.push(S);
while (Q.size()) {
int x = Q.front();
Q.pop();
vis[x] = 0;
for (int i = 0; i < E[x].size(); i++)
if (dis[E[x][i]] > dis[x] + 1) {
dis[E[x][i]] = dis[x] + 1;
if (!vis[E[x][i]]) Q.push(E[x][i]), vis[E[x][i]] = 1;
}
}
}
int Dis_Min(int x, int y) {
if (x > y) swap(x, y);
return min(y - x + 1, Rcnt - y + 1 + x);
}
int Dis_Max(int x, int y) {
if (x > y) swap(x, y);
return max(y - x + 1, Rcnt - y + 1 + x);
}
int main() {
scanf("%d", &n);
int x, y;
for (int i = 1; i <= n; i++)
scanf("%d%d", &x, &y), x++, y++, rd[x]++, rd[y]++, E[x].push_back(y),
E[y].push_back(x);
for (int i = 1; i <= n; i++)
if (rd[i] == 1) Q.push(i);
while (Q.size()) {
int x = Q.front();
Q.pop();
Tree[x] = 1;
for (int i = 0; i < E[x].size(); i++) {
rd[E[x][i]]--;
if (rd[E[x][i]] == 1) Q.push(E[x][i]);
}
}
int R1 = 0, Rn;
for (int i = 1; i <= n; i++)
if (!Tree[i]) {
R1 = i;
Ring[i] = ++Rcnt;
break;
}
Rn = R1;
while (1) {
int flag = 0;
for (int i = 0; i < E[Rn].size(); i++)
if (!Tree[E[Rn][i]] && !Ring[E[Rn][i]]) {
flag = 1;
Ring[E[Rn][i]] = ++Rcnt;
Rn = E[Rn][i];
break;
}
if (!flag) break;
}
for (int i = 1; i <= n; i++)
if (!Tree[i]) dfs(i, 0, i);
double ans = n;
for (int i = 1; i <= n; i++) {
spfa(i);
for (int j = 1; j <= n; j++)
if (j != i) {
double aans = ans;
if ((Tree[i] && Tree[i] == Tree[j]) || (Tree[i] == j) || (Tree[j] == i))
ans += 1. / dis[j];
else {
if (Ring[i] && Ring[j]) {
ans += 1. / Dis_Min(Ring[i], Ring[j]) +
1. / Dis_Max(Ring[i], Ring[j]) - 1. / Rcnt;
} else if (Ring[i]) {
ans += 1. / dis[j];
ans += 1. / (dis[j] - Dis_Min(Ring[i], Ring[Tree[j]]) +
Dis_Max(Ring[i], Ring[Tree[j]]));
ans -= 1. / (dis[j] - Dis_Min(Ring[i], Ring[Tree[j]]) + Rcnt);
} else if (Ring[j]) {
ans += 1. / dis[j];
ans += 1. / (dis[j] - Dis_Min(Ring[j], Ring[Tree[i]]) +
Dis_Max(Ring[j], Ring[Tree[i]]));
ans -= 1. / (dis[j] - Dis_Min(Ring[j], Ring[Tree[i]]) + Rcnt);
} else {
ans += 1. / dis[j];
ans += 1. / (dis[j] - Dis_Min(Ring[Tree[j]], Ring[Tree[i]]) +
Dis_Max(Ring[Tree[j]], Ring[Tree[i]]));
ans -= 1. / (dis[j] - Dis_Min(Ring[Tree[j]], Ring[Tree[i]]) + Rcnt);
}
}
Ans[i][j] = ans - aans;
}
}
printf("%.10lf\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 100;
const int inf = 0x3f3f3f3f;
const int iinf = 1 << 30;
const long long linf = 2e18;
const long long mod = 998244353;
const double eps = 1e-7;
template <class T = int>
T read() {
T f = 1, a = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
a = (a << 3) + (a << 1) + ch - '0';
ch = getchar();
}
return a * f;
}
int n;
vector<int> edge[maxn];
int vis[maxn];
double ans;
int in[maxn], pos[maxn], len;
int dep[maxn], top[maxn];
void find(int now, int step) {
pos[now] = step;
for (int to : edge[now]) {
if (!in[to] || pos[to]) continue;
find(to, step + 1);
}
}
void get_tree(int now, int cnt, int fa = -1) {
dep[now] = fa == -1 ? 1 : dep[fa] + 1, top[now] = cnt;
for (int to : edge[now]) {
if (to == fa || pos[to]) continue;
get_tree(to, cnt, now);
}
}
void init() {
queue<int> q;
len = n;
for (register int i = (0); i <= (n - 1); ++i)
if (in[i] == 1) q.push(i);
while (!q.empty()) {
int now = q.front();
q.pop();
len--;
for (int to : edge[now]) {
if (!in[now]) continue;
in[to]--, in[now]--;
if (in[to] == 1) q.push(to);
}
}
int now = 0;
for (register int i = (0); i <= (n - 1); ++i)
if (in[i]) now = i;
find(now, 1);
for (register int i = (0); i <= (n - 1); ++i)
if (pos[i]) get_tree(i, i);
}
void dfs(int now, int way, int start) {
vis[now] = 1;
if (top[now] == top[start])
ans += 1.0 / way;
else {
int cir = abs(pos[top[now]] - pos[top[start]]);
ans += 1.0 / (dep[now] + dep[start] + cir - 1),
ans += 1.0 / (dep[now] + dep[start] + len - cir - 1);
ans -= 1.0 / (dep[now] + dep[start] + len - 2);
}
for (int to : edge[now])
if (!vis[to]) dfs(to, way + 1, start);
}
void solve(int now) {
for (register int i = (0); i <= (n - 1); ++i) vis[i] = 0;
dfs(now, 1, now);
}
signed main() {
scanf("%d", &n);
for (register int i = (1); i <= (n); ++i) {
int u, v;
scanf("%d %d", &u, &v);
edge[u].push_back(v), edge[v].push_back(u);
in[u]++, in[v]++;
}
init();
for (register int i = (0); i <= (n - 1); ++i) solve(i);
printf("%.114514lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#pragma GCC target("popcnt")
using namespace std;
int stk[3010], n, top = 0;
long double ans = 0;
bool in[3010], loop[3010], found = false;
vector<int> nxt[3010];
vector<pair<int, int> > vec[3010];
template <class T>
void read(T &x) {
char ch = x = 0;
bool fl = false;
while (!isdigit(ch)) fl |= ch == '-', ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
x = fl ? -x : x;
}
void find_loop(int x, int fa) {
if (found) return;
if (in[x]) {
loop[x] = true;
while (stk[top] != x) {
loop[stk[top--]] = true;
}
return found = true, void();
}
in[x] = true, stk[top++] = x;
for (auto &v : nxt[x]) {
if (v != fa) find_loop(v, x);
}
in[x] = false, top--;
}
void dfs(int x, int val, int dep) {
if (loop[x] && !val) val = -dep;
if (!loop[x] && val < 0) val += dep - 2;
in[x] = true;
vec[x].emplace_back(dep, val + (dep - 1) * loop[x]);
for (auto &v : nxt[x]) {
if (!in[v]) dfs(v, val, dep + 1);
}
in[x] = false;
}
int main() {
cin >> n;
for (int i = 1, u, v; i <= n; i++) {
cin >> u >> v, nxt[++u].push_back(++v), nxt[v].push_back(u);
}
find_loop(1, 0);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
vec[j].clear();
}
dfs(i, 0, 1);
for (int j = 1; j <= i; j++) {
if (vec[j].size() == 1) {
ans += 1.0 / vec[j][0].first * (j != i ? 2 : 1);
continue;
}
int a = vec[j][0].second, b = vec[j][1].second,
c = (vec[j][0].first + vec[j][1].first - a - b) / 2;
ans += 2.0 * a / (a + b + c) / (b + c) + 2.0 * b / (a + b + c) / (a + c) +
2.0 / (a + b + c);
}
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
struct edge {
int v;
int next;
edge() {}
edge(int v, int next) : v(v), next(next) {}
} e[6004];
int ind[3004];
bool vzt[3004];
int stk[3004];
int top, newedge;
int circle_len;
bool oncircle[3004];
int d[3];
int path_on_circle;
double ans;
void addedge(int u, int v) {
e[++newedge] = edge(v, ind[u]);
ind[u] = newedge;
}
void findcircle(int u, int fa) {
vzt[u] = true;
stk[++top] = u;
for (int i = ind[u]; i; i = e[i].next) {
if (circle_len) return;
int v = e[i].v;
if (v == fa) continue;
if (vzt[v]) {
while (stk[top + 1] != v) {
oncircle[stk[top--]] = true;
circle_len++;
}
} else
findcircle(v, u);
}
if (top > 0) top--;
}
void dfs(int u, int flag) {
if (flag == 0 && oncircle[u]) flag = 1;
if (flag == 1 && !oncircle[u]) flag = 2;
d[flag]++;
vzt[u] = true;
if (flag == 0 || d[1] == 1)
ans += 1.0 / (d[0] + d[1] + d[2]);
else
ans += 1.0 / (d[0] + d[1] + d[2]) +
1.0 / (d[0] + circle_len + 2 - d[1] + d[2]) -
1.0 / (d[0] + circle_len + d[2]);
for (int i = ind[u]; i; i = e[i].next) {
int v = e[i].v;
if (!vzt[v]) dfs(v, flag);
}
d[flag]--;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int u, v;
scanf("%d%d", &u, &v);
u++, v++;
addedge(u, v);
addedge(v, u);
}
findcircle(1, -1);
for (int i = 1; i <= n; i++) {
memset(vzt, 0, sizeof(vzt));
memset(d, 0, sizeof(d));
dfs(i, 0);
}
printf("%lf", ans);
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.