@Chilling
2016-08-11T15:29:03.000000Z
字数 1527
阅读 1061
数论
Description
RSA is one of the most powerful methods to encrypt data. The RSA algorithm is described as follow:
You can encrypt data with this method :
%
When you want to decrypt data, use this method :
%
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.
题意:输入p,q,e,l,然后接下来输入l个数字,解码他们得到字符M。
分析:按题目可以计算出,,要得到解密后的字符M,% ,因此要求d,而 % %,因此我们从把d从1开始循环,直到找到一个d使等式成立。之后用快速幂取模求M,以%c的形式输出即可。
#include<stdio.h>
#define LL long long
LL quick(LL x,LL m,LL n)
{
LL ans=1;
while(m>0)
{
if(m%2==1)
ans=ans*x%n;
x=x*x%n;
m=m/2;
}
return ans;
}
int main()
{
LL p,q,e,l,n,fn,d,c,ch;
while(scanf("%lld%lld%lld%lld",&p,&q,&e,&l)!=EOF)
{
n=p*q;
fn=(p-1)*(q-1);
for(d=1;;d++)
if(d*e%fn==1%fn)
break;
while(l--)
{
scanf("%lld",&c);
ch=quick(c,d,n);
printf("%c",ch);
}
printf("\n");
}
return 0;
}