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 ...
随机推荐
- 推荐几款VisualStudio的插件
继前几天推荐了一款转换vs插件的插件后,借着安装VS2013之际,把我比较喜欢的几个插件继续推荐一下. C# Outline 2013 2013 C#的代码折叠最小只能到函数级,不像C++那样可以折叠 ...
- HDU 3306
先转一些 http://www.cnblogs.com/frog112111/archive/2013/05/19/3087648.html Fibonacci数列:F(0)=1 , F(1)=1 ...
- ubuntu中写一个shell脚本的过程
gedit hello.sh ,然后输入 #!/bin/bash echo "Hello world!" chmod +x hello.sh ./hello.sh
- poj 1182 食物链 && nyoj 207(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 52414 Accepted: 15346 Description ...
- 技术总结--android篇(一)--MVC模式
先介绍下MVC模式:MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显 ...
- 每一个程序猿都须要了解的一个SQL技巧
对于数据过滤而言CHECK约束已经算是相当不错了.然而它仍存在一些缺陷,比方说它们是应用到表上面的,但有的时候你可能希望指定一条约束,而它仅仅在特定条件下才生效. 使用SQL标准的WITH CHECK ...
- 根据EXCEL模板填充数据
string OutFileName = typeName+"重点源达标率" + DateTime.Now.ToString("yyyy-MM-dd"); ...
- 英语发音规则---W字母
英语发音规则---W字母 一.总结 一句话总结: 1.W在单词开头发[w]? week [wiːk] n. 周,星期 win [wɪn] vt. 赢得 wake [weɪk] vi. 醒来 sweet ...
- Oracle多表连接效率,性能优化
Oracle多表连接,提高效率,性能优化 (转) 执行路径:ORACLE的这个功能大大地提高了SQL的执行性能并节省了内存的使用:我们发现,单表数据的统计比多表统计的速度完全是两个概念.单表统计可能只 ...
- Centos6.5添加Epel和Remi源安装Lamp环境
想搭建一个Lamp环境,因为编译安装太麻烦,对于我这样的新手来说,太过于复杂.而CentOS自带的Apache.MySql和PHP的版本都太低,不想用.上百度搜了一轮,原来可以通过添加Epel和Rem ...