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. Element表单验证(1)

    Element表单验证(1) 首先要掌握Element官方那几个表单验证的例子,然后才看下面的教程. Element主要使用了async-validator这个库作为表单验证 async-valida ...

  2. laravel查看执行的sql语句

    方法一: 我们有时候想测试一段代码生产的 SQL 语句,比如: 我们想看 App\User::all(); 产生的 SQL 语句,我们简单在 routes.php 做个实验即可: //app/Http ...

  3. busybox编译 fatal error: curses.h: 没有那个文件或目录解决办法

    执行make menuconfig时出现如下错误@ubuntu:/home/dev/busybox-1.19.3# make menuconfig HOSTCC scripts/kconfig/lxd ...

  4. 12 new方法和单例、定制访问函数、装饰器

    new方法和单例.定制访问函数.装饰器 上节课作业解答 # 通过多重继承方法,分别定义出动物,人类,和和荷兰人三种类 class Animal(object): def __init__(self, ...

  5. Flask初学者:蓝图Blueprint

    蓝图这个名字好像就是根据单词Blueprint字面意思来,跟平常我们理解的蓝图完全挂不上钩,这里蓝图就是指Blueprint. 使用蓝图的好处是可以将不同功能作用的视图函数/类视图放到不同的模块中,可 ...

  6. 输入cin对象的用法

    #include<iostream> using namespace std; int main() { int carrots ; cout << "How man ...

  7. Echarts 背景渐变柱状图

    var dom = document.getElementById("container"); var myChart1 = echarts.init(dom); var app ...

  8. mysql学习第三天练习(日期和时间函数)

    -- 日期和时间函数 -- 获取当前日期 select curdate(),current_date() from dual -- 返回服务器当前的日期和时间 select NOW(),SYSDATE ...

  9. 6 Django的视图层

    视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. . ...

  10. Kafka消费分组和分区分配策略

    Kafka消费分组,消息消费原理 同一个消费组里的消费者不能消费同一个分区,不同消费组的消费组可以消费同一个分区 Kafka分区分配策略 在 Kafka 内部存在两种默认的分区分配策略:Range 和 ...