RSA

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1243    Accepted Submission(s): 901

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(极光炫影)
 
Source
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1299 1695 1573 1213 1576 
 
 //0MS    236K    1318 B    G++
/* 题意:
RSA密码加解密法的解密 模拟题:
可以算水题,不过也磨了挺久,一是逆元求法不明确,
二是O(lgn)的n次方模数算法忘了,三是没注意64位,
还有电脑有点卡!!郁闷 */
#include<stdio.h>
#include<string.h>
/***************************************
函数:ExGcd
功能:求两个数的最大公约数和模P的乘法逆元。
输入:a,b 输入参数,求这两个数的最大公约数
和a模b的逆元 或 b模a的逆元。
输出:x,y 分别表示a模b的逆元和b模a的逆元。
返回:r 表示a b 的最大公约数。
*************************************/
__int64 Exgcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
{
if(b==){
x=;
y=;
return a;
}
__int64 r=Exgcd(b,a%b,x,y);
__int64 t=x;
x=y;
y=t-a/b*y;
return r;
}
__int64 fac(__int64 a,__int64 d,__int64 n)
{
a%=n;
int t=;
while(d){
if(d%) t=(t*a)%n;
a=(a*a)%n;
d/=;
}
return t;
}
int main(void)
{
__int64 p,q,e;
__int64 l,a;
while(scanf("%I64d%I64d%I64d%I64d",&p,&q,&e,&l)!=EOF)
{
char c[];
memset(c,,sizeof(c));
__int64 d1=,d2=;
__int64 n=p*q;
Exgcd(e,(p-)*(q-),d1,d2);
d1=(d1+(p-)*(q-))%((p-)*(q-));
//printf("%d %d",d1,d2);
for(int i=;i<l;i++){
scanf("%I64d",&a);
a=fac(a,d1,n);
int b=a;
c[i]=b;
//printf("%d %d %c\n",a,c[i],c[i]);
}
puts(c);
}
return ;
}

hdu 1211 RSA (逆元)的更多相关文章

  1. hdu 1211 RSA

    // 表示题目意思我是理解了蛮久 英语太水了 //首先这是解密公式 m=c^d mod n// 给你 p q e 然后 n=p*q fn=(p-1)*(q-1)// 给你 e,根据公式 e*d mod ...

  2. 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$ ...

  3. hdu 1211 逆元

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

  4. HDU 1576 (乘法逆元)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1576 题目大意:求(A/B)mod 9973.但是给出的A是mod形式n,n=A%9973. 解题思 ...

  5. HDU 5651 组合+逆元

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5651 题目意思我看了半天没读懂,一直以为是回文子串又没看见substring的单词最后看博客才知道是用给 ...

  6. hdu 1576 求逆元

    题意:给出n=A mod 9973和B,求(A/B) mod 9973 昨天用扩展欧几里得做过这题,其实用逆元也可以做. 逆元的定义:例如a*b≡1 (mod m),则b就是a关于m的逆元. 求逆元方 ...

  7. HDU 1211

    水.模拟即可.使用EXGCD求逆元 #include <iostream> #include <cstdio> #include <cstring> #includ ...

  8. HDU 5976 数学,逆元

    1.HDU 5976 Detachment 2.题意:给一个正整数x,把x拆分成多个正整数的和,这些数不能有重复,要使这些数的积尽可能的大,输出积. 3.总结:首先我们要把数拆得尽可能小,这样积才会更 ...

  9. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

随机推荐

  1. python 生成器与迭代器(yield 用法)

    背景 首先,我不会解释这两个名词,我看过很多遍解释,可还是看不懂,还是直接看使用情景吧. 我们以佩波拉契数列为例,当我们不知道迭代器的情况下,我们写出来的代码可能是这样子的: '''这种方式计算fib ...

  2. java基础 序列化反序列化流 实现Serializable 接口 自动装载序列号到对象文本文件如修改不能反序列化对象文本,除非自定义long型常量 打印流

    package com.swift.baseKnowledge; import java.io.File; import java.io.FileInputStream; import java.io ...

  3. github上更新fork项目

    转载:https://blog.csdn.net/qq1332479771/article/details/56087333 ps:需要用GitHub所指定的chrome或者firefox浏览器,其它 ...

  4. LVM(扩展)

    LVM(扩展)======================== [root@aminglinux newdir]# fdisk -l /dev/sdb 磁盘 /dev/sdb:10.7 GB, 107 ...

  5. 学习python第十六天,正则表达式

    正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配.采取动态模糊的匹配,最大的应用是爬虫. re 模块使 Python 语言拥有全部的正则表达式功能. compile 函 ...

  6. Python必学:使用哪款文本编辑器更好?

    Python的交互式命令行写程序,好处是一下就能得到结果,坏处是没法保存,下次还想运行的时候,还得再敲一遍. 所以,实际开发的时候,我们总是使用一个文本编辑器来写代码,写完了,保存为一个文件,这样,程 ...

  7. 477. Total Hamming Distance

    class Solution { public: int totalHammingDistance(vector<int>& nums) { ; ; i < ; i++) { ...

  8. 选择排序算法Java实现

    一. 算法描述 选择排序:比如在一个长度为N的无序数组中,在第一趟遍历N个数据,找出其中最小的数值与第一个元素交换,第二趟遍历剩下的N-1个数据,找出其中最小的数值与第二个元素交换......第N-1 ...

  9. Nginx安装,目录结构与配置文件详解

    1.Nginx简介 Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设 ...

  10. MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':ge

    数据库表里命名有这个字段,可怎么就是报错呢,大神的解释: 加上之后立马好用!!!