题意:两个素数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的更多相关文章

随机推荐

  1. Xdebug原理

    前言: 前面一篇博文记录了Xdebug的安装配置.配置使用起来相对简单易懂,那么Xdebug的实现原理又是如何呢?所以就找了些资料来理解下其中的原理. 内容: Xdebug工作原理 1,IDE(如Ph ...

  2. c# winform 为按钮动态背景图片

    参考自:http://www.cnblogs.com/sufei/archive/2012/11/15/2771299.html 第一种,使用Properties.Resources类,这种方法需要你 ...

  3. 【刷题】BZOJ 3512 DZY Loves Math IV

    Description 给定n,m,求 模10^9+7的值. Input 仅一行,两个整数n,m. Output 仅一行答案. Sample Input 100000 1000000000 Sampl ...

  4. 【题解】 bzoj1191: [HNOI2006]超级英雄Hero (二分图)

    bzoj1191,懒得复制,戳我戳我 Solution: 二分图最大匹配板子题 Attention: 注意题干中的一句话 只有当选手正确回答一道题后,才能进入下一题,否则就被淘汰. Code: //I ...

  5. Spring点滴十一:Spring中BeanFactoryPostProcessor和BeanPostProcessor区别

    Spring中BeanFactoryPostProcessor和BeanPostProcessor都是Spring初始化bean时对外暴露的扩展点.两个接口从名字看起来很相似,但是作用及使用场景却不同 ...

  6. Flash 解题报告

    Flash Description 给你一颗树,需要把每个点染色,每个点染色时间为\(t_i\),要求同时染色的点的集合为树的独立集,最小化染色结束时间之和. 其实题面蛮有趣的♂ HINT \(n\l ...

  7. 解题:APIO 2018 铁人两项

    题面 建立圆方树,考虑所有路径,发现路径上原来的点双(现在的方点)里的点都可以做中间点.但是路径上被方点夹着的圆点被计重了,要扣掉:枚举的两个端点也被算进去了,要扣掉.所以直接将方点权值设为点双大小, ...

  8. LeetCode 4.反转整数

    给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: ...

  9. Python学习笔记 - 实现探测Web服务质量

    #!/usr/bin/python3# _*_ coding:utf-8 _*_import sys, osimport timeimport pycurl url = "https://d ...

  10. P2513 [HAOI2009]逆序对数列

    P2513 [HAOI2009]逆序对数列 题目描述 对于一个数列{ai},如果有iaj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的数列,可以很容易求出有多少个逆序对数.那 ...