代码相同分不同,不懂的我已词穷
2026-10-05 19:31:24
发布于:广东
39阅读
0回复
0点赞
洛谷100tps,这里全部TLE?
#include<bits/stdc++.h>
using namespace std;
void solved(){
int a,b,c; cin>>a>>b>>c;
if(a<0) a=-a,b=-b,c=-c;//a小于零要保证a是正数
int d=b*b-4*a*c,k=1;
if(d<0){
cout<<"NO"<<endl;
return ;
}
for(int i=2;i*i<=d;i++) while(d%(i*i)==0) k*=i,d/=(i*i);//化简
if(d==0||d==1){//说明是有理数
int s=abs(__gcd(2*a,-b+k*d));
cout<<(-b+k*d)/s;//分子
if(2*a/s!=1) cout<<"/"<<2*a/s;//分母
cout<<endl;
return ;
}
int s=abs(__gcd(-b,2*a));
if(-b/s!=0){
cout<<-b/s;
if(2*a/s!=1) cout<<"/"<<2*a/s;
cout<<"+";
}
s=abs(__gcd(k,2*a));
if(k/s!=1) cout<<k/s<<"*";
cout<<"sqrt("<<d<<")";
if(2*a/s!=1) cout<<"/"<<2*a/s;
cout<<endl;
}
int main(){
int t,m; cin>>t>>m; while(t--)
solved();
return 0;
}
全部评论 3
这种情况相信洛谷就行了
昨天 来自 上海
1建议改为'\n',有一次我用endl的1001ms,用'\n'的21ms.
昨天 来自 浙江
1已关
昨天 来自 广东
0但为啥还不过?![我已无奈。。。]
昨天 来自 广东
0也有可能是__gcd()的问题
昨天 来自 浙江
1
有一种可能是endl太费时间了
昨天 来自 浙江
1














有帮助,赞一个