HDU 1211 EXGCD

EXGCD的模板水题

RSA算法
给你两个大素数p,q
定义n=pq,F(n)=(p-1)(q-1)
找一个数e 使得(e⊥F(n))
实际题目会给你e,p,q
计算d,$de \mod F(n) = 1$
然后解密的值为$c_{i}^d \mod n$,转换成char输出 用EXGCD求出d就好了

 

/** @Date    : 2017-09-07 22:17:00
  * @FileName: HDU 1211 EXGCD.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    LL d = a;
    if(b == 0)
    {
        x = 1;
        y = 0;
    }
    else
    {
        d = exgcd(b, a % b, y, x);
        y -= (a / b)*x;
    }
    return d;
}

LL fpow(LL a, LL n, LL mod)
{
	LL res = 1;
	while(n)
	{
		if(n & 1)
			res = (res * a % mod + mod) %mod;
		a = (a * a % mod + mod) % mod;
		n >>= 1;
	}
	return res;
}
LL p, q, e, n;
LL a[N];
int main()
{
	while(~scanf("%lld%lld%lld%lld", &p, &q, &e, &n))
	{
		for(int i = 0; i < n; i++)	scanf("%lld", a + i);
		LL mod = p * q;
		LL fn = (p - 1) * (q - 1);
		for(int i = 0; i < n; i++)
		{
			LL d = 0 , y = 0;
			exgcd(e, fn, d, y);
			d = (d + fn) % fn;
			a[i] %= mod;
			LL ans = fpow(a[i], d, mod);
			printf("%c", fpow(a[i], d, mod) % mod);
		}
		printf("\n");
	}
    return 0;
}

转载于:https://www.cnblogs.com/Yumesenya/p/7496425.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值