HDU RSA 扩展欧几里得

Problem Description

RSA is one of the most powerful methods to encrypt data. The RSA algorithm is described as follow:

> choose two large prime integer p, q
> calculate n = p × q, calculate F(n) = (p - 1) × (q - 1)
> choose an integer e(1 < e < F(n)), making gcd(e, F(n)) = 1, e will be the public key
> calculate d, making d × e mod F(n) = 1 mod F(n), and d will be the private key

You can encrypt data with this method :

C = E(m) = me mod n

When you want to decrypt data, use this method :

M = D(c) = cd mod n

Here, c is an integer ASCII value of a letter of cryptograph and m is an integer ASCII value of a letter of plain text.

Now given p, q, e and some cryptograph, your task is to "translate" the cryptograph into plain text.

Input

Each case will begin with four integers p, q, e, l followed by a line of cryptograph. The integers p, q, e, l will be in the range of 32-bit integer. The cryptograph consists of l integers separated by blanks.

Output

For each case, output the plain text in a single line. You may assume that the correct result of plain text are visual ASCII letters, you should output them as visualable letters with no blank between them.

Sample Input

101 103 7 11
7716 7746 7497 126 8486 4708 7746 623 7298 7357 3239

Sample Output

I-LOVE-ACM.

Author

JGShining(极光炫影)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
/*
题目要求
> calculate n = p × q, calculate F(n) = (p - 1) × (q - 1)
> choose an integer e(1 < e < F(n)), making gcd(e, F(n)) = 1, e will be the public key
> calculate d, making d × e mod F(n) = 1 mod F(n), and d will be the private key
知道 P Q E
gcd(a,b) == 1  等价于 存在x,y  a*x+b*y==1
存在x,y
e*x + F(n)*y = 1
d*e%F(n) = 1%F(n)
d*e + F(n)*y = 1; 通过求逆元方法解出 d 即可
*/
LL p,q,e,l;
LL ex_gcd(LL a,LL b,LL &x,LL &y)
{
    if(b==0)
    {
        x = 1;
        y = 0;
        return a;
    }
    LL ans = ex_gcd(b,a%b,x,y);
    LL tmp = x;
    x = y;
    y = tmp - a/b*x;
    return ans;
}
LL cal(LL a,LL b,LL c)
{
    LL x=0,y=0;
    LL gcd = ex_gcd(a,b,x,y);
    if(c%gcd!=0) return -1;
    x *= c/gcd;
    b /= gcd;
    if(b<0) b = -b;
    LL ans = x%b;
    if(ans<0) ans+=b;
    return ans;
}
int main()
{
    while(scanf("%lld%lld%lld%lld",&p,&q,&e,&l)!=EOF)
    {
        LL fn = (p-1)*(q-1),n = p*q;
        LL d = cal(e,fn,1);
        LL tmp,ans;
        for(int i=0;i<l;i++)
        {
            scanf("%lld",&tmp);
            tmp %= n;
            ans = 1;
            for(int j=0;j<d;j++)
                ans = (ans*tmp)%n;
            printf("%c",ans%n);
        }
        printf("\n");
    }
}
时间: 2024-12-26 10:13:26

HDU RSA 扩展欧几里得的相关文章

hdu 1576 扩展欧几里得

(A/B)%9973=K A/B=K+9973*X A=BK+9973*X*B A%9973=n; BK%9973=n; BK=n+9973*Y (K/n)*B+(-Y/n)*9973=GCD(B,9973)=1; 求出k/n,求出k 1 /* 2 扩展欧几里得 3 扩展欧几里德算法是用来在已知a, b求解一组x,y使得ax+by = Gcd(a, b) =d(解一定存在,根据数论中的相关定理) 4 */ 5 #include<cstdio> 6 #include<iostream>

hdu 2669(扩展欧几里得)

Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4400    Accepted Submission(s): 1852 Problem Description The Sky is Sprite.The Birds is Fly in the Sky.The Wind is Wonderful.Blew Throw t

HDU 4180 扩展欧几里得

RealPhobia Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 376    Accepted Submission(s): 151 Problem Description Bert is a programmer with a real fear of floating point arithmetic. Bert has qui

URAL 1141. RSA Attack(欧拉定理+扩展欧几里得+快速幂模)

题目链接 题意 : 给你n,e,c,并且知道me ≡ c (mod n),而且n = p*q,pq都为素数. 思路 : 这道题的确与题目名字很相符,是个RSA算法,目前地球上最重要的加密算法.RSA算法原理 . 看到这个算法之后,就知道这个题是求cd≡m(mod n),要求m,就要先求d,而d则是e的模反元素. 如果两个正整数a和n互质,那么一定可以找到整数b,使得 ab-1 被n整除,或者说ab被n除的余数是1.这时,b就叫做a的模反元素. 由模反元素可知,ed≡1(mod Phi[n])(p

HDU 2669 (扩展欧几里得入门)

练习一下数学知识了.. [题目链接]click here~~ [题目大意]Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead. 求满足式子的x和y否则输出"sorry" [解题思路]扩展欧几里得的基础了, 扩展欧几里德算法是用来在已知a, b求解一组x,y,使它们满足 等式: ax+by = gcd

[ACM] hdu 3923 Invoker (Poyla计数,快速幂运算,扩展欧几里得或费马小定理)

Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful. In

hdu 5512 Pagodas 扩展欧几里得推导+GCD

题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 20000) T组数据T <= 500; 思路:由扩展欧几里得知道对于任意正整数,一定存在整数x,y使得 x*a + y*b = gcd(a,b);并且这个gcd是a,b组成的最小正整数:同时也知道了这也是两个点之间的最小距离: 之后直接求点的个数即可: ps:这道题我竟然想到了组合游戏..明显没有说双方都要用

扩展欧几里得 HDU 1576

题意;要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). 因为:A%9973=n; 所以:9973*y+n=A: 设:A/B=x;(可以整除) 所以:9973*y+n=B*x; 所以:B*x-9973*y=n; ①式 又因为:gcd(B,9973) = 1; 所以必存在 x1*B+9973*y1=1;②式 ②式*n=①式 所以只要求出x1,就可以得到x,又因为x=a/b,只要在 mod 9973就是答案了. 现

[ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful. In