乘法逆元(拓展欧几里得)
逆元的定义
c++
#include <iostream>
#include <stdio.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
#include<iomanip>
#include <string>
#include<limits.h>
using namespace std;
#define ll long long
ll a, b;
ll exgcd(ll a, ll b, ll& x, ll& y)
{
if (b == 0)
{
y = 0;
x = 1;
return a;
}
ll x1, y1;
ll k = exgcd(b, a % b, x1, y1);
x = y1;
y = x1 - a / b * y1;
return k;
}
int main()
{
cin >> a >> b;
ll x, y;
exgcd(a, b, x, y);
cout << (x % b + b) % b << endl;
return 0;
}