91 lines
2.1 KiB
C++
91 lines
2.1 KiB
C++
#include <cstdio>
|
|
#define N 1010
|
|
int T, M, a, b, c;
|
|
int P[N], C[N], tot;
|
|
void divide(int n) {
|
|
tot = 0;
|
|
for (int i = 2; i * i <= n; i++) {
|
|
if (n % i == 0) {
|
|
P[++tot] = i;
|
|
C[tot] = 0;
|
|
while (n % i == 0) {
|
|
n /= i;
|
|
C[tot]++;
|
|
}
|
|
}
|
|
}
|
|
if (n > 1) {
|
|
P[++tot] = n;
|
|
C[tot] = 1;
|
|
}
|
|
}
|
|
int qpow(int x, int y) {
|
|
int res = 1;
|
|
while (y) {
|
|
if (y & 1) res *= x;
|
|
x *= x;
|
|
y >>= 1;
|
|
}
|
|
return res;
|
|
}
|
|
int gcd(int x, int y) {
|
|
return y ? gcd(y, x % y) : x;
|
|
}
|
|
void print_rational(int x, int y) {
|
|
if (x * y < 0) printf("-");
|
|
if (x < 0) x *= -1;
|
|
if (y < 0) y *= -1;
|
|
if (x % y == 0)
|
|
printf("%d", x / y);
|
|
else
|
|
printf("%d/%d", x / gcd(x, y), y / gcd(x, y));
|
|
}
|
|
int main() {
|
|
scanf("%d %d", &T, &M);
|
|
while (T--) {
|
|
scanf("%d %d %d", &a, &b, &c);
|
|
if (a < 0) {
|
|
a *= -1;
|
|
b *= -1;
|
|
c *= -1;
|
|
}
|
|
int delta = b * b - 4 * a * c;
|
|
if (delta < 0) {
|
|
puts("NO");
|
|
continue;
|
|
}
|
|
// ³ýÈ¥ delta ÖеÄÍêȫƽ?Òò?
|
|
divide(delta);
|
|
int d = 1;
|
|
for (int i = 1; i <= tot; i++) {
|
|
if (C[i] & 1)
|
|
d *= qpow(P[i], (C[i] - 1) >> 1);
|
|
else
|
|
d *= qpow(P[i], C[i] >> 1);
|
|
}
|
|
delta /= d * d;
|
|
if (delta == 0)
|
|
print_rational(-b, a << 1);
|
|
else if (delta == 1) {
|
|
print_rational(d - b, a << 1);
|
|
}else {
|
|
if (b) {
|
|
print_rational(-b, a << 1);
|
|
printf("+");
|
|
}
|
|
int x = d;
|
|
int y = a << 1;
|
|
if (x % y == 0) {
|
|
x /= y;
|
|
if (x > 1) printf("%d*", x);
|
|
printf("sqrt(%d)", delta);
|
|
} else {
|
|
if (x / gcd(x, y) > 1) printf("%d*", x / gcd(x, y));
|
|
printf("sqrt(%d)/%d", delta, y / gcd(x, y));
|
|
}
|
|
}
|
|
puts("");
|
|
}
|
|
return 0;
|
|
}
|