Miller-Rabin素数快速检测

满足费马小定理 a^(n-1) === 1(mod n)

--->伪素数      

对于所有a belong Zn*,总存在满足的合数n,称为Carmichael数

----------------------------------

【Miller-Rabin】:

1.随机找多个s个a

2.二次探测定理: 如果p是奇素数,则 x2 === 1(mod p)的解为 x = 1 || x = p - 1(mod p)    {如:5的话,1或4}

//Miller-Rabin
//n  prime a -->a^(n-1)===1(mod n) -->fastPowMod(a,n-1,n)==1
//warn: Carmichael/lucky

ll mulModhaoxiangmeiyonghenman(ll a,ll b,ll n){
    ll ans=0;
    for(;b;a=(a<<1)%n,b>>=1)
        if(b&1)
            ans=(ans+a)%n;
    return ans;
}

ll mulMod(ll a,ll b,ll n){    //黑科技
    ll ans=(a*b-(ll)((long double)a/n*b+0.5)*n);
    return ans<0?ans+n:ans;
}

ll powMod(ll a,ll b,ll n){
    ll ans=1;
    for(;b;a=mulMod(a,a,n),b>>=1)
        if(b&1)
            ans=(ans*a)%n;
    return ans;
}

bool witness(ll a,ll n,ll u,int t){
    ll now=powMod(a,u,n),pre=now;

    for(int i=1;i<=t;i++){
        now=mulMod(now,now,n);
        if(now==1&&pre!=1&&pre!=n-1)
            return true;
        pre=now;
    }
    if(now!=1) return true;
    return false;
}

bool mrP(ll n){
    if(n<=1) return false;
    if(n==2) return true;
    if((n&1)==0) return false;

    ll u=n-1;
    int t=0;
    while((u&1)==0) u>>=1,t++;  //n-1=2^t *u

    int a[6]={2,3,5,7,11,13};   //or random
    for(int i=0;i<6;i++){
        if(n==a[i]) return true;
        else if(witness(a[i],n,u,t)) return false;
    }
    return true;
}
时间: 2024-10-31 11:58:28

Miller-Rabin素数快速检测的相关文章

51nod 1106 质数检测(miller rabin 素数测试.)

1106 质数检测 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出N个正整数,检测每个数是否为质数.如果是,输出"Yes",否则输出"No". Input 第1行:一个数N,表示正整数的数量.(1 <= N <= 1000) 第2 - N + 1行:每行1个数(2 <= S[i] <= 10^9) Output 输出共N行,每行为 Yes 或 No. Input示例 5 2 3 4 5 6

POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 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 num

POJ2429_GCD &amp;amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】

GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b.

HDU1164_Eddy&amp;#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6664    Accepted Submission(s): 3997 Problem Description Eddy's interest is very extensive, recently he is interested in prime

POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数测试】【Pollar Rho整数分解】

GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b.

POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】

Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 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 num

HDU1164_Eddy&#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6664    Accepted Submission(s): 3997 Problem Description Eddy's interest is very extensive, recently he is interested in prime

[算法]Miller Robbin素数判定

Miller Robbin素数判定 一.实现原理 我们以前都是怎么判断素数的呢: inline int is_prime(int n){ if(n==1) return 0; for(int i=2;i<=sqrt(n);i++){ if(n%i==0) return 0; } return 1; } 现在,我们希望更快的判断一个数是否为素数. 我们可以借助费马小定理来判断: 如果p是一个质数,而整数a不是p的倍数,则有 \[a^{p-1}\equiv 1\pmod p\] Miller Rob

逆向工程模型快速检测软件 Geomagic.Qualify.v12.12 1C

逆向工程模型快速检测软件 Geomagic.Qualify.v12.12 1CD Geomagic Qualify是一款用于产品快速检测的软件,可对产品的数字样机模型进行逆向工程分析研究.在产品的计算机辅助设计 (CAD) 模型和产品的制造件之间快速可视化图形比较,首件检验.生产线上或是车间内检验.趋势分析.二维和三维几何形状尺寸标注及自动报告生成. 新版本新增的许多功能.新的检测方法又会掀起数字化制造测量的新浪潮,是检测测量专业领域的软件,测量范围涉及各个行业! VirtualGrid.VRM