88分求助

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

Rosy1 @ 2023-08-26 18:46:08

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,j,s=0,a,b,t;
    cin>>a>>b;
    for(i=a;i<=b;i++)
    {
          t=i;
        while(t!=0)
        {
        s=s*10+t%10;
        t=t/10;
     }
    if(i==s)
    {
        for(j=2;j*j<=i;j++)
        {
            if(i%j==0)
            {
                break;
            }
        }
        if(j*j>i)
        {
            cout<<i<<endl;
        }
    }
    s=0;
    }
    return 0;
}

by liu_le_chen @ 2023-08-26 18:52:39

这道题好像要用埃式筛才能拿满分耶@Rosy1 AC代码如下:

#include<bits/stdc++.h> 
using namespace std;
const int N=10000001;
long long n,m,pis[N],vis[N],cnt;
queue<long long> q;
stack<long long> s;
bool check(){
    while(!q.empty() && !s.empty()){
        if(q.front()!=s.top()){
            return false;
        }
        q.pop();
        s.pop();
    }
    return true;
}
void primes(){
    for(long long i=2;i<=N;i++){
        if(pis[i]==0) vis[++cnt]=i;
        for(long long j=1;j<=cnt && i*vis[j]<=N;j++){
            pis[i*vis[j]]=1;
            if(i%vis[j]==0) break;
        }
    }
}
int main(){
    cin>>n>>m;
    primes();
    for(long long i=1;i<=cnt;i++){
        if(vis[i]>m) break;
        if(vis[i]>=n && vis[i]<=m){
            long long v=vis[i];
            while(v>0){
                q.push(v%10);
                s.push(v%10);
                v/=10;
            }
            if(check()){
                cout<<vis[i]<<endl;
            }
            while(!q.empty()) q.pop();
            while(!s.empty()) s.pop();
        }
    }
    return 0;
}

by Rosy1 @ 2023-08-26 18:55:09

@liulechen 谢谢大佬


|