88分求助!1个TLE!

P1217 [USACO1.5] 回文质数 Prime Palindromes

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 Yue_Hao @ 2024-08-17 12:47:44

@simple_child ,试过埃氏筛,0分的教训(悲......)


by simple_child @ 2024-08-17 12:47:45

@Yue_Hao 不知道了,等我有空做看看吧


by simple_child @ 2024-08-17 12:48:43

@Yue_Hao 埃氏筛+回文数判断按理来说能A啊


by Yue_Hao @ 2024-08-17 12:48:47

@simple_child ,我感觉这个样例有点小问题,10201试过了,是质数,也是奇数回文数,怎么就不是回文质数了呢?dalao


by simple_child @ 2024-08-17 12:49:40

@Yue_Hao 10201不是质数吧


by simple_child @ 2024-08-17 12:50:24

@Yue_Hao 101×101=10201


by Yue_Hao @ 2024-08-17 12:51:44

@simple_child ,???,百度AI给我搞不会了,一个说是质数,一个说不是质数???????


by simple_child @ 2024-08-17 12:52:29

@Yue_Hao 101×101=10201)))


by Yue_Hao @ 2024-08-17 12:53:12

@simple_child ,e好吧,我再去优化几下


by Yue_Hao @ 2024-08-17 12:55:32

@simple_child ,优化了,但没完全优化,我把for循环对半砍了,结果还是TLE了一个,最后一个数据下载了,可以达到9988909


上一页 | 下一页