Yue_Hao @ 2024-08-17 12:26:51
dalao们帮我看一下这个代码,为啥会TLE呀(最后一个样例TLE了)?感谢
#include <bits/stdc++.h>
using namespace std;
bool pd(long long n){
for(long long i = 3; i <= ceil(sqrt(n)); i += 2) if(n % i == 0) return false;
return true;
}
bool reint(long long n){
long long nn = n;
string s = "", c;
while(nn != 0){
c = (nn % 10 + '0');
s.insert(0, c);
nn /= 10;
}
string s1 = s;
reverse(s1.begin(), s1.end());
if(s1 == s){
if( pd(n) ) return true;
else return false;
}else return false;
}
int main(){
long long a, b;
scanf("%lld %lld", &a, &b);
if(a % 2 == 0) a += 1;
for(long long i = a; i <= b; i += 2) if( reint(i) ) printf("%lld\n", i);
return 0;
}
by simple_child @ 2024-08-17 12:58:07
@Yue_Hao 你用C++风格然后加上
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
再把long long改int试试(几乎不可能成功((
by Yue_Hao @ 2024-08-17 13:02:34
@simple_child ,《开幕雷击》,《1也是质数》,《i=a没毛病吧,a=750,它从1开始》
by simple_child @ 2024-08-17 13:04:33
@Yue_Hao 《1也是质数》,笑死了
by Yue_Hao @ 2024-08-17 13:05:17
@simple_child ,没用,还是88(《时间复杂度更复杂了》)
by Yue_Hao @ 2024-08-17 13:06:02
@simple_child ,我试了,一开始上面的对于1的特判等于没写
by Yue_Hao @ 2024-08-17 13:06:22
照样输出1
by simple_child @ 2024-08-17 13:07:12
@Yue_Hao
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
是在使用C++风格且不用文件读入时加速的啊(《时间复杂度更复杂了》)
by simple_child @ 2024-08-17 13:07:41
@Yue_Hao 6
by Yue_Hao @ 2024-08-17 13:13:32
@simple_child ,dalao,我不明白你的意思是啥,你能解释一遍吗?
by simple_child @ 2024-08-17 13:14:29
@Yue_Hao 你知道C++风格和C风格区别吗