1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include <bits/stdc++.h> #define fst first #define snd second #define re register #define int long long #define double long double using namespace std; typedef pair<int,int> pii; const double eps = 1e-5; int T,lim,a,b,c; inline int read(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 1) + (r << 3) + (c ^ 48); c = getchar(); } return r * w; } inline int gcd(int a,int b){ if (!b) return a; return gcd(b,a % b); } inline int qmi(int a,int b){ int res = 1; while (b){ if (b & 1) res *= a; a *= a; b >>= 1; } return res; } inline void print_num(int x,int y,bool f){ if (!x){ if (f) printf("0"); return; } int w = 1; if (x < 0){ x = -x; w *= (-1); } if (y < 0){ y = -y; w *= (-1); } int g = gcd(x,y); x /= g; y /= g; if (!~w) printf("-"); if (y == 1) printf("%lld",x); else printf("%lld/%lld",x,y); } inline pii get_sqrt(int x){ pii res = {1,1}; for (re int i = 2;i * i <= x;i++){ if (x % i == 0){ int cnt = 0; while (x % i == 0){ cnt++; x /= i; } res.fst *= qmi(i,cnt / 2); res.snd *= max(1ll,i * (cnt % 2)); } } if (x != 1) res.snd *= x; return res; } inline pii get_div(int w,bool ok,int x,int y){ if (x < 0){ x = -x; w *= (-1); } if (y < 0){ y = -y; w *= (-1); } int g = gcd(x,y); x /= g; y /= g; if (ok){ if (~w) printf("+"); else printf("-"); } return {x,y}; } inline void solve(){ int w; a = read(); b = read(); c = read(); int del = b * b - 4 * a * c; if (del < 0){ puts("NO"); return; } if (!del){ print_num(-b,2 * a,true); puts(""); return; } double sdel = sqrtl(del); if ((1.0 * (-1.0 * b - sdel) / (2.0 * a)) > (1.0 * (-1.0 * b + sdel) / (2.0 * a))) w = -1; else w = 1; pii t = get_sqrt(del); if (t.snd == 1){ int s = -b + w * t.fst; int m = 2 * a; print_num(s,m,true); } else{ double div = fabs((-b) / (2.0 * a)); bool ok; print_num(-b,2 * a,false); if (div > eps) ok = true; else ok = false; pii res = get_div(w,ok,t.fst,2 * a); if (res.fst != 1) printf("%lld*",res.fst); printf("sqrt(%lld)",t.snd); if (res.snd != 1) printf("/%lld",res.snd); } puts(""); } signed main(){ T = read(); lim = read(); while (T--) solve(); return 0; }
|