求助!66 分のTLE

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

kjdcountryside @ 2024-07-10 18:17:25

#include<bits/stdc++.h>
#define l long
#define ll long long
#define fl float
#define dou double
#define ld long double
#define st string
#define str struct

using namespace std;

int main(){
    ll a,b;
    ll d = 1;
    scanf("%d",&a);
    scanf("%d",&b);
    for(ll i = a;i <= b;i++){
        stringstream ss;
        ss << i;
        string s,c;
        ss >> s;

        if(s.size() == 2 && a <= 11 && b >= 11){
            if(d == 1){
                cout << 11 << endl;
                d = 0;
            }
            else if(d == 0){
                continue;
            }
        }

        else if(s.size() != 2){
            if(s.size() % 2 == 0){
                continue;
            }
            else{
                for(ll k = s.size() - 1;k >= 0;k--){
                    s[k] = s[k];
                    c += s[k];
                }
                if(s == c){
                    bool flag = 1;
                    for(ll j = 2;j <= sqrt(i);j++){
                        if(i % j == 0){
                            flag = 0;
                            continue;
                        }
                        else
                        continue;
                    }
                    if(flag == 1){
                        printf("%d\n",i);
                        //cout << endl;
                    }
                    else 
                    continue;
                }               
            }
        }
        else{
            continue;
        }
    }
}

本蒟蒻第一次发贴不太会请大佬指教和谅解!


by a18981826590 @ 2024-07-10 18:20:40

@kjdcountryside

#include<bits/stdc++.h>
using namespace std;
int a,b,c,d;
int main(){
    scanf("%d%d",&a,&b);
    if(a<=5&&b>=5) printf("5\n");
    if(a<=7&&b>=7) printf("7\n");
    if(a<=11&&b>=11) printf("11\n");
    for(int i=1;i<=9;i+=2){
        for(int j=0;j<=9;j++){
            c=101*i+10*j;
            if(c<a) continue;
            else if(c>b) return 0;
            d=1;
            for(int k=3;k*k<=c;k++){
                if(c%k==0){
                    d=0;
                    break;
                }
            }
            if(d) printf("%d\n",c);
        }
    }
    for(int i=1;i<=9;i+=2){
        for(int j=0;j<=9;j++){
            for(int k=0;k<=9;k++){
                c=10001*i+1010*j+100*k;
                if(c<a) continue;
                else if(c>b) return 0;
                d=1;
                for(int l=3;l*l<=c;l++){
                    if(c%l==0){
                        d=0;
                        break;
                    }
                }
                if(d) printf("%d\n",c);
            }               
        }
    }
    for(int i=1;i<=9;i+=2){
        for(int j=0;j<=9;j++){
            for(int k=0;k<=9;k++){
                for(int l=0;l<=9;l++){
                    c=1000001*i+100010*j+10100*k+1000*l;
                    if(c<a) continue;
                    else if(c>b) return 0;
                    d=1;
                    for(int m=3;m*m<=c;m++){
                        if(c%m==0){
                            d=0;
                            break;
                        }
                    }
                    if(d) printf("%d\n",c);
                }                   
            }               
        }
    }
    return 0;
}

求关


by kjdcountryside @ 2024-07-11 19:10:40

@a18981826590 感谢(第一次发没想到能得到回复太谢谢了)


by a18981826590 @ 2024-07-13 08:10:03

@kjdcountryside

你这个TLE也不好改了,再优化也要TLE。可以换种方法,像题目说的那样去构造。


by kjdcountryside @ 2024-07-13 13:38:15

@a18981826590 好的,感谢指正


|