MLE on #9 奆佬求助

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

Geirangerfjard @ 2022-12-28 15:22:59

#include <iostream>
#include <cstring>
#define int long long

const int N = 100000001;

using namespace std;

bool is_prime[N];
int p[N];
int a,b;

void pri()
{
    memset(is_prime,1,sizeof(is_prime));
    int np=0;
    for (int i=2;i<=b;i++)
    {
        if(is_prime[i]) p[np++]=i;
        for (int j=0;j<np&&i*p[j]<=b;j++)
        {
            is_prime[i*p[j]]=0;
            if(i%p[j]==0) break;
        }
    }
}

bool judge(int x)
{
    int temp=0;
    int k=x;
    while(k)
    {
        temp=temp*10+k%10;
        k/=10;
    }
    return (temp==x);
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin >> a >> b;
    pri();
    for (int i=a;i<=b;i++)
    {
        if(is_prime[i]&&judge(i)) cout << i << endl;
    }
}

by Geirangerfjard @ 2022-12-28 15:23:32

悬赏关注


by Lizj @ 2022-12-28 15:33:31

@Alone_Helpless 额……似乎是 TLE ?


by Kevin_Mamba @ 2022-12-28 15:34:27

@Alone_Helpless

N 太大了,不用筛,逐个判断优化,看题解。


by Geirangerfjard @ 2022-12-28 15:39:23

@Mr_AC 开O2


by Geirangerfjard @ 2022-12-28 15:39:36

@2124Kobe 好的


by Lizj @ 2022-12-28 15:41:00

@Alone_Helpless ……过了


by Geirangerfjard @ 2022-12-28 15:41:43

??? 难道我语言选的不对?


by Lizj @ 2022-12-28 15:42:38

@Alone_Helpless 似乎只用把 long long 改成 int 就能卡过


by Geirangerfjard @ 2022-12-28 15:42:53

我还是不行 @Mr_AC


by Kevin_Mamba @ 2022-12-28 15:42:59

@Alone_Helpless 吸氧。


| 下一页