WA+RE 50 求调
查看原帖
WA+RE 50 求调
1077535
Lcm_simida楼主2024/10/15 21:24
#include<bits/stdc++.h>
using namespace std;
long double t,m,a,b,c,ans;
void write(long double x,long double y){
//	cout<<x;
	while(int(x)!=x){
		x*=10;
		y*=10;
//		if(x<=1000) cout<<x<<" "<<int(x)<<" "<<y<<"\n";
	}
	long long xx=x,yy=y,flag=0;
	if((xx<0&&yy>0)||(xx>0&&yy<0)) flag=1;
	xx=abs(xx),yy=abs(yy);
	for(long long i=2;i<=xx;i++){
		while(xx%i==0&&yy%i==0){
			xx/=i,yy/=i;
		}
	}
	if(flag==1&&xx!=0) cout<<"-";
	cout<<xx;
	if(yy!=1) cout<<"/"<<yy;
}
int main(){
//	freopen("uqe.in","r",stdin);
//	freopen("uqe.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(),cout.tie();
	cin>>t>>m;
	while(t--){
		cin>>a>>b>>c;
//		write(1,4*a);
//		double ans=max((-b+sqrt(b*b-4*a*c))/2/a,(-b-sqrt(b*b-4*a*c))/2/a);
//		cout<<ans<<" "<<(ans*ans*a+ans*b+c==0)<<"\n";
		if(b*b-4*a*c<0) cout<<"NO\n";
		else{
			ans=max((-b+sqrt(b*b-4*a*c))/2/a,(-b-sqrt(b*b-4*a*c))/2/a);
			if(ans*ans*a+ans*b+c==0&&int(ans)==ans){
				if(ans==0) cout<<0;
				else cout<<ans;
				cout<<"\n";
			}
			else{
				long double q1=1,q2=2*a,cnt1=b*b-4*a*c,cnt2=1;
				if(-b!=0){
					write(-b,2*a);
					if(cnt1==0){
						cout<<"\n";
						continue;
					}
					cout<<"+";
				}
				while(int(q1)!=q1||int(q2)!=q2){
					q1*=10,q2*=10;
				}
				while(int(cnt1)!=cnt1){
					cnt1*=10,cnt2*=10;
				}
				long long c1=cnt1,c2=cnt2;
				long long q2_1=q1,q2_2=q2;
				q2_1=abs(q2_1),q2_2=abs(q2_2),c1=abs(c1),c2=abs(c2);
				while(c1%2==0&&c2%2==0) c1/=2,c2/=2;
				while(c1%5==0&&c2%5==0) c1/=5,c2/=5;
				for(int i=2;i<=c1;i++){
					while(c1%(i*i)==0) c1/=(i*i),q2_1*=i;
				}
				for(int i=2;i<=c2;i++){
					while(c2%(i*i)==0) c2/=(i*i),q2_2*=i;
				}
				for(int i=2;i<=q2_1;i++){
					while(q2_1%i==0&&q2_2%i==0) q2_1/=i,q2_2/=i;
				}
//				cout<<q2_1<<" "<<q2_2<<" "<<c1<<" "<<c2<<"\n";
				if(q2_1==q2_2){
					cout<<"sqrt(";
					write(c1,c2);
					cout<<")\n";
				}else if(q2_1%q2_2==0){
					write(q2_1,q2_2);
					cout<<"*sqrt(";
					write(c1,c2);
					cout<<")\n";
				}else if(q2_2%q2_1==0){
					cout<<"sqrt(";
					write(c1,c2);
					cout<<")/"<<q2_2/q2_1<<"\n";
				}else{
					write(q2_1,1);
					cout<<"*sqrt(";
					write(c1,c2);
					cout<<")/";
					write(q2_2,1);
					cout<<"\n";
				}
			}
		}
	}
	return 0;
}
2024/10/15 21:24
加载中...