Miller_Rabin大素数测试与Pollard_rho整数分解模版

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int Times = 20;
LL factor[100], l;
LL gcd(LL a, LL b)
{
	return b ? gcd(b, a%b):a;
}
LL add_mod(LL a, LL b, LL n)
{
    LL ans = 0;
    while(b)
    {
        if(b&1)
            ans = (ans + a)%n;
        b >>= 1;
        a = (a<<1)%n;
    }
    return ans;
}
LL pow_mod(LL a, LL m, LL n)
{
    LL ans = 1;
    while(m)
    {
        if(m&1)
            ans = add_mod(ans, a, n);
        m >>= 1;
        a = add_mod(a, a, n);
    }
    return ans;
}
bool Witness(LL a, LL n)
{
    int j = 0;
    LL m = n-1;
    while(!(m&1))
    {
        j++;
        m >>= 1;
    }
    LL x = pow_mod(a, m, n);
    if(x == 1 || x == n-1)
        return true;
    while(j--)
    {
        x = add_mod(x, x, n);
        if(x == n-1)
            return true;
    }
    return false;
}
bool Miller_Rabin(LL n)
{
    if(n < 2)
        return false;
    if(n == 2)
        return true;
    if(!(n&1))
        return false;
    for(int i = 0; i < Times; i++)
    {
        LL a = rand()%(n-1)+1;
        if(!Witness(a, n))
            return false;
    }
    return true;
}
LL Pollard_rho(LL n, LL c)
{
	LL i = 1, x = rand()%(n-1)+1, y = x, k = 2, d;
	//srand(time(NULL));
	while(true)
	{
		i++;
		x = (add_mod(x,x,n)+c)%n;
		d = gcd(y-x,n);
		if(d > 1 && d < n)
			return d;
		if(y == x)
			return n;
		if(i == k)
		{
			y = x;
			k <<= 1;
		}
	}
}
void get_fact(LL n, LL k)
{
	if(n == 1)
		return;
	if(Miller_Rabin(n))
	{
		factor[l++] = n;
		return;
	}
	LL p = n;
	while(p >= n)
	{
		p = Pollard_rho(p, k--);
	}
	get_fact(p, k);
	get_fact(n/p, k);
}

时间: 2024-08-24 10:22:52

Miller_Rabin大素数测试与Pollard_rho整数分解模版的相关文章

大素数测试和分解质因数

Prime Test http://poj.org/problem?id=1811 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 typedef __int64 LL; 5 LL mulmod(LL a,LL b,LL c) { //ret=(a*b)%c 6 LL ret=0; 7 for(; b; a=(a<<1)%c,b>>=1) { 8 if(b&1) {

Miller_Rabbin大素数测试

伪素数: 如果存在和n互素的正整数a满足a^(n-1)≡1(mod n),则n是基于a的伪素数. 是伪素数但不是素数的个数是非常非常少的,所以如果一个数是伪素数,那么他几乎是素数. Miller_Rabbin素数测试:随机选k个a进行a^(n-1)≡1(mod n)测试,如果都满足则判断n是素数. a^(n-1)%mod用快速幂计算.对于大数相乘(两个大于int的数相乘),中间结果可能溢出,所以需要用快速幂思想进行乘法取模. Miller_Rabbin的出错率为2^(-k). 1 //Mille

Light OJ 1341 Aladdin and the Flying Carpet Pollard_rho整数分解+DFS

输入a b 求有多少对p, q 使得p*q == a && p < q && p >= b 直接大整数分解 然后dfs搜出所有可能的解 #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <cmath> using namespace std; typedef long long LL

Miller-Rabin大素数测试模板

根据费马小定理: 对于素数n,a(0<a<n),a^(n-1)=1(mod n) 如果对于一个<n的正整数a,a^(n-1)!=1(mod n),则n必不是素数. 然后就可以随机生成  <n的数,如果都满足,那n就极有可能是素数. 看书上说,一次素数测试的成功率是 3/4,也就是失败率是1/4,那测m次是错误的概率为:(1/4)^m.可见m稍微大一点就基本不会出错. 但是还有一种数叫,卡迈克尔数. 卡迈克尔数: 一个合数n,对所有满足 gcd(b,n)=1的正整数b都有b^(n-1

Miiler-Robin素数测试与Pollard-Rho大数分解法

板题 Miiler-Robin素数测试 目前已知分解质因数以及检测质数确定性方法就只能\(sqrt{n}\)试除 但是我们可以基于大量测试的随机算法而有大把握说明一个数是质数 Miler-Robin素数测试基于以下两个原理: 费马小定理 即我们耳熟能详的 对于质数\(p\) \[a^{p - 1} \equiv 1 \pmod p\] 二次探测原理 对于质数\(p\),如果存在\(x\)满足 \[x^2 \equiv 1 \pmod p\] 那么\(x\)只能是\(1\)或者\(p - 1\)

数论 - 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算法)

POJ1811 给一个大数,判断是否是素数,如果不是素数,打印出它的最小质因数 随机素数测试(Miller_Rabin算法) 求整数素因子(Pollard_rho算法) 科技题 1 #include<cstdlib> 2 #include<cstdio> 3 const int maxn=10005; 4 const int S=20; 5 int tot; 6 long long n; 7 long long factor[maxn]; 8 long long muti_mod(

数论快速入门(同余、扩展欧几里德、中国剩余定理、大素数测定和整数分解、素数三种筛法、欧拉函数以及各种模板)

数学渣渣愉快的玩了一把数论,来总结一下几种常用的算法入门,不过鶸也是刚刚入门, 所以也只是粗略的记录下原理,贴下模板,以及入门题目(感受下模板怎么用的) (PS:文中蓝色字体都可以点进去查看百度原文) 附赠数论入门训练专题:点我打开专题(题目顺序基本正常,用以配套数论入门) 一.同余定理 简单粗暴的说就是:若 a-b == m 那么 a%m == b%m 这个模运算性质一眼看出...直接上入门水题: Reduced ID Numbers 附AC代码(这个也没啥模板....知道就好) #inclu

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