QwQ,看了讨论,代码优化了,还是有3个TLE……

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

Max6700 @ 2023-04-28 18:09:09

祭:第6次发讨论

#include<bits/stdc++.h>
using namespace std;

int a,b,sum;

bool zs(int n)
{
    if(n==2)return 1;
    if(n==1)return 0;
    if(n%2==0)return 0;
    for(int i=2;i*i<=n;i++)
    {
        if(n%i==0)return 0;
    }
    return 1;
}

bool hw(int x)
{
    int temp=x,y=0;

    while(x>0)
    {
        y=y*10+x%10;
        x/=10;
    }

    return temp==y;

}

int main(){

    cin>>a>>b;

    for (int i = a; i <= b; i++)
    {
        if(i>=1e7)break;
        if(zs(i) && hw(i))cout<<i<<"\n";
    }

    return 0;
}

by Lovely_CCCyh___ @ 2023-04-28 18:39:18

做法不对,要构造 @Max6700


by Max6700 @ 2023-04-28 18:45:59

@_FoolCYH

哈?不明觉厉……


by Lovely_CCCyh___ @ 2023-04-28 18:47:28

????


by hzoi_Shadow @ 2023-04-28 18:48:58

@Max6700 先判断是否回文


by Max6700 @ 2023-04-28 18:51:26

@The_Shadow_Dragon

啊,谢谢谢谢,换了一下

zs[i]

hw[i]

的位置,AC了力


|