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

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

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

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long int LL;
LL getPrime(bool notprime[],LL prime[],LL N)
{
LL tot=0;
for(LL i=2;i<=N;i++)
{
if(!notprime[i]) prime[tot++]=i;
for(LL j=0;j<tot;j++)
{
if(prime[j]*i>N)break;
notprime[prime[j]*i]=true;
if(i%prime[j]==0)break;
}
}
return tot;
}
LL pow_mod(LL n,LL k,LL p)
{
if(k==0)return 1;
LL w=1;
if(k&1)w=n%p;
LL ans=pow_mod(n*n%p,k>>1,p);
return ans*w%p;
}
bool Miller_Rabin(LL n,LL a,LL d)
{
if(n==2)return true;
if(n==a)return true;
if(n%a==0)return false;
if((n&1)==0)return false;
while(!(d&1))d=d>>1;
LL t=pow_mod(a,d,n);
if(t==1)return true;//特别注意!!
while((d!=n-1)&&(t!=1)&&(t!=n-1))
{
t=(LL)t*t%n;
d=d<<1; } return (t==n-1||(d&1)==1);
}
bool isPrime(LL n,LL times)
{
if(n<2)return false;
LL a[4]={2,3,5,7};
for(LL i=0;i<4;i++){if(!Miller_Rabin(n,a[i],n-1))return false;}
return true;
}
int main()
{
LL np;
while(scanf("%lld",&np)==1)
{
LL ans=0,now;
for(LL i=0;i<np;i++)
{
scanf("%lld",&now);
if(isPrime(now,1000000))ans++;
}
printf("%lld\n",ans);
}
return 0;
}

  

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. python logging 日志使用

    https://docs.python.org/3/library/logging.html1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRI ...

  2. 集训第六周 O题

    Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...

  3. 51nod 1126 求递推序列的第N项 && hdu - 1005 Number Sequence (求周期)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 http://acm.hdu.edu.cn/showproblem ...

  4. hdu - 1254 推箱子 (bfs+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目意思很简单,只要思路对就好. 首先考虑搬运工能否到达推箱子的那个点,这个可以根据箱子前进方向得出搬运工 ...

  5. [bzoj3306]树_dfs序_线段树_倍增lca

    树 bzoj-3306 题目大意:给定一颗n个节点的树,支持换根.修改点权.查询子树最小值. 注释:$1\le n,q\le 10^5$. 想法: 如果没有换根操作,就是$dfs$序+线段树维护区间最 ...

  6. Java:PPT(X)转图片、PDF和SVG

    (一) 简介: 工作中,PowerPoint文档有时需要被转换为PDF/图像文件来存档.因为PDF或图片的页面布局是固定的,很难被修改且能被大多数设备打开,所以PDF或者图片比起PowerPoint格 ...

  7. 文件I/O和标准I/O

    转载:https://blog.csdn.net/kyang_823/article/details/79496561 一.文件I/O和标准I/O文件I/O:文件I/O也称为不带缓冲的I/O(unbu ...

  8. HashSet源码分析1

    import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class SetTest { pu ...

  9. Redis集群方案之使用豌豆荚Codis搭建(待实践)

    Codis的模式类似Twemproxy,不过这东西引入了ZooKeeper做为Redis的注册与发现来实现高可用. 部署时需要额外增加应用的部署,请根据业务需求来衡量. 部署图类似如下: 当然,上面的 ...

  10. JSP的异常处理

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/exception-handling.html: 当写JSP代码的时候,有可能会留下一个编码错误,并且它会 ...