HDU 1211 RSA (扩展欧几里得+快速幂)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1211


题意:给你四个数p,q,e,l

n=p*q,f=(p-1)*(q-1)

要你求出一个d满足(e*d)%f=1

然后给你l个c,求出每个c^d%n对应的ASCII。

题解:求d用扩展欧几里得,后面一个用快速幂


AC代码:

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;

typedef long long LL;
const int N=52;
const LL II=29;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);

LL p,q,e,l,n,f;

LL love(LL a,LL b,LL c)
{
    LL ans=1;
    while(b)
    {
        if(b&1) ans=(ans*a)%c;
        a=(a*a)%c;
        b=b>>1;
    }
    return ans;
}

LL ext_gcd(LL a,LL b,LL &x,LL &y)
{
    LL t,ret;
    if(!b)
    {
        x=1,y=0;
        return a;
    }
    ret=ext_gcd(b,a%b,x,y);
    t=x,x=y,y=t-a/b*y;
    return ret;
}

int main()
{
    int i,j;
    while(~scanf("%I64d%I64d%I64d%I64d",&p,&q,&e,&l))
    {
        n=p*q;
        f=(p-1)*(q-1);
        LL d,x,y,c;
        d=ext_gcd(e,f,x,y);
        d=(x%f+f)%f;
        for(LL i=1;i<=l;i++)
        {
            scanf("%I64d",&c);
            x=love(c,d,n);
            printf("%c",char(x));
        }
        puts("");
    }
    return 0;
}
/*

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值