P9750求条
  • 板块灌水区
  • 楼主xxxst12
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/12/19 23:25
  • 上次更新2024/12/20 17:54:48
查看原帖
P9750求条
1392330
xxxst12楼主2024/12/19 23:25
#include <bits/stdc++.h>
using namespace std;
int a, b, c, m;
int n;
int gcd(int a, int b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int f(int n) {
	int maxn=0;
	for (int i = 1; i * i <= n; i++) {
		if (n % (i*i) == 0) {	
			maxn = i*i;
		}
	}
	return sqrt(maxn);
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b >> c;
		int p = b * b - 4 * a * c;
		if (p < 0) cout << "NO" << endl;
		else if (p >= 0) {
			int t1 = abs(-b + sqrt(p));
			int t2 = abs(2 * a);
			int t3 = gcd(t1, t2);
			int t4 = abs(-b - sqrt(p));
			int t5 = gcd(t4, t2);
			double x1 = (-b + sqrt(p)) * 1.0 / 2 / a;
			double x2 = (-b - sqrt(p)) * 1.0 / 2 / a;
			if (int(sqrt(p))*int(sqrt(p)) == p) {
				if (x1 >= x2) {
					if ((-b + sqrt(p)) * 2 * a > 0) {
						if (t2 / t3 != 1) cout << t1 / t3 << "/" << t2 / t3 << endl;
						else cout << t1 / t3 << endl;
					} else if (-b + sqrt(p) == 0) cout << 0 << endl;
					else if ((-b + sqrt(p)) * 2 * a < 0) {
						if (t2 / t3 != 1) cout << "-" << t1 / t3 << "/" << t2 / t3 << endl;
						else cout << "-" << t1 / t3 << endl;
					}
				} else if (x1 < x2) {
					if ((-b - sqrt(p)) * 2 * a > 0) {
						if (t2 / t5 != 1) cout << t4 / t5 << "/" << t2 / t5 << endl;
						else cout << t4 / t5 << endl;
					} else if (-b - sqrt(p) == 0) cout << 0 << endl;
					else if ((-b - sqrt(p)) * 2 * a < 0) {
						if (t2 / t5 != 1) cout << "-" << t4 / t5 << "/" << t2 / t5 << endl;
						else cout << "-" << t4 / t5 << endl;
					}
				}
			} else if (int(sqrt(p))*int(sqrt(p)) != p) {
				int tmp = abs(b);
				int t6 = gcd(tmp, t2);
				int t7 = gcd(f(p), t2);
					if (t2 / t6 != 1) {
						if(-2*a*b>0) cout << tmp / t6 << "/" << t2 / t6 << "+";
						else if(-2*a*b<0) cout <<"-" << tmp / t6 << "/" << t2 / t6 << "+";
						if (f(p) / t7 != 1 && t2 / t7 != 1) {
							cout << f(p) / t7 << "*" << "sqrt(" << p / f(p) / f(p) << ")" << "/" << t2 / t7 << endl;
						} else if (f(p) / t7 != 1 && t2 / t7 == 1) {
							cout << f(p) / t7 << "*" << "sqrt(" << p / f(p) / f(p) << ")" << endl;
						} else if (f(p) / t7 == 1 && t2 / t7 != 1) {
							cout << "sqrt(" << p / f(p) / f(p) << ")" << "/" << t2 / t7 << endl;
						} else if (f(p) / t7 == 1 && t2 / t7 == 1) {
							cout << "sqrt(" << p / f(p) / f(p) << ")" << endl;
						}
					} else {
						if(-2*a*b>0) cout << tmp / t6 << "+";
						if(-2*a*b<0) cout <<"-"<< tmp / t6 << "+";
						if (f(p) / t7 != 1 && t2 / t7 != 1) {
							cout << f(p) / t7 << "*" << "sqrt(" << p / f(p) / f(p) << ")" << "/" << t2 / t7 << endl;
						} else if (f(p) / t7 != 1 && t2 / t7 == 1) {
							cout << f(p) / t7 << "*" << "sqrt(" << p / f(p) / f(p) << ")" << endl;
						} else if (f(p) / t7 == 1 && t2 / t7 != 1) {
							cout << "sqrt(" << p / f(p) / f(p) << ")" << "/" << t2 / t7 << endl;
						} else if (f(p) / t7 == 1 && t2 / t7 == 1) {
							cout << "sqrt(" << p / f(p) / f(p) << ")";
						}
					}
				}
			}
		}
	return 0;
}
/*
  9 1000
  1 -1 0
  -1 -1 -1
  1 -2 1
  1 5 4
  4 4 1
  1 0 -432
  1 -3 1
  2 -4 1
  1 7 1
  4838 20
  1 -19 5
-2 12 15
1 8 7
1 8 -8
-1 -11 0
-1 -7 -7
-2 -5 8
2 -8 -13
-1 -10 11
-1 -15 0
1 -5 18
-2 -9 13
-1 20 17
1 -17 -3
1 0 1
-2 -4 -2
2 -11 10
1 -2 -1
 */
2024/12/19 23:25
加载中...