为什么最后一个测试点总是TLE,还有什么优化方式呢?怎样优化求质数呢?

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

亲测AC ```cpp #include<bits/stdc++.h> using namespace std; bitset<100000001>z; int zs[5000000],a,b,y,o,u; int zx,zd; int find(int l,int r,int k){ int m; while(l+1<r){ m=(l+r)/2; if(zs[m]<=k)l=m; else r=m; } if(k-zs[l]<=zs[r]-k)return l; else return r; } void write(int x){ if(x>9)write(x/10); putchar(x%10+'0'); return; } int read(){ int x=0;char c=getchar(); while(c<'0'||c>'9')c=getchar(); while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x; } int main(){ a=read(),b=read(); zs[1]=2,y=1; for(int i(3);i<=b;i+=2){ if(z[i]==1)continue; y++,zs[y]=i,o=i<<1; for(int j(i+o);j<=b;j+=o)z[j]=1; } zx=find(1,y,a);zd=find(1,y,b); for(int i(zx);i<=zd;i++){ o=zs[i],u=0; while(o>0){ u*=10; u+=o%10; o/=10; } if(zs[i]==u)write(zs[i]),printf("\n"); } return 0; }
by HEROBRINEH @ 2024-03-05 18:16:03


@[HEROBRINEH](/user/1113507) 源代码上有什么建议吗
by Zzz123456789101112 @ 2024-03-05 18:28:30


|