[Rho大整数分解] BZOJ 4522 [Cqoi2016]密钥破解

这种裸题 就是Rho的直接应用

长点记性啊,linux下RAND_MAX=2^31 狂T啊啊


#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> abcd;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(ll &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

inline ll Random(ll n)
{
//	return (ll)((double)rand()/(RAND_MAX+1)*n);
	return (ll)rand()*rand()%n;
}

inline ll gcd(ll a,ll b)
{
	return b!=0?gcd(b,a%b):a;
}

inline ll Mul(ll a,ll b,ll p)
{
	ll t=0;
	a%=p; b%=p;
	for(;b;b>>=1,a=(a+a)%p)
		if(b&1)
			(t+=a)%=p;
	return t;
}

inline ll Pow(ll a,ll b,ll p)
{
	ll t=1;
	for(;b;b>>=1,a=Mul(a,a,p))
		if(b&1)
			t=Mul(t,a,p);
	return t;
}

ll rho(ll n,ll c)  
{  
    ll k=2,x=Random(n),y=x,p=1;  
    for(ll i=1;1;i++)  
    {  
        x=(Mul(x,x,n)+c)%n;
		if (y==x) return n;  
        p=gcd(n,abs(x-y));
        if(1<p && p<n) return p;  
        if(i==k) y=x,k<<=1;
    }
}

ll e,N,c,r,d,m;
ll P,Q;

inline void Calc(ll n,ll c)  
{
    ll t=n;  
	ll k=c;
	while(t==n) t=rho(t,k--);  
	P=t; Q=n/t;
}

inline abcd EXGCD(ll x,ll y){
	abcd ret;
	if (x<y) { ret=EXGCD(y,x); return abcd(ret.second,ret.first); }
	if (y==0) return abcd(1,0);
	ret=EXGCD(y,x%y);
	return abcd(ret.second,ret.first-x/y*ret.second);
}

inline ll inv(ll a,ll p){
	return ((EXGCD(a,p).first)%p+p)%p;
}

int main()
{
	srand(10007);
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(e); read(N); read(c);
	Calc(N,10007);
	r=(P-1)*(Q-1);
	d=inv(e,r);
	m=Pow(c,d,N);
	printf("%lld %lld\n",d,m);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值