cpp
#include<bits/stdc++.h>
using namespace std;
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;cin>>n;int p;
for(int i=0;i<n;i++){
cin>>p;
if(p<2)cout<<"No\n";
else if(p==2)cout<<"Yes\n";
else{
int flag=0;
for(int j=2;j*j<=p;j++){
if(p%j==0){cout<<"No\n";flag=1;break;}
}
if(flag==0)cout<<"Yes\n";
}
}
return 0;
}
这个代码一直有一个超时。
但是把j*j<=p改成j<=sqrt(q)就过了。
cpp
#include<bits/stdc++.h>
using namespace std;
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;cin>>n;int p;
for(int i=0;i<n;i++){
cin>>p;
if(p<2)cout<<"No\n";
else if(p==2)cout<<"Yes\n";
else{
int flag=0;
for(int j=2;j<=sqrt(p);j++){
if(p%j==0){cout<<"No\n";flag=1;break;}
}
if(flag==0)cout<<"Yes\n";
}
}
return 0;
}

问了下AI,AI告诉我是这个原因:
j*j发生整数溢出
你的代码里:
cpp
int p; for(int j=2; j*j<=p; j++)
如果 p 的范围比较大(例如接近 2e9),
当 j > 46340 时:
cpp
46341 * 46341 > INT_MAX
j*j 会超出 int 范围,产生 有符号整数溢出(未定义行为)。
溢出后:
-
j*j可能变成负数 -
条件
j*j <= p可能一直成立 -
循环不会按 √p 停止
-
可能跑到非常大 → 超时
果然我改成
cpp
for(long long j=2;j*j<=p;j++)
就可以了。
毕竟这道题上界是,很有必要考虑一下整数溢出的问题。