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;
}

  

最新文章

  1. ADB常用命令(Android Debug Bridge)
  2. ES6,Array.includes()函数的用法
  3. hive:框架理解
  4. Xcode 9.0 新增功能大全
  5. Swift的基础之UILabel控件
  6. 距离度量以及python实现(二)
  7. 基于用户的协同过滤电影推荐user-CF python
  8. 面试题:int和Integer的区别
  9. Dapper/SqlMapper映射对应问题
  10. mac上mysql root密码忘记或权限错误的解决办法
  11. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(四)——Activiti集成
  12. Python 编程核心知识体系-模块|面向对象编程(三)
  13. 通过HTTP协议发送远程消息
  14. unity, 让主角头顶朝向等于地面法线(character align to surface normal)
  15. codeforces 876 C. Classroom Watch
  16. HDFS基本工作机制
  17. thinkphp 读取页面报错 说 /Runtime/Cache/Home/XXXXXX.php 错误
  18. Mspec
  19. 用 python 来操作 docx, xlsx 格式文件(一)(使用 xlsxwriter 库操作xlsx格式文件)
  20. Delphi xe6 android Popup控件的使用

热门文章

  1. Hadoop-2.7.1伪分布--安装配置hbase 1.1.2
  2. 4. GC 算法(实现篇) - GC参考手册
  3. MyBatis 多参问题
  4. 在linux服务器上搭建Struts2项目运行环境
  5. loadrunner-3个难点
  6. hdu 4788
  7. hdu 1251简单字典树
  8. android listVIew实现button按钮监听程序
  9. 洛谷P1186 玛丽卡
  10. 百度UEditor富文本上传图片