求助,最后三个没过!

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

ll212518205 @ 2024-07-03 15:49:10

这咋搞啊啊啊


auto isprime(long long  j) {
    long long a = sqrt(j);
    for (long long  i = 2; i <= a; i++)
        if (j % i == 0)
            return 0;
    return 1;
}
auto ishuiwen(long long i) {
    string str = to_string(i);
    int start = 0, end = str.size() - 1;
    while (start < end) {
        if (str[start] != str[end])
            return 0;
        start++;
        end--;
    }
    return 1;
}
int main()
{
    long long a, b;
    cin >> a >> b;
    for (long long i = a; i <= b; i += (i % 2 == 0 ? 1 : 2)) {
        if (isprime(i) && ishuiwen(i)) {
            cout << i << endl;
        }
    }

    return 0;
}```

by syy999 @ 2024-07-05 09:44:06

优化点1:先判断回文 优化点2:增加判断位数的函数或判断b>10000000```

include<bits/stdc++.h>

using namespace std;

define int long long

bool p(int a){ if(a==1) return 0; for(int i=2;i<=sqrt(a);i++) if(a%i==0) return 0; return 1; } bool h(int a){ int f=0,t=a; while(t!=0){ f=f*10+t%10; t=t/10; } if(f==a) return 1; else return 0; } signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int a,b; cin>>a>>b; //不加判断直接循环:88分 (1-8AC,9TLE) if(b>=10000000){ for(int i=a;i<=10000000;i++){ if(p(i)&&h(i)) cout<<i<<endl;//先判断质数:66分 (1-6AC,7-9TLE) } }else{ for(int i=a;i<=b;i++){ if(p(i)&&h(i)) cout<<i<<endl; } } return 0; }


|