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)

  1. 5 500

OUTPUT FORMAT

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

SAMPLE OUTPUT (file pprime.out)

  1. 5
  2. 7
  3. 11
  4. 101
  5. 131
  6. 151
  7. 181
  8. 191
  9. 313
  10. 353
  11. 373
  12. 383
  13.  
  14. ——————————————————————题解
    就是枚举回文数然后筛,写了一个logn的筛法(Miller Rabin),结果乘法没开longlong导致wa了一次
    枚举素数我手敲了八个循环……后来acanalysis……只要10-9999然后翻转就行orz我感觉自己真是个辣鸡
    而且这位小哥以神一般的数学直觉便判断出任何二倍长的回文数一定是11的倍数,那么我们就不用考虑偶数个的了。小学数学老师你还要我吗要我吗……
    这轻描淡写的一句话顶上了我30++行代码orz
    那个版权不敢盗,所以不粘别人代码,贴上我易读的代码……以及写的很丑的Miller Rabincheck函数)
  1. /*
  2. PROB: pprime
  3. LANG: C++
  4. ID: jiaqi si
  5. */
  6. #include <iostream>
  7. #include <string.h>
  8. #include <cstdlib>
  9. #include <cstdio>
  10. #include <algorithm>
  11. #include <cstring>
  12. #include <vector>
  13. #include <ctime>
  14. #define ivory
  15. #define mo 1000000007
  16. #define siji(i,x,y) for(int i=(x);i<=(y);i++)
  17. #define gongzi(j,x,y) for(int j=(x);j>=(y);j--)
  18. #define xiaosiji(i,x,y) for(int i=(x);i<(y);i++)
  19. #define sigongzi(j,x,y) for(int j=(x);j>(y);j--)
  20. #define pii pair<int,int>
  21. #define fi first
  22. #define se second
  23. #define mo 1000000007
  24. using namespace std;
  25. int ans[],cnt;
  26. int a,b;
  27. int mpow(int c,int d,int k) {
  28. if(d==) {
  29. return c%k;
  30. }
  31. else {
  32. int tmp=mpow(c,d/,k)%k;
  33. if(d&) {
  34. return 1LL*tmp*tmp%k*c%k;
  35. }
  36. else {
  37. return 1LL*tmp*tmp%k;
  38. }
  39. }
  40. }
  41. bool _check(int v,int s,int k) {
  42. if(v==) return true;
  43. siji(i,,s) {
  44. if(1LL*v*v%k==) {
  45. if(v%k==k-){return true;}
  46. else return false;
  47. }
  48. v=1LL*v*v%k;
  49. }
  50. return false;
  51. }
  52. bool check(int k) {
  53. int tmp=k-;
  54. int s=;
  55. while(tmp%(<<s)==) ++s;
  56. --s;
  57. int d=tmp/(<<s);
  58. int tmp1=mpow(,d,k);
  59. int tmp2=mpow(,d,k);
  60. int tmp3=mpow(,d,k);
  61. bool f=;
  62. if(!_check(tmp1,s,k)) {f=;}
  63. else if(k> && (!_check(tmp2,s,k))) {f=;}
  64. else if(k> && (!_check(tmp3,s,k))) {f=;}
  65. return f;
  66. }
  67. int binary(int k){
  68. int l=,r=cnt;
  69. while(l<r) {
  70. int mid=(l+r+)>>;
  71. if(ans[mid]>=k) r=mid-;
  72. else l=mid;
  73. }
  74. return l;
  75. }
  76. int main() {
  77. #ifdef ivory
  78. freopen("pprime.in","r",stdin);
  79. freopen("pprime.out","w",stdout);
  80. #else
  81. //freopen("f1.in","r",stdin);
  82. #endif
  83. ans[++cnt]=;
  84. siji(i,,) {
  85. if(i%!= && check(i)) ans[++cnt]=i;
  86. }
  87. siji(i,,) {
  88. if(i%==)continue;
  89. int tmp=i*+i;
  90. if(check(tmp)) ans[++cnt]=tmp;
  91. }
  92. siji(i,,) {
  93. if(i%==) continue;
  94. siji(j,,) {
  95. int tmp=i*+j*+i;
  96. if(check(tmp)) ans[++cnt]=tmp;
  97. }
  98. }
  99. siji(i,,) {
  100. if(i%==) continue;
  101. siji(j,,) {
  102. int tmp=i*+j*+j*+i;
  103. if(check(tmp)) ans[++cnt]=tmp;
  104. }
  105. }
  106. siji(i,,) {
  107. if(i%==) continue;
  108. siji(j,,) {
  109. siji(k,,) {
  110. int tmp=i*+j*+k*+j*+i;
  111. if(check(tmp)) ans[++cnt]=tmp;
  112. }
  113. }
  114. }
  115. siji(i,,) {
  116. if(i%==) continue;
  117. siji(j,,) {
  118. siji(k,,) {
  119. int tmp=i*+j*+k*+k*+j*+i;
  120. if(check(tmp)) ans[++cnt]=tmp;
  121. }
  122. }
  123. }
  124. siji(i,,) {
  125. if(i%==) continue;
  126. siji(j,,) {
  127. siji(k,,) {
  128. siji(h,,) {
  129. int tmp=i*+j*+k*+h*+k*+j*+i;
  130. if(check(tmp)) ans[++cnt]=tmp;
  131. }
  132.  
  133. }
  134. }
  135. }
  136. siji(i,,) {
  137. if(i%==) continue;
  138. siji(j,,) {
  139. siji(k,,) {
  140. siji(h,,) {
  141. int tmp=i*+j*+k*+h*+h*+k*+j*+i;
  142. if(check(tmp)) ans[++cnt]=tmp;
  143. }
  144. }
  145. }
  146. }
  147. scanf("%d%d",&a,&b);
  148. int il=binary(a);
  149. int ir=binary(b);
  150. if(ans[ir+]==b) ir++;
  151. siji(i,il+,ir) {
  152. printf("%d\n",ans[i]);
  153. }
  154. }
  1.  
  1.  

USACO 1.5 Prime Palindromes的更多相关文章

  1. USACO Section1.5 Prime Palindromes 解题报告

    pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  2. P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

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

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

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

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

  5. 4190. Prime Palindromes 一亿以内的质数回文数

    Description The number 151 is a prime palindrome because it is both a prime number and a palindrome ...

  6. <Sicily>Prime Palindromes

    一.题目描述 The number 151 is a prime palindrome because it is both a prime number and a palindrome (it i ...

  7. USACO Prime Palindromes 构造回文数

    这道题目一点也不卡素数的判断 就是朴素的sqrt(n) 也不卡 所以~放心的用吧. 构造回文的时候看了HINT 其中是这么写的: Generate palindromes by combining d ...

  8. 【USACO 1.5】Prime Palindromes

    /* TASK: pprime LANG: C++ SOLVE: 枚举数的长度,dfs出对称的数,判断是否在范围内,是否是素数 原来想着枚举每个范围里的数,但是显然超时,范围最大是10^9. 对称的数 ...

  9. USACO Section 1.5 Prime Palindromes 解题报告

    题目 题目描述 题目就是给定一个区间[a,b]((5 <= a < b <= 100,000,000)),我们需要找到这个区间内所有既是回文串又是素数的数字. 输入样例 5 500 ...

随机推荐

  1. webStorm支持.wxml文件高亮显示

    微信小程序官方说明需要在微信开发者工具中开发运行,但这个工具着实不咋地. 我是使用webstrom编辑,然后在微信开发者工具中热加载查看效果,因为webstrom默认并不支持*.wxml,添加使用xm ...

  2. public static void speckOnWin7(string text),在win7中读文字

    public static void speckOnWin7(string text) {    //洪丰写的,转载请注明 try { string lsSource = ""; ...

  3. http缓存与cdn相关技术

    阅读目录 一 http缓存 二.Http缓存概念解析 三.cdn相关技术 摘要:最近要做这个主题的组内分享,所以准备了一个星期,查了比较多的资料.准备的过程虽然很烦很耗时间,不过因为需要查很多的资料, ...

  4. 分离JavaScript

    分离JavaScript类似于使用style属性,在HTML文档里使用诸如onclick之类的属性也是一种既没有效率又容易引发问题的做法.如果我们用一个"挂钩",就像CSS机制中的 ...

  5. CG 标准函数库

    (1)数学函数 函数 功能描述 abs(x) 返回输入参数的绝对值 acos(x) 反余切函数,输入参数范围为[-1,1], 返回[0,π]区间的角度值 all(x) 如果输入参数均不为0,则返回tu ...

  6. ssh中的 Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

    这个错误我整理了  半天才发现问题的存在. 尝试了网上的很多办法,但是最后都没有达到效果. 包括这两种: 第一种: web.xml种的配置 <filter> <filter-name ...

  7. log4j基本使用方法

    通过配置文件可知,我们需要配置3个方面的内容: 1.根目录(级别和目的地) 2.目的地(控制台和文件等) 3.输出样式 Log4j由三个重要的组件构成: 1.日志信息的优先级 日志信息的优先级从高到低 ...

  8. python第五天

    反射 hasattr,getattr class Foo: static_name = 'nba' def __init__(self): self.name = 'alex' def show(se ...

  9. iOS SDWebImage的使用

    现在把代码贴出来,供大家参考.尤其是新手,看完这篇博客,图片缓存so easy.最后有demo供大家下载,先学习. 第一步,下载SDWebImage,导入工程.github托管地址https://gi ...

  10. java中的反编译

    使用JD-GUI工具  支持mac os 和 windows  地址为:http://jd.benow.ca