BZOJ3667 Pollard Rho

https://www.lydsy.com/JudgeOnline/problem.php?id=3667

给你个大质数

判断是不是质数

不是写出最大因子

Pollard Rho模板题

注意大数乘法时要套模板

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
vector <ll> a;

ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}

ll mul(ll a,ll b,ll n){
    return(a*b-(ll)(a/(long double)n*b+1e-3)*n+n)%n;
}


ll quickmod(ll a,ll b,ll m){
    ll ret=1; a%=m;
    while (b){
        if (b&1) ret=mul(ret,a,m);
        a=mul(a,a,m),b>>=1;
    }
    return ret;
}

bool TD(ll a,ll n){
    ll m=n-1,x,y; int num=0;
    while (!(m&1)) m>>=1,num++;
    x=quickmod(a,m,n);
    for (int i=1;i<=num;x=y,i++){
        y=quickmod(x,2,n);
        if ((y==1)&&(x!=1)&&(x!=n-1))
            return true;
    }
    return y!=1;
}
bool MR(int t,ll x){
    if (x==1) return false;
    if (x==2) return true;
    if (!(x&1)) return false;
    while (t--) 
        if (TD(rand()%(x-1)+1,x))
            return false;
    return true;
}
ll PR(ll n,int t){
    ll i=1,k=2,x=rand()%n,y=x,d;
    while (1){
        i++,x=(mul(x,x,n)+t)%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 find(ll x,int k){
    if (x==1) return;
    if (MR(3,x)) return (void)a.push_back(x);
    ll t=x;
    while (t==x) t=PR(x,k--);
    find(t,k),find(x/t,k);
}
int main(){
    int T;
    cin>>T;
    while(T--){
        ll x;
        cin>>x;
        if(MR(3,x)){
            cout<<"Prime"<<endl;
            continue;
        }
        else{
            find(x,100);
            sort(a.begin(),a.end());
            a.erase(unique(a.begin(),a.end()),a.end());
            cout<<*(a.end()-1)<<endl;
            a.clear(); 
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值