HDU2138 给定N个32位大于等于2的正整数 输出其中素数的个数

用Miller Rabin 素数判定法 效率很高

数学证明比较复杂,略过, 会使用这个接口即可。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cmath>
  6. using namespace std;
  7. typedef long long int LL;
  8. LL getPrime(bool notprime[],LL prime[],LL N)
  9. {
  10. LL tot=0;
  11. for(LL i=2;i<=N;i++)
  12. {
  13. if(!notprime[i]) prime[tot++]=i;
  14. for(LL j=0;j<tot;j++)
  15. {
  16. if(prime[j]*i>N)break;
  17. notprime[prime[j]*i]=true;
  18. if(i%prime[j]==0)break;
  19. }
  20. }
  21. return tot;
  22. }
  23. LL pow_mod(LL n,LL k,LL p)
  24. {
  25. if(k==0)return 1;
  26. LL w=1;
  27. if(k&1)w=n%p;
  28. LL ans=pow_mod(n*n%p,k>>1,p);
  29. return ans*w%p;
  30. }
  31. bool Miller_Rabin(LL n,LL a,LL d)
  32. {
  33. if(n==2)return true;
  34. if(n==a)return true;
  35. if(n%a==0)return false;
  36. if((n&1)==0)return false;
  37. while(!(d&1))d=d>>1;
  38. LL t=pow_mod(a,d,n);
  39. if(t==1)return true;//特别注意!!
  40. while((d!=n-1)&&(t!=1)&&(t!=n-1))
  41. {
  42. t=(LL)t*t%n;
  43. d=d<<1;
  44.  
  45. }
  46.  
  47. return (t==n-1||(d&1)==1);
  48. }
  49. bool isPrime(LL n,LL times)
  50. {
  51. if(n<2)return false;
  52. LL a[4]={2,3,5,7};
  53. for(LL i=0;i<4;i++){if(!Miller_Rabin(n,a[i],n-1))return false;}
  54. return true;
  55. }
  56. int main()
  57. {
  58. LL np;
  59. while(scanf("%lld",&np)==1)
  60. {
  61. LL ans=0,now;
  62. for(LL i=0;i<np;i++)
  63. {
  64. scanf("%lld",&now);
  65. if(isPrime(now,1000000))ans++;
  66. }
  67. printf("%lld\n",ans);
  68. }
  69. return 0;
  70. }

  

HDU2138 素数判定的更多相关文章

  1. FZU 1649 Prime number or not米勒拉宾大素数判定方法。

    C - Prime number or not Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  2. HDOJ2012素数判定

    素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. algorithm@ 大素数判定和大整数质因数分解

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  4. Codevs 1702 素数判定 2(Fermat定理)

    1702 素数判定 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 一个数,他是素数么? 设他为P满足(P< ...

  5. hdu 2012 素数判定 Miller_Rabbin

    素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. 素数判定 AC 杭电

    素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. 杭电ACM 素数判定

    素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  9. 多项式求和,素数判定 HDU2011.2012

    HDU 2011:多项式求和 Description 多项式的描述如下: 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ... 现在请你求出该多项式的前n项的和.   Input ...

随机推荐

  1. 谷歌应用商店chrome扩展程序和APP的发布流程

    互联网上有很多大牛,他们再工作中需要一些难题,再找到解决办法后,如果会使用js的话,大多数人就可以自己动手写一个chrome插件,而且非常容易.开发人员都喜欢与大家分享自己的成就!google是一个全 ...

  2. 利用类装饰器自定制property实现延迟计算

    class LazyProperty: ''' hello,我是非数据描述符(没有定义__set__,不然是大哥数据描述符了--!) ''' def __init__(self, func): pri ...

  3. 数据结构代码实现之队列的链表实现(C/C++)

    上班闲着无聊,一直想着要开始写博客,但又不知道写什么.最近又回顾了下数据结构的知识,那就从数据结构开始吧. 前言 关于C语言结构体的知识以及队列的特性请读者自行了解,此处不做过多解释,嘻嘻. 同时此篇 ...

  4. 集训第四周(高效算法设计)N题 (二分查找优化题)

    原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...

  5. java字符串利用正则表达式分割

    平时用到,整理的,总感觉缺点什么: private String getKeywordByContent(String content) { StringBuffer sbAllKwyword = n ...

  6. poj 3667 Hotel (线段树的合并操作)

    Hotel The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  7. 玛丽卡(codevs 1021)

    题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...

  8. intent使用Serializable传递对象

    package com.pingyijinren.test; import android.content.Intent; import android.support.v7.app.AppCompa ...

  9. UVA 11400_ Lighting System Design

    题意: 给定一系列灯泡的额定功率,电源价钱,一个灯泡的价格以及系统所需该种灯泡的数量.已知流过灯泡的电流相等,所以为省钱可以将电压小的灯泡换成电压大的灯泡,但是不能换成电压更小的灯泡,问最少要花多少钱 ...

  10. mybatis association和collection标签怎么用

    <resultMap type="Bill" id="ResultBill"> <id property="id" col ...