哪位大佬告诉我哪里错了(比赛时写的,得分非常惨)
#include<bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> mp;
int gcd(int a, int b) {
if (mp.count({a, b}))
return mp[{a, b}];
if (a == 0)return b;
else return mp[{a, b}] = gcd(b % a, a);
}
int lcm(int a, int b) {
return a * b / gcd(a, b);
}
pair<int, int> fun(int x) {
int s = 1;
for (int i = 2; i * i <= x; i++)
if (x % (i * i) == 0) {
x /= (i * i);
s *= i;
i--;
}
return make_pair(s, x);
}
struct Faction {
int a, b;
Faction(int _A = 1, int _B = 1): a(_A), b(_B) {this -> cut();}
Faction operator + (const Faction &x) const {
Faction c;
int l = lcm(b, x.b);
int t1, t2;
t1 = a * (l / b);
t2 = x.a * (l / x.b);
c = Faction(t1 + t2, l);
c.cut();
return c;
}
Faction operator - (const Faction &x) const {
Faction c;
int l = lcm(b, x.b);
int t1, t2;
t1 = a * (l / b);
t2 = x.a * (l / x.b);
c = Faction(t1 - t2, l);
c.cut();
return c;
}
void operator = (const Faction &x) {
a = x.a, b = x.b;
return ;
}
void cut() {
if (b < 0 && a < 0)a = -a, b = -b;
if (b < 0 && a > 0)a = -a, b = -b;
if (a == 0)b = 1;
int t = gcd(a, b);
a /= t;
b /= t;
}
void output() {
this -> cut();
if (a == 0)printf("0");
else if (b == 1)printf("%d", a);
else if (b == -1)printf("%d", -a);
else if (a > 0 && b < 0)printf("%d/%d", -a, -b);
else printf("%d/%d", a, b);
}
void output_r() {
this -> cut();
swap(a, b);
if (a == 0)printf("0");
else if (b == 1)printf("%d", a);
else if (b == -1)printf("%d", -a);
else if (a > 0 && b < 0)printf("%d/%d", -a, -b);
else printf("%d/%d", a, b);
}
};
int t, m;
int check(int x) {
int sq = sqrt(x + 0.001d);
if (sq * sq == x)return sq;
else return -1;
}
void print_sqrt(int x) {
printf("sqrt(%d)", x);
return ;
}
int main() {
freopen("uqe.in", "r", stdin);
freopen("uqe.out", "w", stdout);
scanf("%d%d", &t, &m);
int a, b, c;
while (t--) {
scanf("%d%d%d", &a, &b, &c);
//Delta
int t1 = b * b, t2 = 4 * a * c;
if (t1 < t2) {
printf("NO\n");
continue;
}
if (t1 == t2) {
Faction ans(-b, 2 * a);
ans.output();
printf("\n");
continue;
}
int delta = t1 - t2;
int temp = check(delta);
if (temp != -1) {
int x = -b, y = temp;
Faction s(x + y, 2 * a);
s.output();
printf("\n");
continue;
}
Faction Tmp;
Tmp = Faction(-b, 2 * a);
Tmp.cut();
bool flag = false;
if (Tmp.a == 0)flag = true;
else {
Tmp.output();
}
pair<int, int> tmp = fun(delta);
int &x = tmp.first, &y = tmp.second;
if (x % 2) {
if (x % a == 0) {
x /= a;
if (x == 1) {
if (b != 0)printf("+");
print_sqrt(y);
} else {
if (x > 0)
if (b != 0)printf("+%d*", abs(x));
else printf("%d*", abs(x));
else printf("-%d*", abs(x));
print_sqrt(y);
}
printf("/%d\n", 2);
continue;
}
int tmp = gcd(a, x);
if (tmp > 1) {
x /= tmp;
a /= tmp;
if (x == 1) {
if (b != 0)printf("+");
print_sqrt(y);
} else {
if (x > 0)
if (b != 0)printf("+%d*", abs(x));
else printf("%d*", abs(x));
else printf("-%d*", abs(x));
print_sqrt(y);
}
printf("%d\n", 2 * a);
} else {
if (x == 1) {
if (b != 0)printf("+");
print_sqrt(y);
} else {
if (x > 0)
if (b != 0)printf("+%d*", abs(x));
else printf("%d*", abs(x));
else printf("-%d*", abs(x));
print_sqrt(y);
}
printf("%d\n", 2 * a);
}
} else {
x /= 2;
if (x % a == 0) {
x /= a;
if (x == 1) {
if (b != 0)printf("+");
print_sqrt(y);
} else {
if (x > 0)
if (b != 0)printf("+%d*", abs(x));
else printf("%d*", abs(x));
else printf("-%d*", -x);
print_sqrt(y);
}
printf("\n");
continue;
}
int tmp = gcd(a, x);
if (tmp > 1) {
x /= tmp;
a /= tmp;
if (x == 1) {
if (b != 0)printf("+");
print_sqrt(y);
} else {
if (x > 0)
if (b != 0)printf("+%d*", abs(x));
else printf("%d*", abs(x));
else printf("-%d*", -x);
print_sqrt(y);
}
printf("/%d\n", a);
}
else {
if (x == 1) {
if (b != 0)printf("+");
print_sqrt(y);
} else {
if (x > 0)
if (b != 0)printf("+%d*", abs(x));
else printf("%d*", abs(x));
else printf("-%d*", -x);
print_sqrt(y);
}
printf("/%d\n", a);
}
}
}
return 0;
}