挺水的一题。其实只要理解了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的更多相关文章

  1. POJ推荐50题

    此文来自北京邮电大学ACM-ICPC集训队 此50题在本博客均有代码,可以在左侧的搜索框中搜索题号查看代码. 以下是原文: POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求, ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  9. 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 ...

随机推荐

  1. windows系统中软件开发常用的软件

    1.windwos快速打开控制面板:热键+r打开运行框,输入control就打开windows的控制面板了 2.windows自带的远程桌面控制系统:mstsc -Microsoft terminal ...

  2. Python Study (05)装饰器

    装饰器(decorator)是一种高级Python语法.装饰器可以对一个函数.方法或者类进行加工.在Python中,我们有多种方法对函数和类进行加工,比如在Python闭包中,我们见到函数对象作为某一 ...

  3. ios除去可变字符串中的某些字符

    //除去字符串中的"111@" NSMutableString *str = [[NSMutableString alloc]initWithFormat:@"111@s ...

  4. ACM-SG函数之Fibonacci again and again——hdu1848

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  5. 0x31 质数

    poj2689 算根号R的质数,然后把L~R区间(这个很小啊)的合数判下 #include<cstdio> #include<iostream> #include<cst ...

  6. 0x27 A*

    终于完全了解A*到底是什么玩意儿了 对于当前的决策,选取当前花费+预估花费最小来拓展. 因为假如预估出现失误,那么很可能就会延伸到一个错误的决策点,而这个决策点偏偏就是ed,而由于预估失误,其他点的当 ...

  7. 0x08 总结与练习

    1:前面已经搞好了. 2:poj2965 这种开关问题一个点要么点一次要么不点,枚举所有点的方案实行即可 #include<cstdio> #include<iostream> ...

  8. dns tunnel C&C

    通过DNS控制主机以及执行命令 我的ubuntu 安装过程 1854 mkdir dns_tunnel_tool 1855 cd dns_tunnel_tool/ 1856 ls 1857 git c ...

  9. PySide2运行出错问题解决

    PySide2是QT官方出的Python的QT封装, 不过默认安装运行时候会有一些小问题, 可能是系统里已经安装过其他版本QT的原因, 会报错如下: PySide2 qt.qpa.plugin: Co ...

  10. 关于content-type

    content-type 包含了表单类型和边界字符串信息. 关于content-type get请求的headers中没有content-type这个字段 post 的 content-type 有两 ...