大佬求助!66,7 8 9tle了可以怎么修改呢

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

ShiyoX @ 2023-09-11 12:37:27


#include<iostream>
#include<vector>
using namespace std;
bool ip(int n) {
    if (n <= 3) return n > 1;
    for (int d = 2; d * d <= n; ++d)
        if (n % d == 0) return 0;
    return 1;
}
bool hw(int b)
{
    vector<int>arr;
    while (b > 0)
    {

        int temp = b;
        arr.push_back ( temp % 10);
        b /= 10;
    }
    int ans = arr.size();
    for (int i = 0; i < ans/2; i++)
    {
        if (arr[i] != arr[ans - i - 1])
            return false;
    }
    return true;
}
int main()
{
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; i++)
    {
        bool n = (ip(i) && hw(i));
        if (n)cout << i << endl;
    }

return 0;
}

by small_john @ 2023-09-11 12:42:37

@ShiyoX 判断质数 T 了,建议使用 Miller_Rabin。


by China_cc @ 2023-09-11 13:32:13

看题目下面的提示,需要先把所有的回文数列出来,再判断素数


|