数论二1010大整数的质因子分解(此题模板得记)

题目大意:求n 是否只有4个因子 如果是的就输出除1外的所有因子

本题特点:这题目n 太大太大肯定使用不了 欧拉筛质因数分解,空间肯定肯定会爆炸的 1*10^9还是可以使用欧拉筛的

需要用到Pollard_rbo 和Miller_Rabin 算法 。Miller_Rabin算法的作用是判断一个数是否为素数,算法速度很快,虽然是概率算法但是多次运算可以大幅度减少误判,误判概率为 2^(-t)  当t 够大是,误判Pollard_rho算法作用是求一个数的因子

复杂度为O(sqrt(p)),p为这个数的因子

具体算法 参考PPT 以及相关博客 我看不懂看不懂  模板也太长 到考试就算遇到我可能也直接GG

参考博客 http://blog.csdn.net/a601025382s/article/details/12177549

下面贴出代码 牢记模板!!!

#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <ctime>  
#include <iostream>  
#include <algorithm>  
using namespace std;  
typedef __int64 LL;  
const LL NUM=10;//运算次数,Miller_Rabin算法为概率运算,误判率为2^(-NUM);  
LL t,f[100];  
LL mul_mod(LL a,LL b,LL n)//求a*b%n,由于a和b太大,需要用进位乘法  
{  
    a=a%n;  
    b=b%n;  
    LL s=0;  
    while(b)  
    {  
        if(b&1)  
            s=(s+a)%n;  
        a=(a<<1)%n;  
        b=b>>1;  
    }  
    return s;  
}  
LL pow_mod(LL a,LL b,LL n)//求a^b%n  
{  
    a=a%n;  
    LL s=1;  
    while(b)  
    {  
        if(b&1)  
            s=mul_mod(s,a,n);  
        a=mul_mod(a,a,n);  
        b=b>>1;  
    }  
    return s;  
}  
bool check(LL a,LL n,LL r,LL s)  
{  
    LL ans,p,i;  
    ans=pow_mod(a,r,n);  
    p=ans;  
    for(i=1;i<=s;i++)  
    {  
        ans=mul_mod(ans,ans,n);  
        if(ans==1&&p!=1&&p!=n-1)return true;  
        p=ans;  
    }  
    if(ans!=1)return true;  
    return false;  
}  
bool Miller_Rabin(LL n)//Miller_Rabin算法,判断n是否为素数  
{  
    if(n<2)return false;  
    if(n==2)return true;  
    if(!(n&1))return false;  
    LL i,r,s,a;  
    r=n-1;s=0;  
    while(!(r&1)){r=r>>1;s++;}  
    for(i=0;i<NUM;i++)  
    {  
        a=rand()%(n-1)+1;  
        if(check(a,n,r,s))  
            return false;  
    }  
    return true;  
}  
LL gcd(LL a,LL b)  
{  
    return b==0?a:gcd(b,a%b);  
}  
LL Pollard_rho(LL n,LL c)//Pollard_rho算法,找出n的因子  
{  
    LL i=1,j,k=2,x,y,d,p;  
    x=rand()%n;  
    y=x;  
    while(true)  
    {  
        i++;  
        x=(mul_mod(x,x,n)+c)%n;  
        if(y==x)return n;  
        if(y>x)p=y-x;  
        else p=x-y;  
        d=gcd(p,n);  
        if(d!=1&&d!=n)return d;  
        if(i==k)  
        {  
            y=x;  
            k+=k;  
        }  
    }  
}  
void find(LL n)//找出n的所有因子  
{  
    if(Miller_Rabin(n))  
    {  
        f[t++]=n;//保存所有因子  
        return;  
    }  
    LL p=n;  
    while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);//由于p必定为合数,所以通过多次求解必定能求得答案  
    find(p);  
    find(n/p);  
}  
int main()  
{  
    srand(time(NULL));//随机数设定种子  
    LL n;  
    while(cin>>n)  
    {  
        if(n==1){cout<<"is not a D_num"<<endl;continue;}//特判  
        t=0;  
        find(n);  
        if(t!=2&&t!=3){cout<<"is not a D_num"<<endl;continue;}  
        sort(f,f+t);  
        if(t==2)  
        {  
            if(f[0]!=f[1])cout<<f[0]<<" "<<f[1]<<" "<<n<<endl;  
            else cout<<"is not a D_num"<<endl;  
        }  
        else//n是一个素数的三次方  
        {  
            if(f[0]==f[1]&&f[1]==f[2])cout<<f[0]<<" "<<f[0]*f[0]<<" "<<n<<endl;  
            else cout<<"is not a D_num"<<endl;  
        }  
    }  
    return 0;  
}  






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值