hdu 1211 RSA (逆元)
RSA
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1243 Accepted Submission(s): 901
> 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.
//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 (逆元)的更多相关文章
- hdu 1211 RSA
// 表示题目意思我是理解了蛮久 英语太水了 //首先这是解密公式 m=c^d mod n// 给你 p q e 然后 n=p*q fn=(p-1)*(q-1)// 给你 e,根据公式 e*d mod ...
- 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$ ...
- hdu 1211 逆元
RSA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- HDU 1576 (乘法逆元)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1576 题目大意:求(A/B)mod 9973.但是给出的A是mod形式n,n=A%9973. 解题思 ...
- HDU 5651 组合+逆元
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5651 题目意思我看了半天没读懂,一直以为是回文子串又没看见substring的单词最后看博客才知道是用给 ...
- hdu 1576 求逆元
题意:给出n=A mod 9973和B,求(A/B) mod 9973 昨天用扩展欧几里得做过这题,其实用逆元也可以做. 逆元的定义:例如a*b≡1 (mod m),则b就是a关于m的逆元. 求逆元方 ...
- HDU 1211
水.模拟即可.使用EXGCD求逆元 #include <iostream> #include <cstdio> #include <cstring> #includ ...
- HDU 5976 数学,逆元
1.HDU 5976 Detachment 2.题意:给一个正整数x,把x拆分成多个正整数的和,这些数不能有重复,要使这些数的积尽可能的大,输出积. 3.总结:首先我们要把数拆得尽可能小,这样积才会更 ...
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
随机推荐
- Python 统计不同url svn代码变更数
#!/bin/bash/python # -*-coding:utf-8-*- #svn统计不同url代码行数变更脚本,过滤空行,不过滤注释. import subprocess,os,sys,tim ...
- 文件权限管理命令chmod,chown与文本搜索命令grep
1.复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限. [root@bogon home]# cp -r /etc/sk ...
- 转:2018最全Redis面试题整理
Java面试----2018最全Redis面试题整理 1.什么是Redis? 答:Redis全称为:Remote Dictionary Server(远程数据服务),是一个基于内存的高性能key-va ...
- JAVA / MySql 编程—— 第三章 高级查询(一)
1. 修改表: (1)修改表名语法: ALTER TABLE <旧表名> RENAME [ TO ] <新表名>: 注意:其中[TO]为可选参数,使用与否不影响结 ...
- 【PHP项目】【Smarty】Smarty截取字符串方法truncate
smarty truncate 截取字符串 //在sql中truncate是删除表格(truncate是只删除内容,delete彻底删除)从字符串开始处截取某长度的字符,默认的长度为80指定第二个参 ...
- python -- 输出异常详细信息
在使用try: except: 捕获异常后,想要获取到异常信息的详细内容另做它用,可以使用python的内置模块traceback进行获取. traceback.print_exc() 直接打印异 ...
- pytorch中词向量生成的原理
pytorch中的词向量的使用 在pytorch我们使用nn.embedding进行词嵌入的工作. 具体用法就是: import torch word_to_ix={'hello':0,'world' ...
- Git-补丁文件交互
版本库间的交互是通过git push和/或git pull命令实现的,这是Git最主要的交互模式,但并不是全部.使用补丁文件是另外一种交互方式,适用于参与者众多的大型项目进行分布式开发. 创建补丁 G ...
- DOM事件里封装方法eventUtil
var eventUtil={ //添加句柄 addHandler:function (element,type,handler) { //element相当于btn2,type此时用的是click类 ...
- 如何创造财富?硅谷创业之父 Paul Graham 《黑客与画家》思维导图
先送上亚马逊传送门:<黑客与画家>:硅谷创业之父 Paul Graham 文集 再送上一个思维导图: 下载大图:http://caifujianghu.com/article/ruhe-c ...