求逆元—穷举、扩展Euclid法
方法1:穷举
#include<iostream> using namespace std; int main(){ int m = 123,i;//求11mod123的逆元 for (i = 2; (11*i-1)%123!=0; i++); cout << i; system("pause"); return 0; }
方法2:扩展Eulide
int Moni(int p,int q) { int s = 1, t = 0; int a = p, mod = q, tem; int z = mod / a; while (1 != a && 1 != mod) { tem = a; a = mod % a; mod = tem; tem = s; s = t- s * z; t = tem; z = (int)mod / a; } s = s % q; if (s < 0) s += mod; return s; }
方法3:利用Euler定理
推荐文章