计算模乘逆元原理上有四种方法:

1.暴力算法

2.扩展欧几里得算法

3.费尔马小定理

4.欧拉定理

模乘逆元定义:满足 ab≡1(mod m),称b为a模乘逆元。以下是有关概念以及四种方法及程序。

文章出处:Modular Multiplicative Inverse

The modular multiplicative inverse of an integer a modulo m is an integer x such that

That is, it is the multiplicative inverse in the ring of integers modulo m. This is equivalent to

1. Brute Force
We can calculate the inverse using a brute force approach where we multiply a with all possible valuesx and find ax such that Here’s a sample C++ code:

int modInverse(int a, int m) {a %= m;for(int x = 1; x < m; x++) {if((a*x) % m == 1) return x;}
}

2. Using Extended Euclidean Algorithm
We have to find a number x such that a·x = 1 (mod m). This can be written as well as a·x = 1 + m·y, which rearranges into a·x – m·y = 1. Since x and y need not be positive, we can write it as well in the standard form, a·x + m·y = 1.

Iterative Method

/* This function return the gcd of a and b followed bythe pair x and y of equation ax + by = gcd(a,b)*/
pair<int, pair<int, int> > extendedEuclid(int a, int b) {int x = 1, y = 0;int xLast = 0, yLast = 1;int q, r, m, n;while(a != 0) {q = b / a;r = b % a;m = xLast - q * x;n = yLast - q * y;xLast = x, yLast = y;x = m, y = n;b = a, a = r;}return make_pair(b, make_pair(xLast, yLast));
}int modInverse(int a, int m) {return (extendedEuclid(a,m).second.first + m) % m;
}

Recursive Method

/* This function return the gcd of a and b followed bythe pair x and y of equation ax + by = gcd(a,b)*/
pair<int, pair<int, int> > extendedEuclid(int a, int b) {if(a == 0) return make_pair(b, make_pair(0, 1));pair<int, pair<int, int> > p;p = extendedEuclid(b % a, a);return make_pair(p.first, make_pair(p.second.second - p.second.first*(b/a), p.second.first));
}int modInverse(int a, int m) {return (extendedEuclid(a,m).second.first + m) % m;
}

3. Using Fermat’s Little Theorem
Fermat’s little theorem states that if m is a prime and a is an integer co-prime to m, thenap − 1 will be evenly divisible by m. That is or Here’s a sample C++ code:

/* This function calculates (a^b)%MOD */
int pow(int a, int b, int MOD) {
int x = 1, y = a;while(b > 0) {if(b%2 == 1) {x=(x*y);if(x>MOD) x%=MOD;}y = (y*y);if(y>MOD) y%=MOD;b /= 2;}return x;
}int modInverse(int a, int m) {return pow(a,m-2,m);
}

4. Using Euler’s Theorem
Fermat’s Little theorem can only be used if m is a prime. If m is not a prime we can use Euler’s Theorem, which is a generalization of Fermat’s Little theorem. According to Euler’s theorem, if a is coprime to m, that is, gcd(a, m) = 1, then, where where φ(m) is Euler Totient Function. Therefore the modular multiplicative inverse can be found directly:. The problem here is finding φ(m). If we know φ(m), then it is very similar to above method.

vector<int> inverseArray(int n, int m) {vector<int> modInverse(n + 1,0);modInverse[1] = 1;for(int i = 2; i <= n; i++) {modInverse[i] = (-(m/i) * modInverse[m % i]) % m + m;}return modInverse;
}

转载于:https://www.cnblogs.com/tigerisland/p/7564860.html

Modular Multiplicative Inverse(模乘逆元)相关推荐

  1. Modular multiplicative inverse 模逆元

    https://en.wikipedia.org/wiki/Modular_multiplicative_inverse https://zh.wikipedia.org/wiki/%E6%A8%A1 ...

  2. 扩展欧几里得算法与模乘逆元的程序

    代码来自维基百科的Extended Euclidean algorithm. 扩展欧几里得算法程序: function extended_gcd(a, b)s := 0; old_s := 1t := ...

  3. J - Just Multiplicative Inverse Gym - 102875J

    J - Just Multiplicative Inverse Gym - 102875J 题目: 题解: 给定一个x,求出F(1,x)+F(2,x)+-+F(x-1,x) 的和除以(x-1) F(x ...

  4. c语言中欧几里得模乘法逆元,扩展欧几里得算法同余方程模m乘法逆元详解

    欧几里德算法: 复习:求最大公约数算法(欧几里得算法.也叫辗转相除法).欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd ...

  5. 除法取模与逆元/费马小定理

    对于正整数和,如果有,那么把这个同余方程中的最小正整数解叫做模的逆元. 逆元一般用扩展欧几里得算法来求得,如果为素数,那么还可以根据费马小定理得到逆元为.(都要求a和m互质) 推导过程如下(摘自Acd ...

  6. 辗转相除法求模的逆元

    最近研究RSA算法,发现在这个算法里,实现过程中的核心就是求出密钥D,求密钥的公式: E*D ≡ 1 mod r ,现在已知了E和r,求E即是一个求模的逆元问题. 注:≡是数论中表示同余的符号.公式中 ...

  7. zcmu-1934(卡特兰数大数取模(逆元))

    1934: ly的二叉树 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 42  Solved: 9 [Submit][Status][Web Boar ...

  8. BC div2补题以及 复习模除 逆元__BestCoder Round #78 (div.2)

    第一题没话说 智商欠费 加老柴辅导终于过了 需要在意的是数据范围为2的63次方-1 三个数相加肯定爆了 四边形的定义 任意边小于其余三边之和 换句话说就是 最长边小于其余三边之和 这样的话问题转化为 ...

  9. 阿里云 超级码力在线编程大赛初赛 第2场 题目4. 小栖的金字塔(超级卡特兰数+除法求模/乘法逆元)

    文章目录 1. 题目 2. 解题 1. 题目 来源:https://tianchi.aliyun.com/oj/15165469968503404/76745683739284070 2. 解题 按道 ...

最新文章

  1. 关于apache kylin 安装32位linux办法
  2. sql中left join后用on还是where
  3. linux awk数组使用
  4. 【快速、批量】修改图片格式
  5. 本特利3300XL 25mm前置器 330780-50-CN
  6. Utils 前端随机生成id,中文姓名
  7. Android APP自动升级安装失败
  8. 金士顿U盘被写保护的解决方法(量产)
  9. Unity3D游戏开发之游戏模型制作:机器人
  10. android 耗时分析,启动耗时分析(四)-具体方法耗时分析
  11. 32岁了学python来的及吗_你要悄悄的学Python,然后惊艳所有人,后来都学的怎么样呢?...
  12. MFC实现弹出模态对话框和非模态对话框(基于对话框)
  13. ArcGIS干货教程:DEM数字高程模型数据的生成
  14. Ubuntu系统安装LAMP应用Discuz 建设论坛网站
  15. 赛宁网安荣获国贸集团2022网络安全演练活动“优秀保障奖”
  16. 爵士乐里全用13和弦吗?_用微妙的视差爵士化静态网页
  17. 微信小程序获得用户头像昵称调整(2022年9月28日修改)
  18. php 右下脚弹窗,JavaScript实现右下角弹出提示框的方法
  19. html5+css 三列布局
  20. jquery批量上传图片 java_简单多图片上传 jquery+java 代码

热门文章

  1. kafka 重复消费和数据丢失_刨根问底,Kafka消息中间件到底会不会丢消息
  2. Google Capture The Flag 2018 (Quals) - Beginner's Quest - Reverse - Firmware
  3. Shell--shell中的判断
  4. deepin配置反向代理映射本地到公网
  5. easyui dialog 中嵌入html页面
  6. Docker 安装私有镜像库的简单使用
  7. 解决margin-top没有效果
  8. 老子《道德经》第四十一章
  9. Beaglebone Back学习七(URAT串口测试)
  10. 计算机:2014年考研大纲解析之数据结构