找x的两个素数因子使x=pq(Pollard_Rho)

void Pollard_Rho(int x)  {
    if(Test(x)) { ///素数测试
        Ans=max(x,Ans);
        return; 
    }
    int t1=rand()%(x-1)+1;
    int t2=t1,b=rand()%(x-1)+1;
    t2=(mul(t2,t2,x)+b)%x;
    int p=1;
    int i=0;
    while(t1!=t2) {
        ++i;
        p=qmul(p,abs(t1-t2),x);
        if(p==0) {
            int t=gcd(abs(t1-t2),x);
            if(t!=1&&t!=x) {
                Pollard_Rho(t),Pollard_Rho(x/t);    
            }
            return;
        }
        if(i%127==0) {
            p=gcd(p,x);
            if(p!=1&&p!=x) {
                Pollard_Rho(p),Pollard_Rho(x/p);
                return;
            }
            p=1;
        }
        t1=(mul(t1,t1,x)+b)%x;
        t2=(mul(t2,t2,x)+b)%x;
        t2=(mul(t2,t2,x)+b)%x;
    }
    p=gcd(p,x);
    if(p!=1&&p!=x) {
        Pollard_Rho(p),Pollard_Rho(x/p);
        return;
    }
}
原文地址:https://www.cnblogs.com/iwomeng/p/11628446.html