Description

  Give you a lot of positive integers, just to find out how many prime numbers there are.

Input

  There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.

Output

  For each case, print the number of prime numbers you have found out.

Sample Input

3
2 3 4

Sample Output

2

【题目简述】输入一个n和n个int32整数,询问其中有多少个质数,有多组数据

【题解】

有的时候我们需要快速判断一个数是不是质数

这时候我们需要使用miller-rabin算法

首先,根据费马小定理

我们认识到若p是质数

则a^p=a(mod p)

于是我们使用一个推广的结论

“记n=a*2^b,在[1,n)中随机选取一个整数x,如果x^a ≡1或x^(a*2^i) ≡-1(其中0<=i<b),那么我们认为n是质数。”——ysy

如果这样判断,我们会发现有1/4的概率出错

我们多判断几次即可

除非你是宇宙无敌非洲人

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
#define il inline
#define re register
using namespace std;
typedef long long ll;
int T;
ll n,ans=,a,b;
il int ran(){
return rand()*rand()+rand();
}
il ll pow(ll base,ll pow){
ll ans=;
for(;pow;pow>>=){
if(pow&) ans=ans*base%n;
base=base*base%n;
}
return ans;
}
il bool chk(){
ll x=ran()%(n-)+,now=pow(x,a);
if(now==) return true;
for(int i=;i<b;i++){
if(now==n-) return true;
now=now*now%n;
}
return false;
}
il bool isprime(){
a=n-;b=;
while(a%==){
a/=;b++;
}
for(int i=;i<=;i++)
if(!chk()) return false;
return true;
}
il void init(){
srand(T);ans=;
for(int i=;i<=T;i++){
cin>>n;
ans+=isprime();
}
cout<<ans<<endl;
}
int main(){
while(scanf("%d",&T)!=EOF){
init();
}
return ;
}

hdu2138 Miller_Rabin的更多相关文章

  1. HDU2138 素数判定

    HDU2138 给定N个32位大于等于2的正整数 输出其中素数的个数 用Miller Rabin 素数判定法 效率很高 数学证明比较复杂,略过, 会使用这个接口即可. #include<iost ...

  2. hdu2138 How many prime numbers 米勒测试

    hdu2138 How many prime numbers #include <bits/stdc++.h> using namespace std; typedef long long ...

  3. Miller_Rabin素数测试

    #include<iostream> #include<cmath> #include<cstdio> #include<cstring> #inclu ...

  4. HDU2138 & 米勒拉宾模板

    题意: 给出n个数,判断它是不是素数. SOL: 米勒拉宾裸题,思想方法略懂,并不能完全理解,所以实现只能靠背模板.... 好在不是很长... Code: /*==================== ...

  5. 【数论】Miller_Rabin

    Miller_Rabin素数测试     Miller_Rabin判断单个素数的方法运用了费马小定理,可以说非常之快了.     Miller_Rabin曾经被称作“黑科技”,但是根据费马小定理其实完 ...

  6. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  7. 优化后的二次测试Miller_Rabin素性测试算法

    ll random(ll n) { return (ll)((double)rand()/RAND_MAX*n + 0.5); } ll pow_mod(ll a,ll p,ll n) { ) ; l ...

  8. Miller_Rabin (米勒-拉宾) 素性测试

    之前一直对于这个神奇的素性判定方法感到痴迷而又没有时间去了解.借着学习<信息安全数学基础>将素性这一判定方法学习一遍. 首先证明一下费马小定理. 若p为素数,且gcd(a, p)=1, 则 ...

  9. HDU-3864 D_num Miller_Rabin和Pollard_rho

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给定一个数n,求n的因子只有四个的情况. Miller_Rabin和Pollard_rho ...

随机推荐

  1. 使用过滤器解决JSP页面的乱码问题

    乱码详情  总结:讨论了使用GET和POST方法,控制台和JSP页面显示的问题. 最终发现:在servlet或者过滤器中添加:request.setCharacterEncoding("ut ...

  2. maven scope属性值设置含义

    1.枚举各个属性值的含义 compile,缺省值,适用于所有阶段,会打包进项目. provided,类似compile,期望JDK.容器或使用者会提供这个依赖. runtime,只在运行时使用,如JD ...

  3. maven项目的标准目录结构

    maven项目的标准目录结构如下:

  4. Excel VBA宏 链接服务器 上传和下载数据

    首先说明以下. 第一: 下面的 “ _" 也就是 空格下划线 在VBA中表示换行的意思:& 表示链接连个字符串的操作,注意 & 的前后是否需要空格 第二: 如果链接服务器,服 ...

  5. 探路者-Beta发布中间产物

    版本控制 版本控制报告:http://www.cnblogs.com/linym762/p/7881047.html git地址:https://git.coding.net/clairewyd/to ...

  6. 第一个Sprint冲刺成果

    组长:李咏江,组员:叶煜稳,谢洪跃,周伟雄 进程:第一个算法功能完成

  7. FD.io 社区中国行暨未来网络技术沙龙 南京站 参会小结

    FD.io 社区中国行暨未来网络技术沙龙 南京站,2018 年 3 月 17 日. 开场致辞 Ray 介绍了一些有的没的 ⁃ (Future Event)DPDK summit, FD.io summ ...

  8. Internet History, Technology and Security (Week2)

    Week2. History: The First Internet - NSFNet coursera address Supercomputers Justify a National Netwo ...

  9. Team饭来了团队作业3需求改进与系统设计

    团队名称:饭来了 人员组成: 队长:侯晓东          学号:2016012087 队员:崔啸寒          学号:2016012006 队员:方柱权          学号:201601 ...

  10. js如何判断一个值是不是Array类型

    本来判断一个对象类型用typeof是最好的,不过对于Array类型是不适用的可以使用 instanceof操作符var arrayStr=new Array("1","2 ...