#10TLE,求助大佬

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

u_s_er_na_me @ 2023-08-23 19:11:33


#include <iostream>
using namespace std;
int reverse(int j)
{
    int sum = 0;
    while (j != 0)
    {
        sum = sum * 10 + j % 10;
        j /= 10;
    }
    return sum;
}
bool check(int a)
{
    for (int i = 2;i * i <= a;i++)
    {
        if (a % i == 0) return 0;
    }
    return 1;
}
int main()
{
    int a,b;
    cin >> a >> b;
    for (int i = a;i <= b;i++)
    {
        if (i == reverse(i) && check(i))
        cout << i << endl;
    }
}

by xiaoyang111 @ 2023-08-23 19:24:15

10测试点的数据过于强大,每一次埃式筛肯定会T的


by u_s_er_na_me @ 2023-08-23 19:25:20

@xiaoyang111 大佬,那我该怎么办


by heyx0201 @ 2023-08-23 19:26:52

@u_s_er_na_me 不能暴力质数判断,要写质数筛,或者构造出合法的回文数之后判断质数


by xiaoyang111 @ 2023-08-23 19:27:01

首先我们可以只枚举奇数,因为偶数(除了 2)都不是质数,可以排除。

然后暴力跑一遍,看哪些大部分地方没有回文质数,可以判断一下。

然后开个O2。

当然最好的办法是打表和构造回文数判断以及欧拉筛


by xiaoyang111 @ 2023-08-23 19:28:24

给个暴力枚举代码,还没写欧拉筛和构造

记录,已开启代码公开


by u_s_er_na_me @ 2023-08-23 19:34:11

@xiaoyang111 已关注,但我还是TLE


by xiaoyang111 @ 2023-08-23 19:35:03

如果你非要想暴力的话只能开O2过


by u_s_er_na_me @ 2023-08-23 19:41:33

@xiaoyang111 谢谢大佬


by Igallta @ 2023-08-23 20:17:09

@u_s_er_na_me

我尽力了

关注我QwQ


by Igallta @ 2023-08-23 20:19:52

@u_s_er_na_me

过了!!!


| 下一页