POJ 2447 RSA

测试了一下pollard_rho中那个随机的gcc,时间差了有近200+ms

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define oo (1<<28)
#define gcc 121232
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b)
{
    return b==0?a:gcd(b,a%b);
}
LL ext_gcd(LL a,LL b,LL &x,LL &y)
{
    if(!b){
        x=1;y=0;return a;
    }
    LL d=ext_gcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}
LL Mul(LL a,LL b,LL m)
{
    LL r=0;
    while(b)
    {
        if(b&1) r=(r+a)%m;
        a=(a<<1)%m;
        b>>=1;
    }
    return r;
}
LL power(LL a,LL b,LL m)
{
    LL r=1;
    while(b)
    {
        if(b&1) r=Mul(r,a,m);
        a=Mul(a,a,m);
        b>>=1;
    }
    return r;
}
LL pollard_rho(LL n)
{
    LL x,y,d,i=1,k=2;
    x=rand()%(n-1)+1;
    y=x;
    while(true)
    {
        ++i;
        x=((Mul(x,x,n)-gcc)%n+n)%n;
        d=gcd(y-x+n,n);
        if(d>1&&d<n) return d;
        if(x==y) return n;
        if(i==k)
            y=x,k<<=1;
    }
}
int main()
{
    LL c,e,n;
    while(~scanf("%I64d%I64d%I64d",&c,&e,&n))///因为n是2个素数的乘积,直接用pollard_rho分解即可
    {
        LL p=pollard_rho(n),q=n/p;
        LL t=(p-1)*(q-1);
        LL x,d;
        ext_gcd(e,t,d,x);///求e对t的逆元
        d=(d%t+t)%t;
        printf("%I64d\n",power(c,d,n));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值