• loj2045 「CQOI2016」密钥破解


    CQOI 板子大赛之 pollard rho

    #include <iostream>
    #include <cstdio>
    using namespace std;
    typedef long long ll;
    ll e, N, c, jzm, p, q, r, d, n;
    ll gcd(ll a, ll b){
    	return !b?a:gcd(b,a%b);
    }
    ll mul(ll a, ll b, ll p){
    	ll re=0;
    	while(b){
    		if(b&1)	re = (re + a) % p;
    		a = (a + a) % p;
    		b >>= 1;
    	}
    	return re;
    }
    ll pollardRho(){
    	ll x=2, y=2;
    	while(true){
    		x = (mul(x,x,N)+jzm) % N;
    		y = (mul(y,y,N)+jzm) % N;
    		y = (mul(y,y,N)+jzm) % N;
    		if(x==y)	return 1;
    		ll re=gcd(x>y?x-y:y-x, N);
    		if(re>1)	return re;
    	}
    }
    ll exgcd(ll a, ll b, ll &x, ll &y){
    	if(!b){
    		x = 1;
    		y = 0;
    		return a;
    	}
    	ll re=exgcd(b, a%b, x, y);
    	ll z=x;
    	x = y;
    	y = z - (a / b) * y;
    	return re;
    }
    ll ksm(ll a, ll b, ll p){
    	ll re=1;
    	while(b){
    		if(b&1)	re = mul(re, a, p);
    		a = mul(a, a, p);
    		b >>= 1;
    	}
    	return re;
    }
    int main(){
    	cin>>e>>N>>c;
    	do{
    		jzm++;
    		p = pollardRho();
    	}while(p==1);
    	q = N / p;
    	r = (p - 1) * (q - 1);
    	ll dddddd;
    	exgcd(e, r, d, dddddd);
    	d = (d % r + r) % r;
    	n = ksm(c, d, N);
    	cout<<d<<" "<<n<<endl;
    	return 0;
    }
    
  • 相关阅读:
    hdu 4302 Holedox Eating 夜
    poj 1947 Rebuilding Roads 夜
    hdu 4303 Hourai Jeweled 夜
    poj 1286 Necklace of Beads 夜
    poj 2057 The Lost House 夜
    hdu 4301 Divide Chocolate 夜
    poj 3140 Contestants Division 夜
    BOM(二)
    Str的方法
    求出字符窜的字母的个数
  • 原文地址:https://www.cnblogs.com/poorpool/p/8954819.html
Copyright © 2020-2023  润新知