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 ...
随机推荐
- Cocos2dx之使用UI库结合cocostudio
使用cocostudio的UI编辑器编辑好UI界面,导出UI文件,直接在cocos2dx中使用.通过tag或者name来获取到UI控件 1.编辑ui界面,直接用模板然后拖几个控件过去 2.cocos2 ...
- cocos2D(六)----CCLayer
一个游戏中能够有非常多个场景,每一个场景里面又可能包括有多个图层,这里的图层一般就是CCLayer对象.CCLayer本身差点儿没什么功能.对照CCNode,CCLayer可用于接收触摸和加速计输入. ...
- 移植MonkeyRunner的图片对照和获取子图功能的实现-Appium篇
假设你的目标測试app有非常多imageview组成的话,这个时候monkeyrunner的截图比較功能就体现出来了. 而其它几个流行的框架如Robotium,UIAutomator以及Appium都 ...
- gemm() 与 gesvd() 到矩阵求逆(inverse)(根据 SVD 分解和矩阵乘法求矩阵的逆)
可逆方阵 A 的逆记为,A−1,需满足 AA−1=I. 在 BLAS 的各种实现中,一般都不会直接给出 matrix inverse 的直接实现,其实矩阵(方阵)的逆是可以通过 gemm()和gesv ...
- 【转】如何在Mac 终端升级ruby版本
原文网址:https://segmentfault.com/a/1190000003784636 rvm是什么?为什么要安装rvm呢,因为rvm可以让你拥有多个版本的Ruby,并且可以在多个版本之间自 ...
- BZOJ 3195 DP
http://www.cnblogs.com/CXCXCXC/p/5093584.html //By SiriusRen #include <cstdio> using namespace ...
- 算法入门经典-第六章 例题6-21 SystemDependencies
题意:软件组件之间会有依赖关系,比如你下一个Codeblocks你也得顺带着把编译器给下上.你的任务是模拟安装和卸载软件组件的过程.有以下五种指令,如果指令为“END”则退出程序:若为以下四种指令,则 ...
- MacOS系统下简单安装以及配置MongoDB数据库(一)
最近写了一个用node来操作MongoDB完成增.删.改.查.排序.分页功能的示例,并且已经放在了服务器上地址:http://39.105.32.180:3333. 项目一共四部分: 1.MacOS下 ...
- mybatis的二级缓存的使用
1.引入ehcache的jar包和mybatis整合ehcache的jar包: <!-- ehchache --> <dependency> <groupId>ne ...
- Oracle数据库基础(二)
1.表名命名规则:必须以字母开头,不能超过30个字符,不要有Oracle保留字 2.数据类型 字符型: char :2000个字符 定长 效率高 ...