CSP-J T3 求条
  • 板块学术版
  • 楼主BLuemoon_
  • 当前回复12
  • 已保存回复12
  • 发布时间2023/10/23 07:55
  • 上次更新2023/11/2 11:45:45
查看原帖
CSP-J T3 求条
701302
BLuemoon_楼主2023/10/23 07:55

考场上两个样例都对了,估分是100,但是小图灵和洛谷都只有10分,求条代码和求安慰

#include <bits/stdc++.h>

using namespace std;

int t, m, a, b, c;

bool C(int x) {
	int k = sqrt(x);
	for (int i = (k - 1); i <= (k + 2); i++) {
		if (i * i == x) {
			return 1;
		}
	}
	return 0;
}
void P(int x, int y) {
	for (int i = 2; i <= max(x, y); i++) {
		while (x % i == 0 && y % i == 0) {
			x /= i, y /= i;
		}
	}
	if (y == 1) {
		cout << x;
	} else {
		cout << x << '/' << y;
	}
}
void Q(int x) {
	int as1 = 1;
	for (int i = 2; i <= x; i++) {
		while (x % (i * i) == 0) {
			as1 *= i, x /= (i * i);
		}
	}
	if (x == 1) {
		cout << as1;
	} else {
		cout << as1 << "*sqrt(" << x << ')';
	}
}
pair<int, int> R(int x, int y) {
	for (int i = 2; i <= max(x, y); i++) {
		while (x % i == 0 && y % i == 0) {
			x /= i, y /= i;
		}
	}
	if (y < 0) {
		return make_pair(-x, -y);
	} else {
		return make_pair(x, y);
	}
}
pair<int, int> S(int x) {
	int as1 = 1;
	for (int i = 2; i <= x; i++) {
		while (x % (i * i) == 0) {
			as1 *= i, x /= (i * i);
		}
	}
	return make_pair(as1, x);
}

int main() {
	for (cin >> t >> m; t; t--) {
		double ans1, ans2, ans3, ans4, ans5;
		cin >> a >> b >> c;
		if (b == 0 && c == 0) {
			cout << "0\n";
			continue;
		}
		double d = 1.0 * b * b - 4.0 * a * c;
		if (d < 0) {
			cout << "NO\n";
			continue;
		}
		if (d == 0) {
			b *= -1;
			if ((b < 0 && a < 0) || (b > 0 && a > 0)) {
				P(abs(b), abs(2 * a));
			} else {
				cout << "-";
				P(abs(b), abs(2 * a));
			}
			cout << '\n';
			continue;
		}
		double k = a * 2;
		if (C(d)) {
			int q = sqrt(d) + (-1 * b);
			if ((q < 0 && a < 0) || (q > 0 && a > 0)) {
				P(abs(q), abs(2 * a));
			} else {
				cout << "-";
				P(abs(q), abs(2 * a));
			}
		} else {
			if (b == 0) {
				double w = -1.0 * c;
				pair<int, int> tmp = R(w, a);
				int l = tmp.first, r = tmp.second;
				if (l < 0) {
					cout << '-';
				}
				Q(abs(l));
				if (r != 1) {
					cout << '/' << r;
				}
			} else {
				pair<int, int> tmp = S(d);
				int l = tmp.first, r = tmp.second;
				b *= -1;
				if ((b < 0 && a < 0) || (b > 0 && a > 0)) {
					P(abs(b), abs(2 * a));
				} else {
					cout << "-";
					P(abs(b), abs(2 * a));
				}
				cout << '+';
				if (l == 1) {
					cout << "sqrt(" << r << ")/" << a + a;
				} else {
					l = abs(l), a = abs(a);
					int G = __gcd(l, a + a);
					l /= G;
					int u = (a + a) / G;
					if (l == 1) {
						cout << "sqrt(" << r << ")";
						if (u != 1) {
							cout << "/" << u;
						}
					} else {
						cout << l << "*sqrt(" << r << ")";
						if (u != 1) {
							cout << "/" << u;
						}
					}
				}
			}
		}
		cout << '\n';
	}
	return 0;
}
2023/10/23 07:55
加载中...