60pts求调
查看原帖
60pts求调
794701
wo_hen_la楼主2024/10/17 22:46

https://www.luogu.com.cn/record/182819147

先睡觉了,明晚看。

#include <bits/stdc++.h>
using namespace std;

#define sp putchar(' ')
#define en putchar('\n')
#define pb push_back
#define int long long
#define P 998244353
#define HP 1000000000000002097
#define N (int)(2e5+5)

int read(){ char ch=getchar();int x=0,f=1;while(ch>'9' || ch<'0'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return x*f; }
void print(int x){ if(x<0) putchar('-'),x=-x;if(x>9)print(x/10);putchar(x%10+'0'); }

int m;

int gcd(int a,int b){ return b==0?a:gcd(b,a%b); }
int lcm(int a,int b){ return a/gcd(a,b)*b; }
void add(int &p,int &q,int pp,int qq)
{
	int G=lcm(q,qq),c1=G/q,c2=G/qq;
	p*=c1;pp*=c2;
	p+=pp;
	int G2=gcd(p,G);
	p/=G2;q=G/G2;
}
void mul(int &p,int &q,int pp,int qq)
{
	p*=pp;q*=qq;
	int G=gcd(p,q);
	p/=G;q/=G;
}
void solve()
{
	int a,b,c;
	cin>>a>>b>>c;
	int p=-b,q=2*a,pd=b*b-4*a*c;
	if(pd<0) return cout<<"NO\n",void();
	if(sqrt(pd)==(int)sqrt(pd)){
		if(a<0) p-=(int)sqrt(pd);
		else p+=(int)sqrt(pd); 
		mul(p,q,1,1);
		if(p<0 || q<0) cout<<'-';
		if(p){
			if(abs(p)==abs(q)) cout<<"1";
			else cout<<abs(p);
			if(abs(q)!=1) cout<<'/'<<abs(q);
			cout<<"\n";
		}
		else cout<<0<<"\n";
		return;
	}
	int x=-1,y=-1;
	for(int i=sqrt(pd);i>=2;i--){
		if(pd%(i*i)==0){
			x=i,y=pd/i/i;
			break;
		}
	}
	if(x==-1){
		mul(p,q,1,1);
		if(p<0 || q<0) cout<<'-';
		if(p){
			if(abs(p)==abs(q)) cout<<"1";
			else cout<<abs(p);
			if(abs(q)!=1) cout<<'/'<<abs(q);
			if(a<0) cout<<'-';
			else cout<<'+';
		}
		cout<<"sqrt("<<pd<<")/";
		if(a!=0) cout<<2*a; 
		cout<<"\n";
		return;
	}
	mul(p,q,1,1);
	if(p<0 || q<0) cout<<'-';
	if(p){
		if(abs(p)==abs(q)) cout<<1;
		else cout<<abs(p);
		if(abs(q)!=1) cout<<'/'<<abs(q);
		if(a<0) cout<<'-';
		else cout<<'+';
	}
	p=x,q=2*a;
	mul(p,q,1,1);
	if(p!=1) cout<<p<<'*';
	cout<<"sqrt("<<y<<")";
	if(q!=1) cout<<"/"<<q;
	cout<<"\n";
}
signed main()
{
	ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
	int T;
	cin>>T>>m;
	while(T--){
		solve();
	}	
	return 0;
}



2024/10/17 22:46
加载中...