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;
}
posted @ 2017-09-08 21:13  Lweleth  阅读(141)  评论(0编辑  收藏  举报