日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Modular Multiplicative Inverse(模乘逆元)

發布時間:2025/7/14 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Modular Multiplicative Inverse(模乘逆元) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

計算模乘逆元原理上有四種方法:

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

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Modular Multiplicative Inverse(模乘逆元)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 成人两性视频 | 杏导航aⅴ福利网站 | 秋葵视频污 | 国产亚洲黄色片 | 国产精品无码人妻一区二区在线 | 婷婷午夜| 射精一区二区 | 久久人人干 | 日本五十肥熟交尾 | 一级二级三级黄色片 | 性生活一区 | 国产精品分类 | 欧美性天堂 | 欧美成人aaaaa | 中文字幕在线一区二区三区 | 亚洲精品一区二区三区在线 | 久久久精品91 | 国产精品一卡二卡在线观看 | 免费超碰在线观看 | 夜夜操夜夜骑 | 91丨九色丨蝌蚪丨对白 | 亚洲资源站 | 亚洲人屁股眼子交1 | 天堂av免费观看 | 亚洲666 | 一区二区自拍偷拍 | xxxx少妇| 男男全肉变态重口高h | 色香影视| 亚洲少妇色 | www天堂在线| 在线免费观看污片 | 蜜臀久久99精品久久一区二区 | 国产手机在线视频 | 婷婷五月小说 | 自拍偷拍亚洲视频 | 色二区| 黄色裸体视频 | 国产激情久久久久久熟女老人av | 游戏涩涩免费网站 | 人妻与黑人一区二区三区 | 色原网 | 久久国产精品久久国产精品 | 啦啦啦av | 国产激情视频在线 | 日韩中文字幕一区二区三区 | 免费久久久久久 | 日本韩国中文字幕 | 妻色成人网 | 亚洲女人毛片 | 亚洲片国产一区一级在线观看 | 中文字幕在线观看91 | 95在线视频 | 亚洲不卡视频在线观看 | 女同av在线 | 色老头综合 | 精产国产伦理一二三区 | 亚洲AV成人无码久久 | 日韩在线高清视频 | 成人在线三级 | 日本高清视频一区 | 一区二区一级片 | 美国免费高清电影在线观看 | 玖玖精品在线 | 7799精品视频天天看 | 成全世界免费高清观看 | 欧美性xxxxxx| 精品免费一区二区三区 | 水蜜桃亚洲精品 | 成人毛片在线视频 | 四虎最新网址在线观看 | 久久婷婷av | 91免费黄色 | 麻豆国产一区二区 | 欧美三级免费 | 国产视频一区在线 | 欧美黄色大片网站 | 亚洲熟女乱色综合亚洲小说 | 91中文字幕在线播放 | 日本美女交配 | 欧美一区二区三区黄色 | 欧美国产在线观看 | 国产精品8| 女生的胸无遮挡 | 做暧暧视频在线观看 | 国产剧情在线观看 | 自拍偷拍日韩 | 欧美一区二区三区激情视频 | 中文字幕日韩人妻在线视频 | 欧美二区视频 | 草草视频在线观看 | 久久国产日韩欧美 | 国产18照片色桃 | 亚洲成人免费影院 | 在线看片你懂 | 手机看片福利在线 | 久久免费国产精品 | 久久这里只有精品8 | 四虎视频 |