c++最后一个咋超时了

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

lt1526679338 @ 2023-04-12 00:00:05

#include<iostream>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    int i,j,t;
    for(i=a;i<=b;i++){
        int s=0;
        t=i;
        while(t!=0){
            s=s*10+t%10;
            t=t/10;
        }
        if(i==s){
            for(j=2;j*j<=i;j++){
                if(i%j==0){
                    break;
                }

            }
            if(j*j>i){
                    cout<<i<<endl;
                }
        }
    }
} 

by SpeedStar @ 2023-04-12 04:22:45

@lt1526679338 数据范围内最大的回文质数是7位数,可以把上限改成1e7


by lt1526679338 @ 2023-04-12 19:27:27

@寒烟冷浅暮殇 可以方便告诉我一下怎么改不


by SpeedStar @ 2023-04-12 22:41:17

@lt1526679338 i≤min(b,1e7)


by lt1526679338 @ 2023-04-15 23:38:46

@寒烟冷浅暮殇 但是用这个好像有问题


by SpeedStar @ 2023-04-15 23:48:11

我的代码

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

using namespace std;

const int N = 1e8 + 10;

bool prime[N];

bool iscyc(int a) {
    int b = 0, c = a;
    while (a) {
        b = b * 10 + a % 10;
        a /= 10;
    }
    return c == b;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int a, b;
    cin >> a >> b;

    for (int i = 2; i <= b; ++i) prime[i] = 1;

    for (int i = 2; i <= b && i <= 10000000; ++i) {
        if (prime[i]) {
            for (int j = i * i; j <= b && j <= 10000000; j += i) {
                prime[j] = 0;
            }
        }
    } 

    for (int i = a; i <= b && i <= 10000000; ++i) {
        if (prime[i] == 1 && iscyc(i)) 
            cout << i << '\n';
    }

    return 0;
}

by SpeedStar @ 2023-04-15 23:48:27

@lt1526679338


|