为什么只有10分?
#include <cstdio>
#include <cmath>
#ifdef __GNUC__
#include <algorithm>
#define gcd __gcd
#else
inline int gcd(int a, int b){
return (b == 0 ? a : gcd(b, a % b));
}
#endif
using namespace std;
int sq[5000005];
int P, Q;
void init_sq(int m){
int i, j, k;
const int M = (m<<2) + m; // <= m^2 - 4*m*(-m) = 5 * m^2
const int S = floor(sqrt(M));
sq[1] = 1;
for (i=2; i<=S; i++){
//if (sq[i]) continue;
j = i*i;
for (k=j; k<=M; k+=j)
sq[k] = i;
}
}
void gpq(int c, int d){
if (d == 0) return;
if (c == 0){
P = 0, Q = 1;
return;
}
short s = 1;
if (d < 0) c = -c, d = -d;
if (c < 0) c = -c, s = -1;
int g = gcd(c, d);
int p = c / g, q = d / g;
P = s*p, Q = q;
}
bool opr(int c, int d, bool oz = 1){
gpq(c, d);
if (P == 0 && !oz) return 0;
if (Q == 1) printf("%d", P);
else printf("%d/%d", P, Q);
return 1;
}
inline int delta(int a, int b, int c){
return b*b - ((a*c)<<2);
}
int main()
{
int t, m, a, b, c, d, k;
scanf("%d%d", &t, &m);
init_sq(m*m);
while (t--){
scanf("%d%d%d", &a, &b, &c);
d = delta(a, b, c);
if (d < 0){
puts("NO");
continue;
}
a <<= 1;
if (d == 0){
opr(-b, a);
puts("");
continue;
}
if (sq[d] == 0){
if (opr(-b, a, 0)) putchar('+');
printf("sqrt(%d)/%d\n", d, a);
continue;
}
k = sq[d];
d /= k*k;
if (d == 1){
opr(k-b, a);
puts("");
continue;
}
bool f = opr(-b, a, 0);
gpq(k, a);
if (P != 1){
if (P > 0 && f) putchar('+');
printf("%d*", P);
}
if (P == 1 && f) putchar('+');
printf("sqrt(%d)", d);
if (Q != 1)
printf("/%d", Q);
puts("");
}
return 0;
}