POJ 2447
挺水的一题。其实只要理解了RSA算法,就知道要使用大整数分解的方法来直接模拟了。
不过,要注意两个INT64的数相乘来超范围
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#define LL __int64
using namespace std; LL e,n,c,p,q,f;
int cnt;
LL prime[10]; LL gcd(LL a,LL b){
if(b==0) return a;
return gcd(b,a%b);
} LL random(LL nc){
return (LL)((double)rand()/RAND_MAX*nc+0.5);
} LL multi(LL a,LL b,LL m){
LL ret=0;
while(b>0){
if(b&1)
ret=(ret+a)%m;
b>>=1;
a=(a<<1)%m;
}
return ret;
} LL quick(LL a,LL b,LL m){
LL ans=1;
a%=m;
while(b){
if(b&1)
ans=multi(ans,a,m);
b>>=1;
a=multi(a,a,m);
}
return ans;
} LL witness(LL a, LL nc){
LL m=nc-1;
int j=0;
while(!(m&1)){
j++;
m>>=1;
}
LL x=quick(a,m,nc);
if(x==1||x==nc-1)
return false;
while(j--){
x=multi(x,x,nc);
if(x==nc-1)
return false;
}
return true;
} bool miller_rabin(LL nc){
if(nc<2) return false;
if(nc==2) return true;
if(!(nc&1)) return false;
for(int i=1;i<=10;i++){
LL a=random(nc-2)+1;
if(witness(a,nc)) return false;
}
return true;
} LL pollard_rho(LL nc,int inc){
LL x,y,d,i=1,k=2;
x=random(nc-1)+1;
y=x;
while(1){
i++;
x=(multi(x,x,nc)+inc)%nc;
d=gcd(y-x,nc);
if(d>1&&d<nc)
return d;
if(y==x)
return nc;
if(i==k){
y=x;
k=(k<<1);
}
}
} bool find(LL nc,int k){
if(nc==1)
return false;
if(miller_rabin(nc)){
p=nc;
return true;
}
LL pe=nc;
while(pe>=nc)
pe=pollard_rho(pe,k--);
if(find(pe,k)) return true;;
if(find(nc/pe,k)) return true;;
} void exgcd(LL a,LL b,LL &x,LL &y){
if(b==0){
x=1; y=0;
return ;
}
exgcd(b,a%b,x,y);
LL tmp=x;
x=y;
y=tmp-a/b*y;
} int main(){
LL x,y;
while(scanf("%I64d%I64d%I64d",&c,&e,&n)!=EOF){
srand(time(0));
cnt=0;
find(n,201);
q=n/p;
f=(p-1)*(q-1);
exgcd(e,f,x,y);
x=(x%f+f)%f;
LL ans=quick(c,x,n);
printf("%I64d\n",ans);
}
return 0;
}
POJ 2447的更多相关文章
- POJ推荐50题
此文来自北京邮电大学ACM-ICPC集训队 此50题在本博客均有代码,可以在左侧的搜索框中搜索题号查看代码. 以下是原文: POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求, ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
随机推荐
- 【转】C# ArcgisEngine开发中,对一个图层进行过滤,只显示符合条件的要素
有时候,我们要对图层上的地物进行有选择性的显示,以此来满足实际的功能要求. 按下面介绍的方法可轻松实现图层属性过滤显示: 1.当图层已经加载时 private void ShowByFilter(Ax ...
- CC2540 与 CC2541 差别 1
CC2540 的 1234 PIN 是 USB 功能,4 PIN 是 USB 的电压输入引脚. CC2541 没有 USB 功能.它的 1234 PIN 是 I2C 功能,为了与 CC2540 引脚兼 ...
- CF 372B Counting Rectangles is Fun [dp+数据维护]
题意,给出一个n行m列的矩阵 里面元素是0或者1 给出q个询问 a,b,c,d 求(a,b)到(c,d)有多少个由0组成的矩形 我们定义 watermark/2/text/aHR0cDovL2Jsb2 ...
- HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告
题目链接:pid=1312" target="_blank">HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red ...
- 石子合并(区间dp)
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程仅仅能每次将相邻 ...
- 开发效率必备之Mac双屏显示
自从2015年9月苹果公布EI Captain,带来了一个新的功能,叫做分屏,也就是在一块屏幕上分成左右两部分,能够分别进行操作,互不影响. 例如以下图所看到的: watermark/2/text/a ...
- 杂项-Java:Shiro(Java安全架构)
ylbtech-杂项-Java:Shiro(Java安全架构) Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以 ...
- shell脚本创建和执行
shell脚本并不能作为正式的编程语言,因为它是在Linux的shell中运行的,所以称他为shell脚本. 事实上,shell脚本就是一些命令的集合. 我们不妨吧所有的操作都记录到一个文档中,然后去 ...
- Spring学习笔记(一) 简介
版权声明 本文是摘自IBM上Naveen Balani的一篇文章,原文请点击此处:http://www.ibm.com/developerworks/cn/java/wa-spring1/ Sprin ...
- net 线程挂起
2013.10.18 通讯组件开发 情景: 主线程添加队列,子线程负责队列中消息发送.当队列中数据为空时,停止发送挂起子线程. 当主线程添加队列时,重新开启子线程进行消息发送. 方案一 但是不采用传 ...