TLE求助

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

_colin1112_ @ 2023-05-03 20:27:51

#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
//#pragma GCC optimize(2)
using namespace std;
const int E=1e8+114514;
bool cmp[E]={};
ll n=0,m=0;
ll temp1=0;
int main(){
    scanf("%lld%lld",&n,&m);
    for(int i=2;i*i<=m;i++){
        if(cmp[i]==0){
            for(int j=2*i;j<=m;j+=i){
                cmp[j]=1;
        //      cout <<j<<endl;
            }
        }
    }
    for(int i=n;i<=m;i++){
        if(i==9989900){
            break;
        }
        if(i%2==0 && i!=2){
            continue;
        }
        if(i%3==0 && i!=3){
            continue;
        }
        if(i%5==0 && i!=5){
            continue;
        }
        ll temp=i,sum=0;
        while(temp){
            sum=sum*10+temp%10;
            temp/=10;
        }
        if(sum==i){
            if(cmp[i]==0){
                printf("%d\n",i);
            }
        }
    }
    return 0;
}

能用的优化都用了,还是TLE0.03秒,求大神帮助


by jcls @ 2023-05-03 20:35:10

@colin1112 据说没有除11外的偶次位回文质数


by __er @ 2023-05-03 20:42:29

@colin1112

#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
//#pragma GCC optimize(2)
using namespace std;
const int E=1e8+114514;
bitset<E> cmp;
ll n=0,m=0;
ll temp1=0;
int main(){
    scanf("%lld%lld",&n,&m);
    for(int i=2;i*i<=m;i++){
        if(cmp[i]==0){
            for(int j=2*i;j<=m;j+=i){
                cmp[j]=1;
        //      cout <<j<<endl;
            }
        }
    }
    for(int i=n;i<=m;i++){
        if(i==9989900){
            break;
        }
        if(i%2==0 && i!=2){
            continue;
        }
        if(i%3==0 && i!=3){
            continue;
        }
        if(i%5==0 && i!=5){
            continue;
        }
        ll temp=i,sum=0;
        while(temp){
            sum=sum*10+temp%10;
            temp/=10;
        }
        if(sum==i){
            if(cmp[i]==0){
                printf("%d\n",i);
            }
        }
    }
    return 0;
}

boolbitset,能过


by _colin1112_ @ 2023-05-03 20:45:02

@__er A了,谢谢大佬,已关


|