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 ...
随机推荐
- HDU2188(巴什博奕)
悼念512汶川大地震遇难同胞--选拔志愿者 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Android开发之——编码规范
1. 前言 这份文档参考了 Google Java 编程风格规范和 Google 官方 Android 编码风格规范.该文档仅供参考,只要形成一个统一的风格,见量知其意就可. 2. 源文件基础 2.1 ...
- java 微信server录音下载到自己server
/** * @author why * */ public class VoiceDownload { /** * * 依据文件id下载文件 * * * * @param mediaId * * 媒体 ...
- Android中XML解析,保存的三种方法
简单介绍 在Android开发中,关于XML解析有三种方式,各自是: SAX 基于事件的解析器.解析速度快.占用内存少.非常适合在Android移动设备中使用. DOM 在内存中以树形结构存放,因此检 ...
- VC 对话框程序加入工具栏button图标及其buttontooltip
注意:本人使用VC++2010开发环境进行測试. 在使用VC开发对话框程序时不像开发单文档程序和多文档程序那么方便,非常多资源都须要自己手动加入.近期在开发一个程序时.想尝试在对话框程序里面加入 工具 ...
- 2015.04.21,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 32
TEASER PREVIEW 如何描述这些人: 很友好.容易相处的人: 不知疲倦的人: 简单.直率..aboveboard([ə'bʌv'bɔ:d] adv. 光明正大地, 率直地 adj. 光明正大 ...
- 2015.03.12,外语,读书笔记-《Word Power Made Easy》 10 “如何讨论交谈习惯”学习笔记 SESSION 25
1.about keeping one's mouth shut taciturn,名词形式taciturnity,沉默寡言. 美国第30任总统库里奇,以沉默寡言著称.他来自新英格兰,那里视tacit ...
- Rsync 服务器搭建
Rsync简介 rsync 是一个 Unix 系统下的文件同步和传输工具. 它具备以下特性: 1. 能更新整个目录和树和文件系统 2. 有选择性的保持符号链链.硬链接.文件属于.权限.设备以及时间 等 ...
- 将百度百科的机器学习词条中的一段关于机器学习的demo改用Java写了一遍
这是引用的百度百科中关于机器学习的一段示例,讲述了通过环境影响来进行学习的例子. 下面是代码: import java.io.BufferedReader; import java.io.IOExce ...
- C语言整数类型在X86和X64下的字节大小
C声明 32位机器(X86) 64位机器(X64) char 1 1 short int 2 2 int 4 4 long int 4 8 long long int 8 8 char * 4 8 f ...