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(极光炫影)

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. typedef long long LL;
  7. /*
  8. 题目要求
  9. > calculate n = p × q, calculate F(n) = (p - 1) × (q - 1)
  10. > choose an integer e(1 < e < F(n)), making gcd(e, F(n)) = 1, e will be the public key
  11. > calculate d, making d × e mod F(n) = 1 mod F(n), and d will be the private key
  12. 知道 P Q E
  13. gcd(a,b) == 1 等价于 存在x,y a*x+b*y==1
  14. 存在x,y
  15. e*x + F(n)*y = 1
  16. d*e%F(n) = 1%F(n)
  17. d*e + F(n)*y = 1; 通过求逆元方法解出 d 即可
  18. */
  19. LL p,q,e,l;
  20. LL ex_gcd(LL a,LL b,LL &x,LL &y)
  21. {
  22. if(b==)
  23. {
  24. x = ;
  25. y = ;
  26. return a;
  27. }
  28. LL ans = ex_gcd(b,a%b,x,y);
  29. LL tmp = x;
  30. x = y;
  31. y = tmp - a/b*x;
  32. return ans;
  33. }
  34. LL cal(LL a,LL b,LL c)
  35. {
  36. LL x=,y=;
  37. LL gcd = ex_gcd(a,b,x,y);
  38. if(c%gcd!=) return -;
  39. x *= c/gcd;
  40. b /= gcd;
  41. if(b<) b = -b;
  42. LL ans = x%b;
  43. if(ans<) ans+=b;
  44. return ans;
  45. }
  46. int main()
  47. {
  48. while(scanf("%lld%lld%lld%lld",&p,&q,&e,&l)!=EOF)
  49. {
  50. LL fn = (p-)*(q-),n = p*q;
  51. LL d = cal(e,fn,);
  52. LL tmp,ans;
  53. for(int i=;i<l;i++)
  54. {
  55. scanf("%lld",&tmp);
  56. tmp %= n;
  57. ans = ;
  58. for(int j=;j<d;j++)
  59. ans = (ans*tmp)%n;
  60. printf("%c",ans%n);
  61. }
  62. printf("\n");
  63. }
  64. }

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

  1. HDU 5114 扩展欧几里得

    题目大意:给你两个球的坐标 他们都往(1, 1)这个方向以相同的速度走,问你他们在哪个位置碰撞. 思路:这种题目需要把x方向和y方向分开来算周期,两个不同周期需要用扩展欧几里得来求第一次相遇. #in ...

  2. hdu 2669(扩展欧几里得)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. HDU 4180 扩展欧几里得

    RealPhobia Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. hdu 2669 扩展欧几里得(裸)

    #include<stdio.h> #include<iostream> #define ll __int64 ll gcd(ll a,ll b,ll &x,ll &a ...

  5. 扩展欧几里得 hdu 1576

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 不知道扩展欧几里得的同学可以参考:https://blog.csdn.net/zhjchengf ...

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

    题目链接 题意 : 给你n,e,c,并且知道me ≡ c (mod n),而且n = p*q,pq都为素数. 思路 : 这道题的确与题目名字很相符,是个RSA算法,目前地球上最重要的加密算法.RSA算 ...

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

    题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 2000 ...

  8. hdu 1573 A/B (扩展欧几里得)

    Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973)= 1). Input 数据的第一行 ...

  9. hdu 1576 A/B 【扩展欧几里得】【逆元】

    <题目链接> <转载于 >>> > A/B Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)( ...

随机推荐

  1. ACM_错排(递推dp)

    RPG的错排 Time Limit: 2000/1000ms (Java/Others) Problem Description: 今年暑假GOJ集训队第一次组成女生队,其中有一队叫RPG,但做为集训 ...

  2. yii框架下jquery在ajax更新后失效问题

    解决方案,以live的形式重新绑定一次, /***回复隐藏收起效果***/ $(".btn-reply").live('click',function(event){ var da ...

  3. 经典矩阵dp寻找递增最大长度

    竖向寻找矩阵最大递增元素长度,因为要求至少一列为递增数列,那么每行求一下最大值就可以作为len[i]:到i行截止的最长的递增数列长度. C. Alyona and Spreadsheet time l ...

  4. iOS基础笔试题 - 集锦一

    前言 下文转载自https://mp.weixin.qq.com/s?__biz=MzA4ODk0NjY4NA==&mid=454115946&idx=1&sn=c7f1b50 ...

  5. Dota2团战实力蔑视人类,解剖5只“AI英雄”

    去年,OpenAI 在 DOTA 的 1v1 比赛中战胜了职业玩家 Dendi,而在距离进阶版 OpenAI Five 系统战胜人类业余玩家不过一个月的时间,今天凌晨,它又以 2:1 的战绩再次完成对 ...

  6. ImmutableJS

    引用大神的一句话:(具体是谁自己问度娘) Shared mutable state is the root of all evil(共享的可变状态是万恶之源) -- Pete Hunt   JavaS ...

  7. JMeter在linux上分布式压测环境配置(一)

    环境配置 一.在Linux服务器先安装SDK 1.先从官网下载jdk1.8.0_131.tar.gz,l(linux版本,32位,64位根据系统来判断) 2.在/usr/目录下创建java文件夹,(当 ...

  8. 梦想CAD控件安卓图层

    新建图层 CAD中我们设置好图层后除了我们平常的绘图时选择线段的颜色,线型,线宽等作用,而且我们还可以在出图时选择性显示图形,冻结图形,已达到我们想要的效果. 实现代码说明: //增加一个图层 参数为 ...

  9. vue启动

    首先在终端terminal连上npm 镜像库 npm config set registry https://registry.npm.taobao.orgnpm installnpm run loc ...

  10. css 实现鼠标滑过流光效果

    来划我啊 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...