J组 T3抱灵求救
查看原帖
J组 T3抱灵求救
352913
123xiayicheng楼主2023/10/22 14:21
#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=(l);i<(r);i++)
#define int long long
using namespace std;
int gcd(int a, int b)
{
	a = abs(a), b = abs(b);
	if (a < b)
		swap(a, b);
	if (!b)
		return a;
	return gcd(b, a % b);
}
bool pd(int a)
{
	int d = sqrt(a);
	return d * d == a;
}
bool pd2(int a, int b)
{
	int d = gcd(a, b);
	return d == b or b == 1;
}
void yf(int &ansx, int &ansy)
{
	int g = gcd(ansx, ansy);
	ansx /= g, ansy /= g;
}
struct node
{
	int x, y;
	node operator +(node t)
	{
		node ans;
		ans.x = x * t.y + y * t.x;
		ans.y = y * t.y;
		yf(ans.x, ans.y);
		return ans;
	}
	node operator /(node t)
	{
		node ans;
		ans.x = x * t.y;
		ans.y = y * t.x;
		yf(ans.x, ans.y);
		return ans;
	}
};
void yf(node &ans)
{
	int g = gcd(ans.x, ans.y);
	ans.x /= g, ans.y /= g;
	if (ans.y < 0)
		ans.y = -ans.y, ans.x = -ans.x;
}
int yf(int &x)
{
	int i = 2, c = x, ans = 1;
	while (c != 1)
	{
//		cout<<c<<" "<<i<<endl;
		if (c % i == 0)
		{
			int cnt = 0;
			while (c % i == 0) c /= i, cnt++;
			ans *= pow(i, cnt >> 1);
		}
		i++;
	}
	x /= ans * ans;
	return ans;
}
signed main()
{
	int T, M;
//	freopen("uqe.in","r",stdin);
//	freopen("uqe.out","w",stdout);
//	node a={1,2},b={1,3};
//	node c=a+b,d=a/b;
//	cout<<c.x<<"/"<<c.y<<" "<<d.x<<"/"<<d.y;
//	int x=8;
//	int tmp=yf(x);
//	cout<<tmp<<" "<<x;
	cin >> T >> M;
	while (T--)
	{
		node a, b, c;
		scanf("%lld%lld%lld", &a.x, &b.x, &c.x);
		a.y = b.y = c.y = 1;
		if (b.x * b.x < 4 * a.x * c.x)
		{
			printf("NO\n");
			continue;
		}
		int d = b.x * b.x - 4 * a.x * c.x;
		if (pd(d))  //d为平方数
		{
			int q1 = sqrt(d) - b.x;
//			cout<<q1<<" "<<d;
			if (pd2(q1, 2 * a.x)) //整数
				printf("%lld\n", q1 / 2 / a.x);
			else
			{
				int ansx = q1, ansy = a.x * 2;
//				cout<<ansx<<" "<<ansy;
				yf(ansx, ansy);
				printf("%lld/%lld\n", ansx, ansy);
			}
		}
		else
		{
			node q = {-b.x, 2 * a.x};
			yf(q);
			if (q.x != 0)
			{
				if (q.y == 1)
					printf("%lld+", q.x);
				else
					printf("%lld/%lld+", q.x, q.y);
			}
			int t = yf(d), q2 = a.x * 2;
			yf(t, q2);
			if (t != 1)
				printf("%lld*", t);
			printf("sqrt(%lld)", d);
			if (q2 != 1)
				printf("/%lld", q2);
			printf("\n");
		}
	}
	return 0;
}
//
2023/10/22 14:21
加载中...