求逆元—穷举、扩展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定理

推荐文章

逆元的运用—仿射密码

求逆元的详细讲解

 

posted @ 2021-10-31 00:49  Grit_L。  阅读(66)  评论(0编辑  收藏  举报