poj2447
题意:两个素数P,Q。N=P*Q; T=(P-1)*(Q-1); (E*D)mod T = 1; (0<=D<T)。E与T互质,公钥是{E,N},私钥是{D,N}。原始信息M的加密过程为C=(M^E)mod N; 解密过程为 M=(C^D)mod N;("^"表示幂) 现在给出C,E,N(<2^62)。求M。
分析:先通过N分解求P,Q(pollard-rho+Miller-rabin)。通过P,Q求T,通过(E*D)mod T = 1求D(扩展欧几里德),通过M=(C^D)mod N求M。
如何使用扩展欧几里德呢,
(E*D)mod T = 1 <=> (E*D) = 1 + k*T <=> E*(D*g) + T*[(-k)*g] = g(g是T和E的最大公约数gcd(T,E))。
不过这道题好像不用这么麻烦,因为E和T互质,所以g=1。
这样就变成了a*x+b*y=gcd(a,b)的形式了。
pollard-rho和Miller-rabin算法参见poj1811解题报告 http://www.cnblogs.com/rainydays/archive/2011/09/01/2162049.html
扩展欧几里德算法参见poj1061解题报告 http://www.cnblogs.com/rainydays/archive/2013/07/19/3201618.html
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std; typedef long long LL;
#define maxn 10000
const int S=; LL factor[maxn];
int tot; LL muti_mod(LL a,LL b,LL c){ //返回(a*b) mod c,a,b,c<2^63
a%=c;
b%=c;
LL ret=;
while (b){
if (b&){
ret+=a;
if (ret>=c) ret-=c;
}
a<<=;
if (a>=c) a-=c;
b>>=;
}
return ret;
} LL pow_mod(LL x,LL n,LL mod){ //返回x^n mod c ,非递归版
if (n==) return x%mod;
int bit[],k=;
while (n){
bit[k++]=n&;
n>>=;
}
LL ret=;
for (k=k-;k>=;k--){
ret=muti_mod(ret,ret,mod);
if (bit[k]==) ret=muti_mod(ret,x,mod);
}
return ret;
} bool check(LL a,LL n,LL x,LL t){ //以a为基,n-1=x*2^t,检验n是不是合数
LL ret=pow_mod(a,x,n),last=ret;
for (int i=;i<=t;i++){
ret=muti_mod(ret,ret,n);
if (ret==&& last!=&& last!=n-) return ;
last=ret;
}
if (ret!=) return ;
return ;
} bool Miller_Rabin(LL n){
LL x=n-,t=;
while ((x&)==) x>>=,t++;
bool flag=;
if (t>=&& (x&)==){
for (int k=;k<S;k++){
LL a=rand()%(n-)+;
if (check(a,n,x,t)) {flag=;break;}
flag=;
}
}
if (!flag || n==) return ;
return ;
} LL gcd(LL a,LL b){
if (a==) return ;
if (a<) return gcd(-a,b);
while (b){
LL t=a%b; a=b; b=t;
}
return a;
} LL Pollard_rho(LL x,LL c){
LL i=,x0=rand()%x,y=x0,k=;
while (){
i++;
x0=(muti_mod(x0,x0,x)+c)%x;
LL d=gcd(y-x0,x);
if (d!=&& d!=x){
return d;
}
if (y==x0) return x;
if (i==k){
y=x0;
k+=k;
}
}
} void findfac(LL n){ //递归进行质因数分解N
if (!Miller_Rabin(n)){
factor[tot++] = n;
return;
}
LL p=n;
while (p>=n) p=Pollard_rho(p,rand() % (n-) +);
findfac(p);
findfac(n/p);
} void gcdExtend(long long a,long long b,long long &d,long long &x,long long &y)
{
if(!b) {d=a;x=;y=;return;}
gcdExtend(b,a%b,d,y,x);
y-=a/b*x;
} int main()
{
LL C, E, N, T, M, D;
LL x, y, d;
while (~scanf("%lld%lld%lld", &C, &E, &N))
{
tot = ;
findfac(N);
T = (factor[] - ) * (factor[] - );
gcdExtend(E, T, d, x, y);
D = (x % T + T) % T;
M = pow_mod(C, D, N);
printf("%lld\n", M);
}
return ;
}
poj2447的更多相关文章
随机推荐
- Elk 进阶部署
虚拟机两台: 192.168.1.42 192.168.1.46 系统环境保持一致: cat /etc/redhat-release uname -a elk准备环境保持一致: elasticsear ...
- 怎么判断pagecontrol下的TabSheet是否打开还是关闭求答案
if pagecontrol.activepage=TabSheet1 then是在当前显示页------解决方案------------------------------------------- ...
- C# 妈妈再打我一下生成器
设计背景 网上很火的一个"妈妈再打我一下"的漫画图片,给了网友无限的想象发挥空间,此小程序可以给图片添加配文的形式,快速生成图片 设计思路 GDI+ 绘图技术,在图片基础上添加文字 ...
- java 强制转换之降级
大空间的数据类型向小空间的数据类型去转换. 语法:(目标数据类型)变量名 当大空间数据类型转换给小空间时且超过小空间的容量时,编译不会出错,但是会丢失精度 例如 int a = 128 抓换byte ...
- BZOJ 3516 国王奇遇记加强版(乱推)
题意 求\(\sum_{k=1}^{n}k^mm^k (n\leq1e9,m\leq1e3)\) 思路 在<>中有一个方法用来求和,称为摄动法. 我们考虑用摄动法来求这个和式,看能不能得到 ...
- BZOJ2423 HAOI2010最长公共子序列(动态规划)
大讨论.注意去重. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib& ...
- NOI&&NOIP知识点集萃
更新日志 \(update:2019-3-4\) 更新了自为风月马前卒的后缀数组(省选不到一个月了,我才开始学后缀数组怕是要凉凉) \(update:2019-2-21\) 更新了一篇李超线段树的讲解 ...
- Benefit UVA - 11889(已知LCM和其中一个数,求另一个数)
首先对于C不能整除A的状况肯定排除 然后得到B=C/A 然后取G=GCD(A,B) 如果G==1,那么此时B就是解 否则的话,就证明A,B,的最小公倍数肯定不是C,因为其最小公倍数是A*B/G 那么我 ...
- C++实用整数快速输入输出模板(C++)
随便写一点放在这里,以后想蛇皮卡常就很方便啦 蒟蒻太懒了,也就暂时不搞什么封namespace之类的操作了 程序结束时记得flush一下. #include<cstdio> #define ...
- BZOJ 3609: [Heoi2014]人人尽说江南好
3609: [Heoi2014]人人尽说江南好 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 470 Solved: 336[Submit][Sta ...