TLE 66,救

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

mengleo @ 2023-04-03 20:25:15

#include <bits/stdc++.h>
using namespace std;
int n, m;
int wei(int x)
{
    if ((1000 <= x && x <= 9999) || (100000 <= x && x <= 999999))
    {
        return 0;
    }
    return 1;
}
int zhi(int x)
{
    int a[20], flag = 1;
    while (x > 0)
    {
        a[flag] = x % 10;
        x /= 10;
        flag++;
    }
    for (int i = 1; i <= flag / 2; i++)
    {
        if (a[i] != a[flag - i])
        {
            return 0;
        }
    }
    return 1;
}
int hui(int x)
{
    if (x == 2)
    {
        return 1;
    }
    for (int i = 2; i <= sqrt(x); i++)
    {
        if (x % i == 0)
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
    cin >> n >> m;
    if (n == 2)
    {
        printf("2\n");
    }
    if (n % 2 == 0)
    {
        n++;
    }
    m = min(9999999, m);
    for (int i = n; i <= m; i += 2)
    {
        if (!wei(i) || !hui(i) || !zhi(i))
        {
            continue;
        }
        printf("%d\n", i);
    }

    return 0;
}

by ZZWX_luogu @ 2023-04-03 20:42:41

你可以每一次只枚举从第1到(n+1)/2位的数字,这虽然可能会有点累,但是顶多也就处理199998次,可以直接过!


by mengleo @ 2023-04-06 20:36:20

@ZZWX_luogu 谢谢,此帖结


|