rt,题目链接
代码
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m, a, b, c, p, q, r, qq, sq, gc, delta;
int gcd(int x, int y) {
if (y) return gcd(y, x % y);
return x;
}
int main() {
scanf("%d %d", &n, &m);
while (n--) {
scanf("%d %d %d", &a, &b, &c);
delta = b * b - (a * c << 2);
if (delta < 0) {
puts("NO");
continue;
}
sq = (int)sqrt(delta);
if (sq * sq == delta) {
p = -b;
q = a << 1;
p += sq;
gc = gcd(p, q);
p /= gc;
q /= gc;
if (q < 0) {
p = -p;
q = -q;
}
if (q == 1 && p) printf("%d\n", p);
else if (p) printf("%d/%d\n", p, q);
else puts("0");
continue;
}
p = -b;
q = a << 1;
gc = gcd(p, q);
p /= gc;
q /= gc;
if (q < 0) {
p = -p;
q = -q;
}
if (q == 1 && p) printf("%d+", p);
else if (p) printf("%d/%d+", p, q);
for (int i = 1; i * i <= delta; i++) {
if (delta % (i * i)) continue;
qq = i;
}
r = delta / qq / qq;
q = abs(a << 1);
gc = gcd(qq, q);
qq /= gc;
q /= gc;
if (qq == 1) printf("sqrt(%d)", r);
else printf("%d*sqrt(%d)", qq, r);
if (q == 1) putchar(10);
else printf("/%d\n", q);
}
return 0;
}