求助!50ptsWA
查看原帖
求助!50ptsWA
716396
Zero_fallen楼主2023/11/2 13:36

除了有特殊性质C的都没过……一个一个对样例数据对了几百个了都没查出错来,要疯了:)

#include	<bits/stdc++.h>
using namespace std;
int n,sq;
int gcd(int a,int b){
	if(b==0) return a;
	return gcd(b,a%b);
}
void work(int x){
	for(int i=2;i*i<=x;i++){
		int s=i*i;
		while(x%s==0){
			x/=s;
			n*=i;
		}
	}
	sq=x;
}
int main()
{
	//freopen("uqe2.in","r",stdin);
	//freopen("uqe.out","w",stdout);
	int t,m;
	scanf("%d%d",&t,&m);
	while(t--){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		int delta=b*b-4*a*c;
		if(delta<0){
			printf("NO\n");
		}
		else if(delta==0){
			int p=-b,q=2*a;
			if(p*q<0)printf("-");
			q=abs(q),p=abs(p);
			if(q/gcd(p,q)==1) printf("%d\n",p/gcd(p,q));
			else printf("%d/%d\n",p/gcd(p,q),q/gcd(p,q));
		}
		else{
			n=1,sq=1;
			int cnt=delta;
			int q=2*a,p=-b;
			work(cnt);
			if(sq==1){
				
				p+=n;
				int p2=-b-n;
				if(p2/q>p/q){
					p=p2;
				}
				if(p*q<0) printf("-");
				q=abs(q),p=abs(p);
				if(q/gcd(p,q)==1) printf("%d\n",p/gcd(p,q));
				else printf("%d/%d\n",p/gcd(p,q),q/gcd(p,q));
			}
			else{
				
				if(p*q<0) printf("-");
				q=abs(q),p=abs(p);
				int gcd1=gcd(p,q),gcd2=gcd(n,q);
				if(p!=0){
					if(q/gcd1==1) printf("%d+",p/gcd1);
					else printf("%d/%d+",p/gcd1,q/gcd1);
				}
				if(n/gcd2==1) printf("sqrt(%d)",sq);
				else printf("%d*sqrt(%d)",n/gcd2,sq);
				if(q/gcd2==1) printf("\n");
				else printf("/%d\n",q/gcd2);
			}
		}
	}
	return 0;
}
/*
8 14
1 1 4
1 0 0
-1 2 3
-1 -2 4
-2 4 -2
-9 12 14
1 12 8
4 12 2
*/
2023/11/2 13:36
加载中...