代码(P9750)
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
int main()
{
int T,QwQ;
cin>>T>>QwQ;
while(T--)
{
int a,b,c;
cin>>a>>b>>c;
if(a<0)
{
a=-a;
b=-b;
c=-c;
}
int delta=b*b-4*a*c;
if(delta<0)
{
cout<<"NO"<<endl;
continue;
}
int xon=(b/gcd(2*a,b))*-1;
int xun=(2*a/gcd(2*a,b));
if(xun<0)
{
xun=-xun;
xon=-xon;
}
int k=1;
for(int i=2;i*i<=delta;i++)
{
while(delta%(i*i)==0)
{
k*=i;
delta/=(i*i);
}
}
int yon=k/gcd(k,2*a);
int yun=2*a/gcd(k,2*a);
if(yun<0)
{
yon=-yon;
yun=-yun;
}
// cout<<xon<<" "<<xun<<" "<<yon<<" "<<yun<<" "<<delta<<endl;
if(delta==0)
{
if(xun==1) cout<<xon;
else cout<<xon<<"/"<<xun;
}
else if(delta==1)
{
int ansun=xun*yun;
int anson=yun*xon+yon*xun;
int agcd=gcd(anson,ansun);
ansun/=agcd;
anson/=agcd;
if(ansun==1) cout<<anson;
else cout<<anson<<"/"<<ansun;
}
else
{
bool fg0=true;
bool fg=true;
if(xun==1)
{
if(xon==0)fg0=false;
else cout<<xon;
}
else if(xun!=0) cout<<xon<<"/"<<xun;
if(yon!=1)
{
if(fg0) cout<<"+"<<yon;
else cout<<yon;
fg = false;
}
if(delta!=0)
{
if(fg && fg0)
{
if(delta==1) cout<<"+1";
else cout<<"+sqrt("<<delta<<")";
}
else if(fg && !fg0)
{
if(delta==1) cout<<"1";
else cout<<"sqrt("<<delta<<")";
}
else
{
if(delta!=1)cout<<"*sqrt("<<delta<<")";
}
}
if(yun!=1)cout<<"/"<<yun;
}
cout<<endl;
}
}