66分,3TLE,为啥超时,跪求大佬帮助

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

XDYZ_N @ 2024-02-21 21:35:09

#include<bits/stdc++.h>
using namespace std;
int qw(int p){
    int q=p,m=0,a=p;
    while(a){
            m = m*10+a%10;
            a /= 10;
    }
    if(q==m) return 1; 
    else return 0;
} 
int as(int m){
    int a=m,w=0;
    for(int i=2;i<=a-1;i++){
        if(a%i==0){
            return 0;
            break;
        }
        else w++;
    }
    if(w == a-2) return 1;
}
int main()
{
    int a,b;
    cin>>a>>b;
    for(int i = a;i<=b;i++)
    {
        if(qw(i) == 1)
        {
            if(as(i) == 1)
            {
                cout<<i<<endl;
            }
        }
    }
    return 0;
}

by XDYZ_N @ 2024-02-22 08:33:20

跪谢大佬


by lunjiahao @ 2024-02-22 09:44:28

@QWQ_HY_DFX

确实当 n10^7 时是会有可能超时,回文素数的数量的较少可以适当减复杂度,算是比较极限的优化


上一页 |