44分 蒟蒻求助

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

Determined_Love_Act @ 2023-10-19 13:37:13

代码如下

#include<bits/stdc++.h>
using namespace std;
bool hui(int k){     
        int a[10],i=0;     
        while (k>0){
            a[i]=k%10;
            k/=10;
            i++;}
        for(int j=0;j<i;j++){
            if(a[j] != a[i-j-1]){
                return false;   
                }
        }
        return true;     
    }
int n;
int isp[100000000];
int pd(int x){
    isp[0] = isp[1] = 1;
    for(int i = 2;i<=x;i++){
        for(int j = 2*i;j<=x;j+=i){
            isp[j] = 1;
        }
    }
}
int main(){
    int m,n;
    cin>>m>>n;
    pd(n);
    for(int i = m ;i<=n;i++){
        if(hui(i)){
            if(isp[i] == 0)cout<<i<<endl;
        }
    }
    return 0;
}

我知道有很多不严谨,没必要的地方,不喜勿喷

~栓Q(回复者必关)


by zhuozhiyuan @ 2023-10-19 15:35:59

是55分吧


by Determined_Love_Act @ 2023-10-21 10:30:23

@zhuozhiyuan 我测出来是44


by craftmine @ 2023-10-22 09:29:08

数据范围1亿,要用线性筛才能100(如果TLE再交几遍,总有AC(欧皇一遍过))

#include<bits/stdc++.h>
using namespace std;
bool v[100000001];
int prime[50000001],len;
bool palindrome(int x){
    int c=0,s=x;
    while(x>0)
        c=c*10+x%10,x/=10;
    return c==s;
}
int qread(){
    int ans=0,negative=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-'){
            negative=-1;
        }
        c=getchar();
    }
    while('0'<=c&&c<='9'){
        ans=ans*10+(c-'0');
        c=getchar();
    }
    return ans*negative;
}
void qwrite(int num){
    if(num<0){
        putchar('-');
        num=-num;
    }
    if(num>9){
        qwrite(num/10);
    }
    putchar(num%10+'0');
}
void fill_c(int n){
    for(int i=2;i<=n;i++){
        if(v[i]==false){
            prime[++len]=i;
        }
        for(int j=1;j<=len&&i*prime[j]<=n;j++){
            v[i*prime[j]]=true;
            if(i%prime[j]==0){
                break;
            }
        }
    }
}
int main(){
    int a=qread(),b=qread();
    fill_c(b);
    for(int i=1;i<=len;i++){
        if(prime[i]>b){
            break;
        }
        if(palindrome(prime[i])&&prime[i]>=a){
            qwrite(prime[i]);
            putchar('\n');
        }
    }
    return 0;
}

include<bits/stdc++.h>

using namespace std; bool v[100000001]; int prime[50000001],len; bool palindrome(int x){ int c=0,s=x; while(x>0) c=c10+x%10,x/=10; return c==s; } int qread(){ int ans=0,negative=1; char c=getchar(); while(c<'0'||c>'9'){ if(c=='-'){ negative=-1; } c=getchar(); } while('0'<=c&&c<='9'){ ans=ans10+(c-'0'); c=getchar(); } return ansnegative; } void qwrite(int num){ if(num<0){ putchar('-'); num=-num; } if(num>9){ qwrite(num/10); } putchar(num%10+'0'); } void fill_c(int n){ for(int i=2;i<=n;i++){ if(v[i]==false){ prime[++len]=i; } for(int j=1;j<=len&&iprime[j]<=n;j++){ v[i*prime[j]]=true; if(i%prime[j]==0){ break; } } } } int main(){ int a=qread(),b=qread(); fill_c(b); for(int i=1;i<=len;i++){ if(prime[i]>b){ break; } if(palindrome(prime[i])&&prime[i]>=a){ qwrite(prime[i]); putchar('\n'); } } return 0; }


by craftmine @ 2023-10-22 09:30:29

(后面那些不用管,打错了)


by Determined_Love_Act @ 2023-10-28 17:37:49

@FUchenhao123 谢谢大佬,这周有点事所以没有及时回复,感谢感谢


|