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

推荐文章 https://www.cnblogs.com/ZhouL3777/archive/2012/12/30/2839702.html

https://www.cnblogs.com/zishu/p/8650214.html

上一篇:数组的练习


下一篇:多校NOIP18