USACO 1.5 Prime Palindromes

时间:2023-11-16 19:16:32

Prime Palindromes

The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b <= 100,000,000); both a and b are considered to be within the range .

PROGRAM NAME: pprime

INPUT FORMAT

Line 1: Two integers, a and b

SAMPLE INPUT (file pprime.in)

5 500

OUTPUT FORMAT

The list of palindromic primes in numerical order, one per line.

SAMPLE OUTPUT (file pprime.out)

5
7
11
101
131
151
181
191
313
353
373
383 ——————————————————————题解
就是枚举回文数然后筛,写了一个logn的筛法(Miller Rabin),结果乘法没开longlong导致wa了一次
枚举素数我手敲了八个循环……后来ac看analysis……只要10-9999然后翻转就行orz我感觉自己真是个辣鸡
而且这位小哥以神一般的数学直觉便判断出任何二倍长的回文数一定是11的倍数,那么我们就不用考虑偶数个的了。小学数学老师你还要我吗要我吗……
这轻描淡写的一句话顶上了我30++行代码orz
那个版权不敢盗,所以不粘别人代码,贴上我易读的代码……以及写的很丑的Miller Rabin(check函数)
 /*
PROB: pprime
LANG: C++
ID: jiaqi si
*/
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <ctime>
#define ivory
#define mo 1000000007
#define siji(i,x,y) for(int i=(x);i<=(y);i++)
#define gongzi(j,x,y) for(int j=(x);j>=(y);j--)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);i++)
#define sigongzi(j,x,y) for(int j=(x);j>(y);j--)
#define pii pair<int,int>
#define fi first
#define se second
#define mo 1000000007
using namespace std;
int ans[],cnt;
int a,b;
int mpow(int c,int d,int k) {
if(d==) {
return c%k;
}
else {
int tmp=mpow(c,d/,k)%k;
if(d&) {
return 1LL*tmp*tmp%k*c%k;
}
else {
return 1LL*tmp*tmp%k;
}
}
}
bool _check(int v,int s,int k) {
if(v==) return true;
siji(i,,s) {
if(1LL*v*v%k==) {
if(v%k==k-){return true;}
else return false;
}
v=1LL*v*v%k;
}
return false;
}
bool check(int k) {
int tmp=k-;
int s=;
while(tmp%(<<s)==) ++s;
--s;
int d=tmp/(<<s);
int tmp1=mpow(,d,k);
int tmp2=mpow(,d,k);
int tmp3=mpow(,d,k);
bool f=;
if(!_check(tmp1,s,k)) {f=;}
else if(k> && (!_check(tmp2,s,k))) {f=;}
else if(k> && (!_check(tmp3,s,k))) {f=;}
return f;
}
int binary(int k){
int l=,r=cnt;
while(l<r) {
int mid=(l+r+)>>;
if(ans[mid]>=k) r=mid-;
else l=mid;
}
return l;
}
int main() {
#ifdef ivory
freopen("pprime.in","r",stdin);
freopen("pprime.out","w",stdout);
#else
//freopen("f1.in","r",stdin);
#endif
ans[++cnt]=;
siji(i,,) {
if(i%!= && check(i)) ans[++cnt]=i;
}
siji(i,,) {
if(i%==)continue;
int tmp=i*+i;
if(check(tmp)) ans[++cnt]=tmp;
}
siji(i,,) {
if(i%==) continue;
siji(j,,) {
int tmp=i*+j*+i;
if(check(tmp)) ans[++cnt]=tmp;
}
}
siji(i,,) {
if(i%==) continue;
siji(j,,) {
int tmp=i*+j*+j*+i;
if(check(tmp)) ans[++cnt]=tmp;
}
}
siji(i,,) {
if(i%==) continue;
siji(j,,) {
siji(k,,) {
int tmp=i*+j*+k*+j*+i;
if(check(tmp)) ans[++cnt]=tmp;
}
}
}
siji(i,,) {
if(i%==) continue;
siji(j,,) {
siji(k,,) {
int tmp=i*+j*+k*+k*+j*+i;
if(check(tmp)) ans[++cnt]=tmp;
}
}
}
siji(i,,) {
if(i%==) continue;
siji(j,,) {
siji(k,,) {
siji(h,,) {
int tmp=i*+j*+k*+h*+k*+j*+i;
if(check(tmp)) ans[++cnt]=tmp;
} }
}
}
siji(i,,) {
if(i%==) continue;
siji(j,,) {
siji(k,,) {
siji(h,,) {
int tmp=i*+j*+k*+h*+h*+k*+j*+i;
if(check(tmp)) ans[++cnt]=tmp;
}
}
}
}
scanf("%d%d",&a,&b);
int il=binary(a);
int ir=binary(b);
if(ans[ir+]==b) ir++;
siji(i,il+,ir) {
printf("%d\n",ans[i]);
}
}