可以先找出回文数,再用素数测试来判是否为素数即可。

打回文数时,因为左右对称,可以只枚举后半部,然后通过逆转得到前半部分。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8. const int Max=200000;
  9. int huiwen[Max],answer[Max];
  10. int htop;
  11.  
  12. int anter(int i){
  13. int ans=1;
  14. while(i){
  15. ans*=10;
  16. i--;
  17. }
  18. return ans;
  19. }
  20.  
  21. int gethuiwen(int t,int p){
  22. int ans=0;
  23. while(p>1){
  24. ans*=10;
  25. ans+=(t%10);
  26. p/=10;
  27. t/=10;
  28. }
  29. return ans;
  30. }
  31.  
  32. void for_back(){
  33. htop=0;
  34. for(int i=5;i<=9;i++)
  35. huiwen[htop++]=i;
  36. for(int i=2;i<=8;i++){
  37. int p=anter(i/2);
  38. // cout<<p<<endl;
  39. if(i%2){
  40. for(int k=0;k<=9;k++){
  41. for(int t=0;t<p;t++){
  42. if(t%10==0) continue;
  43. int tail=gethuiwen(t,p);
  44. // cout<<tail<<' '<<t<<endl;
  45. huiwen[htop++]=(tail*10+k)*p+t;
  46. }
  47. }
  48. }
  49. else{
  50. for(int t=0;t<p;t++){
  51. if(t%10==0) continue;
  52. int tail=gethuiwen(t,p);
  53. huiwen[htop++]=(tail)*p+t;
  54. }
  55. }
  56. }
  57. // cout<<huiwen[htop-1]<<endl;
  58. // for(int i=10;i<=50;i++)
  59. // cout<<huiwen[i]<<endl;
  60. }
  61.  
  62. long long random(long long n){
  63. return (long long )((double)rand()/RAND_MAX*n+0.5);
  64. }
  65.  
  66. long long quick(long long a,long long k,long long m){
  67. long long ans=1;
  68. while(k){
  69. if(k&1){
  70. ans=ans*a%m;
  71. }
  72. k>>=1;
  73. a=a*a%m;
  74. }
  75. return ans;
  76. }
  77.  
  78. bool Miller_Rabin(long long k){
  79. for(int i=1;i<=50;i++){
  80. long long a=random(k-2)+1;
  81. if(quick(a,k-1,k)!=1)
  82. return false;
  83. }
  84. return true;
  85. }
  86.  
  87. int main(){
  88. srand(time(0));
  89. for_back();
  90. int tmp=htop; htop=0;
  91. // cout<<huiwen[tmp-1]<<endl;
  92. for(int i=0;i<tmp;i++){
  93. // cout<<"NO"<<endl;
  94. if(Miller_Rabin(huiwen[i])){
  95. // cout<<"YES"<<endl;
  96. // break;
  97. huiwen[htop++]=huiwen[i];
  98. }
  99. }
  100. int a,b,ast;
  101. while(scanf("%d%d",&a,&b)!=EOF){
  102. ast=0;
  103. for(int i=0;i<htop;i++){
  104. if(huiwen[i]>=a&&huiwen[i]<=b)
  105. answer[ast++]=huiwen[i];
  106. }
  107. sort(answer,answer+ast);
  108. for(int i=0;i<ast;i++)
  109. printf("%d\n",answer[i]);
  110. cout<<endl;
  111. }
  112. return 0;
  113. }

  

HDU 1431的更多相关文章

  1. HDU 1431 思维 基础数论

    找范围内回文素数,最大到1e8,我就是要枚举回文串,再判素数,然后因为这种弱智思路死磕了很久题目. /** @Date : 2017-09-08 15:24:43 * @FileName: HDU 1 ...

  2. F题 hdu 1431 素数回文

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1431 素数回文 Time Limit: 2000/1000 MS (Java/Others)    M ...

  3. HDU 1431 素数回文 离线打表

    题目描述:给定一个区间,将这个区间里所有既是素数又是回文数的数输出来. 题目分析:这题的这个数据范围比较大,达到了10^8级别,而且输入的数据有多组,又因为判断一个数是否是回文数貌似只有暴力判断,时间 ...

  4. HDU 1431 素数回文

    有人问我这个问题. 个人感觉暴搜会TLE O(n*sqrt(n)).n=100000000:(推断素数用2~sqrt(n)+1 去除) 还是枚举好了. 枚举 1~10000,把他每一位存下来,回文数已 ...

  5. 题解报告:hdu 1431 素数回文

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1431 Problem Description xiaoou33对既是素数又是回文的数特别感兴趣.比如说 ...

  6. hdu 1431 素数回文(暴力打表,埃托色尼筛法)

    这题开始想时,感觉给的范围5 <= a < b <= 100,000,000太大,开数组肯定爆内存,而且100000000也不敢循环,不超时你打我,反正我是不敢循环. 这题肯定得打表 ...

  7. ACM_回文素数

    回文素数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 131号是一个主回文,因为它是一个素数和一个回文(当向后读时,它是相同的 ...

  8. FZU ICPC 2020 寒假训练 2

    A - 排序 输入一行数字,如果我们把这行数字中的'5'都看成空格,那么就得到一行用空格分割的若 干非负整数(可能有些整数以'0'开头,这些头部的'0'应该被忽略掉,除非这个整数就是由 若干个'0'组 ...

  9. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

随机推荐

  1. luogu3911 最小公倍数之和

    题目大意 给出一些数\(A_1,A_2,\cdots A_n\),求 \[\sum_{i=1}^{n}\sum_{j=1}^{n}\mathrm{lcm}(A_i,A_j)\] \(A_i,A_n\l ...

  2. [POJ 3621] Sightseeing Cows

    [题目链接] http://poj.org/problem?id=3621 [算法] 01分数规划(最优比率环) [代码] #include <algorithm> #include &l ...

  3. SimpleMappingExceptionResolver异常映射

    转自:https://blog.csdn.net/qqqqqq654/article/details/65767701 SimpleMappingException异常映射 当异常发生时,我们可以将它 ...

  4. HTML不熟悉方法总结

    1. onblur   属性在元素失去焦点时触发. 2. onfocus 属性在元素获得焦点时触发. 3.addEventlistener 事件监听 4.focus() 方法用于给予该元素焦点.这样用 ...

  5. postgresql 备份(pg_dump,pg_restore)

    PG提供物理备份和逻辑备份(本篇主要讲逻辑备份)物理备份:WAL热备份逻辑备份:pg_dump,pg_dumpall,恢复时pg_restore 查看帮助命令: pg_dump --help 跟MyS ...

  6. 第一个"hello python!"

    第一个python程序"hello python!" 首先打开我们的编辑器,在安装好python后,直接在windows快捷方式里,输入IDLE,就可以看到我们的python默认自 ...

  7. 在量化金融中15个最流行的Python数据分析库

    Python是当今应用最广泛的编程语言之一,以其效率和代码可读性著称.作为一个科学数据的编程语言,Python介于R和java之间,前者主要集中在数据分析和可视化,而后者主要应用于大型应用.这种灵活性 ...

  8. Android之MVP架构

    MVP(Model View Presenter)模式是由MVC模式发展而来的,在如今的Android程序开发中显得越来越重要.本篇文章简单讨论了MVP模式的思想. 啥是MVP MVP模式的主要思想是 ...

  9. CSS3实现简单的幻灯片

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 装饰模式(Decorator)C++实现

    装饰模式 层层包装,增强功能.这就是装饰模式的要旨!装饰器模式就是基于对象组合的方式,可以很灵活的给对象添加所需要的功能.它把需要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象. 意图: 动 ...