TLE怎么办啊

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

ysxq @ 2023-07-13 17:50:57

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
    int a, sum = 0, i, b, c, j, e;
    scanf("%d %d", &b, &c);
    for (i = b; i <= c; i++){
        a = i;
        while (a != 0){
            sum = sum * 10 + a % 10;
            a /= 10;
        }
        if (sum == i) {
            e = 0;
            for (j = 2; j < i; j++) {
                if (i % j == 0) {
                    e=1;
                    break;
                }
            }
            if (e == 0)
                printf("%d\n", i);
        }
        sum = 0;
    }
    return 0;
}

by onlyfiee @ 2023-07-13 18:46:09

@TiAm0 开启02优化,学习更好的筛法或优化筛法


by onlyfiee @ 2023-07-13 18:47:00

@TiAm0 提示 1: 找出所有的回文数再判断它们是不是质数(素数).


by printf_ @ 2023-07-16 15:23:37


#include <iostream>
#include <stdio.h>
using namespace std;

bool zhi(long long n){
    if(n <= 1) return false;
    for(int i = 2 ; i <= n / i ; i++){
        if(n % i == 0) return false;
    }
    return true;
}

int main(){
    int a , b;
    scanf("%d%d" , &a , &b);
    if(a <= 9)
    for(int i = a ; i <= 9 ; i++){
        if(zhi(i) && a <= i && i <= b) printf("%d\n" , i);
        else if(i > b) return 0;
    }
    if(a <= 99 && b >= 9)
    for(int i = 1 ; i <= 9 ; i++){
        int ans = i + i * 10;
        if(zhi(ans) && a <= ans && ans <= b) printf("%d\n" , ans);
        else if(ans > b) return 0;
    }
    if(a <= 999 && b >= 99)
    for(int i = 1 ; i <= 9 ; i++)
    for(int j = 0 ; j <= 9 ; j++){
        int ans = i + j * 10 + i * 100;
        if(zhi(ans) && a <= ans && ans <= b) printf("%d\n" , ans);
        else if(ans > b) return 0;
    }
    if(a <= 9999 && b >= 999)
    for(int i = 1 ; i <= 9 ; i++)
    for(int j = 0 ; j <= 9 ; j++){
        int ans = i + j * 10 + j * 100 + i + 1000;
        if(zhi(ans) && a <= ans && ans <= b) printf("%d\n" , ans);
        else if(ans > b) return 0;
    }
    if(a <= 99999 && b >= 9999)
    for(int i = 1 ; i <= 9 ; i++)
    for(int j = 0 ; j <= 9 ; j++)
    for(int k = 0 ; k <= 9 ; k++){
        int ans = i + j * 10 + k * 100 + j * 1000 + i * 10000;
        if(zhi(ans) && a <= ans && ans <= b) printf("%d\n" , ans);
        else if(ans > b) return 0;
    }
    if(a <= 999999 && b >= 99999)
    for(int i = 1 ; i <= 9 ; i++)
    for(int j = 0 ; j <= 9 ; j++)
    for(int k = 0 ; k <= 9 ; k++){
        int ans = i + j * 10 + k * 100 + k * 1000 + j * 10000 + i * 100000;
        if(zhi(ans) && a <= ans && ans <= b) printf("%d\n" , ans);
        else if(ans > b) return 0;
    }
    if(a <= 9999999 && b >= 999999)
    for(int i = 1 ; i <= 9 ; i++)
    for(int j = 0 ; j <= 9 ; j++)
    for(int k = 0 ; k <= 9 ; k++)
    for(int l = 0 ; l <= 9 ; l++){
        int ans = i + j * 10 + k * 100 + l * 1000 + k * 10000 + j * 100000 + i * 1000000;
        if(zhi(ans) && a <= ans && ans <= b) printf("%d\n" , ans);
        else if(ans > b) return 0;
    }
    if(a <= 99999999 && b >= 9999999)
    for(long long i = 1 ; i <= 9 ; i++)
    for(long long j = 0 ; j <= 9 ; j++)
    for(long long k = 0 ; k <= 9 ; k++)
    for(long long l = 0 ; l <= 9 ; k++){
        long long ans = i + j * 10 + k * 100 + l * 1000 + l * 10000 + k * 100000 + j * 1000000 + i * 10000000;
        if(zhi(ans) && a <= ans && ans <= b) printf("%lld\n" , ans);
        else if(ans > b) return 0;
    }
    if(a <= 999999999 && b >= 99999999)
    for(long long i = 1 ; i <= 9 ; i++)
    for(long long j = 0 ; j <= 9 ; j++)
    for(long long k = 0 ; k <= 9 ; k++)
    for(long long l = 0 ; l <= 9 ; l++)
    for(long long m = 0 ; m <= 9 ; m++){
        long long ans = i + j * 10 + k * 100 + l * 1000 + m * 10000 + l * 100000 + k * 1000000 + j * 10000000 + i * 100000000;
        if(zhi(ans) && a <= i && i <= b) printf("%lld\n" , ans);
        else if(ans > b) return 0;
    }
    return 0;
}

|