33分,求助

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

lmc_qbfz @ 2024-12-25 19:09:15

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

by ___xuzhimo___ @ 2024-12-25 19:15:07

@lmc_qbfz 要用时间复杂度更小的欧拉筛法


by EXR_FAL @ 2024-12-25 19:22:04

@lmc_qbfz 这题纯暴力会T飞起,要搞很多优化(如第一篇题解),建议看看题解,直接构造会比纯暴力简单一些qwq


by lmc_qbfz @ 2024-12-25 20:00:50

@xuzhimo欧拉筛法是啥?


by Dx__ @ 2024-12-25 20:10:56

@EXR_FAL tql%%%
@lmc_qbfz 看看这个link


by lmc_qbfz @ 2024-12-25 20:17:32

@Dx__谢谢


by wangqiyu_why @ 2024-12-27 13:40:38

#include<bits/stdc++.h>
using namespace std;
int zh(int i){
    for(int j=2;j*j<=i;j++){
        if(i%j==0){
            return 0;
        }
    }
    return 1;
}
int h(int i){
    int a=i,b=0;
    while(i!=0){
        b=b*10+i%10;
        i/=10;
    }
    if(a==b){
        return 1;
    }
    return 0;
}
int main(){
    int a,b=0,c=1,n,m;
    cin>>n>>m;
    if(n%2==0)n++;
    for(int i=n;i<=m;i+=2){
        if(h(i))
        {
            if(zh(i))
            {
                printf("%d\n",i);
            }
        }

    }
    return 0;
}

@lmc_qbfz


by wangqiyu_why @ 2024-12-27 13:40:54

@lmc_qbfz


by lmc_qbfz @ 2024-12-30 18:50:54

@wangqiyu_why 谢谢


|