[关闭]
@Chilling 2016-08-11T15:29:03.000000Z 字数 1527 阅读 1065

HDU-1211: RSA(快速幂)

数论


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的形式输出即可。


  1. #include<stdio.h>
  2. #define LL long long
  3. LL quick(LL x,LL m,LL n)
  4. {
  5. LL ans=1;
  6. while(m>0)
  7. {
  8. if(m%2==1)
  9. ans=ans*x%n;
  10. x=x*x%n;
  11. m=m/2;
  12. }
  13. return ans;
  14. }
  15. int main()
  16. {
  17. LL p,q,e,l,n,fn,d,c,ch;
  18. while(scanf("%lld%lld%lld%lld",&p,&q,&e,&l)!=EOF)
  19. {
  20. n=p*q;
  21. fn=(p-1)*(q-1);
  22. for(d=1;;d++)
  23. if(d*e%fn==1%fn)
  24. break;
  25. while(l--)
  26. {
  27. scanf("%lld",&c);
  28. ch=quick(c,d,n);
  29. printf("%c",ch);
  30. }
  31. printf("\n");
  32. }
  33. return 0;
  34. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注