zoukankan      html  css  js  c++  java
  • BZOJ 4522 Pollard-rho+exgcd

    思路:
    N=P*Q
    求出来P和Q 模拟就好…

    //By SiriusRen
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    ll shai[10]={2,3,5,7,11,13,17,19,21,23};
    ll mul(ll x,ll y,ll mod){
        x%=mod;ll res=0;
        while(y){
            if(y&1)res=(x+res)%mod;
            (x<<=1)%=mod,y>>=1;
        }return res;
    }
    ll pow(ll x,ll y,ll mod){
        x%=mod;ll res=1;
        while(y){
            if(y&1)res=mul(res,x,mod);
            x=mul(x,x,mod),y>>=1;
        }return res;
    }
    bool check(ll a,ll n,ll r,int s){
        ll x=pow(a,r,n),pre=x;
        for(int i=1;i<=s;i++){
            x=mul(x,x,n);
            if(x==1&&pre!=1&&pre!=n-1)return 0;
            pre=x;
        }return x==1;
    }
    bool miller_rabin(ll x){
        if(x<=1)return 0;
        ll r=x-1;int s=0;
        while(!(r&1))r>>=1,s++;
        for(int i=0;i<10;i++){
            if(x==shai[i])return 1;
            if(!check(shai[i],x,r,s))return 0;
        }return 1;
    }
    ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
    ll prime_factor(ll n,ll c){
        ll k=2,x=rand()%n,y=x,p=1;
        for(int i=1;p==1;i++){
            x=(mul(x,x,n)+c)%n;
            p=gcd(abs(x-y),n);
            if(i==k)y=x,k<<=1;
        }return p;
    }
    ll P,Q,R,E,N,C,X,Y;
    void pollard_rho(ll x){
        if(x==1)return;
        if(miller_rabin(x)){P=x;return;}
        ll p=x;
        while(p==x)p=prime_factor(x,rand()%(x-1));
        pollard_rho(p),pollard_rho(x/p);
    }
    ll exgcd(ll a,ll b,ll &x,ll &y){
        if(!b)x=1,y=0;
        else exgcd(b,a%b,y,x),y-=x*(a/b);
    }
    int main(){
        scanf("%lld%lld%lld",&E,&N,&C);
        pollard_rho(N),Q=N/P,R=(P-1)*(Q-1);
        exgcd(E,R,X,Y),X=(X+R)%R;
        printf("%lld %lld
    ",X,pow(C,X,N));
    }

    这里写图片描述

  • 相关阅读:
    java 06 作业代码
    java 06 abstract 抽象类
    java 06 重写(覆盖) final 内部类
    java 06 重写和final
    java 06 继承
    java 05 this static构造函数
    java 05 构造函数-构造代码块
    java 05 heap satck 堆和栈
    java 05 成员变量和成员函数-封装
    BJFU-207-基于顺序存储结构的图书信息表的逆序存储
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6532029.html
Copyright © 2011-2022 走看看