11分测评五彩斑斓WA,AC,RE,mle

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

kanqiqin @ 2024-11-17 15:12:19

11分测评五彩斑斓WA,AC,RE,mle

不会呀!!!求线性筛法代码,改正我的代码也行

#include<bits/stdc++.h>
using namespace std;
int n,m,t,prime[100000010],st[100000010];
void getprimes(int n,int m){
    if(n<=1) st[n]=1;
    if(n==1) st[n]=1;
    for(int i=n;i<=m;i++){
        if(st[i]==0) prime[++t]=i;
        for(int j=1;i*prime[j]<=m;j++){
            st[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
bool get(int x){
    int cnt=0,k=x;
    while(k!=0){
        cnt=cnt*10+k%10;
        k/=10;
    }
    if(x==cnt) return 1;
    return 0;
}
int main(){
    cin>>n>>m;
    getprimes(n,m);
    for(int i=1;i<=n;i++){
        if(prime[i]!=0){
            if(get(prime[i])==1) cout<<prime[i]<<endl;
        }
    }
    return 0;
}

by LionBlaze @ 2024-11-17 15:22:05

@kanqiqin 你应当构造回文数。


by kanqiqin @ 2024-11-17 15:27:42

好像有啊???挠头

私信吧


by kakahuang @ 2024-12-22 14:46:03

for (d1 = 1; d1 <= 9; d1+=2) {    
     for (d2 = 0; d2 <= 9; d2++) {
         for (d3 = 0; d3 <= 9; d3++) {
           palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;
         }
     }
 }

by kakahuang @ 2024-12-22 14:46:28

@kanqiqin


by kakahuang @ 2024-12-22 14:46:59

@大佬


|