蒙哥马利幂模运算
来源:互联网 发布:常见fps游戏的端口 编辑:程序博客网 时间:2024/04/28 13:53
蒙哥马利模乘的优点在于减少了取模的次数(在大数的条件下)以及简化了除法的复杂度(在2的k次幂的进制下除法仅需要进行左移操作)。模幂运算是RSA 的核心算法,最直接地决定了RSA 算法的性能。
针对快速模幂运算这一课题,西方现代数学家提出了大量的解决方案,通常都是先将幂模运算转化为乘模运算。
例如求D=C^15%N
由于:a*b % n = (a % n)*(b % n) % n
所以令:
C1 =C*C % N =C^2 % N
C2 =C1*C % N =C^3 % N
C3 =C2*C2 % N =C^6 % N
C4 =C3*C % N =C^7 % N
C5 =C4*C4 % N =C^14 % N
C6 =C5*C % N =C^15 % N
int Monto(int a,int b,int c){int ans = 1;while(b){if(b&1) ans = (ans*a)%c;b>>=1;a =(a*a)%c;}return ans;}
例如HDU 1395
2^x mod n = 1
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15203 Accepted Submission(s): 4699
Problem Description
Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.
Input
One positive integer on each line, the value of n.
Output
If the minimum x exists, print a line with 2^x mod n = 1.
Print 2^? mod n = 1 otherwise.
You should replace x and n with specific numbers.
Print 2^? mod n = 1 otherwise.
You should replace x and n with specific numbers.
Sample Input
25
Sample Output
2^? mod 2 = 12^4 mod 5 = 1
Author
MA, Xiao
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;int Monto(int a,int b,int c){int ans = 1;while(b){if(b&1) ans = (ans*a)%c;b>>=1;a =(a*a)%c;}return ans;}int main(){int i,j,m,n,g,t,d,k;int cnt,a,b,c;while(cin>>n){if(n%2==0 || n==1){cout<<"2^? mod "<<n<<" = 1"<<endl;}else{for(i=1;;i++)if(Monto(2,i,n)==1){cout<<"2^"<<i<<" mod "<<n<<" = 1"<<endl;break;}}}}
虽然这样可以AC,但是,挺耗时的,还不如暴力的循环
0 0
- 蒙哥马利幂模运算
- 蒙哥马利幂模运算
- HDU 1420 蒙哥马利幂模算法
- 蒙哥马利算法-快速幂模
- 蒙哥马利快速幂摸算法
- 蒙哥马利
- Montgomery蒙哥马利算法
- 蒙哥马利算法简介
- 蒙哥马利算法详解
- 快速幂模-蒙哥马利-递推-HDU4506-HDU1211-HDU1575
- 【洛谷1965】转圈游戏 蒙哥马利快速幂模算法
- 算法与数据结构-数论之蒙哥马利模乘
- 蒙哥马利(Montgomery)算法简介
- 蒙哥马利(Montgomery)算法简介
- 蒙哥马利(Montgomery)算法简介
- Hdu 4506 小明系列故事——师兄帮帮忙 +Hdu 1420 (蒙哥马利幂模算法)
- a^b%c(蒙格马利快速幂模算法)
- 费马小定理 素数判定 蒙哥马利算法
- 生产订单结算KKS1常见错误
- 一个8583报文的简单例子
- Android DNK开发错误记录
- 如何获得button.tag值?
- PAT1020月饼
- 蒙哥马利幂模运算
- 78. Subsets
- http和socket简介
- 线程钩子
- linux服务器TCP并发连接数优化
- TCP/IP socket编程
- layout.setBackgroundColor(color),直接在colorl里面设置透明度
- 为什么类中的线程函数必须要声明静态?
- PAT1021个位数统计