hdu2138 Miller_Rabin

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=0,a,b;
il int ran(){
    return rand()*rand()+rand();
}
il ll pow(ll base,ll pow){
    ll ans=1;
    for(;pow;pow>>=1){
        if(pow&1) ans=ans*base%n;
        base=base*base%n;
    }
    return ans;
}
il bool chk(){
    ll x=ran()%(n-1)+1,now=pow(x,a);
    if(now==1) return true;
    for(int i=0;i<b;i++){
        if(now==n-1) return true;
        now=now*now%n;
    }
    return false;
}
il bool isprime(){
    a=n-1;b=0;
    while(a%2==0){
        a/=2;b++;
    }
    for(int i=1;i<=100;i++)
        if(!chk()) return false;
    return true;
}
il void init(){
    srand(T);ans=0;
    for(int i=1;i<=T;i++){
        cin>>n;
        ans+=isprime();
    }
    cout<<ans<<endl;
}
int main(){
    while(scanf("%d",&T)!=EOF){
        init();
    }
    return 0;
}
时间: 2024-08-07 17:02:16

hdu2138 Miller_Rabin的相关文章

HDU2138 随机素数测试 Miller-Rabin算法

题目描述 Give you a lot of positive integers, just to find out how many prime numbers there are.. 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

Miller_Rabin算法(随机算法,判断一个数是否是素数)

1 const int S = 20;//随机算法判定次数,S越大,判错概率越小 2 LL pow_mod(LL a, LL b, LL mod) { // a^b%mod 3 LL ans = 1; 4 a = a % mod; 5 while(b) { 6 if(b & 1) { 7 ans = (ans * a) % mod; 8 } 9 a = ( a * a ) % mod; 10 b >>= 1; 11 } 12 return ans; 13 } 14 bool check

POJ 1181 大整数是否为素数以及求大整数的质因数-数论-(Miller_rabin+Pollard_rho)

题意:求一个整数是否是素数,如果不是,则输出它最小的质因数. 分析: 判断一个大整数是否为素数用Miller_rabin算法,求一个大整数的所有质因数用Pollard_rho算法.这题就是直接套模板. 另外这里的gcd和pow_mod不能用一般的方式,T了.代码里我注释掉的就是T了的写法. 代码: #include<iostream> #include<cmath> #include<ctime> #include<cstdio> #include<a

HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )

How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12955    Accepted Submission(s): 4490 Problem Description Give you a lot of positive integers, just to find out how many pr

HDOJ 5150 Sum Sum Sum Miller_Rabin

很少有这么裸的题目,测一下Miller_Rabin Sum Sum Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 72    Accepted Submission(s): 52 Problem Description We call a positive number X P-number if there is not a

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

Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the

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

素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: http://blog.csdn.net/maxichu/article/details/45459533 然后是参考了kuangbin的模板: http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646396.html 模板如下: //快速乘 (a

Miller_Rabin codevs 1702 素数判定2

#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<ctime> #define ll long long #define T 10 using namespace std; ll slow_mul(ll a,ll b,ll c)//防止爆掉 { ll ans=0; a=a%c;b=b%c; while(b) { if(b&1)

miller_rabin模板

miller_rabin素数测试法 #include <iostream> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <math.h> #include <stdlib.h> #include <iomanip> #include <ctime> #define ll long long using namespace