code
ax + by = m方程有解的必要条件是m % gcd(a, b) = 0。
又因为 ax + by = 1有解,所以可以推出gcd(a, b) = 1,于是可以用扩展欧几里得求得x。
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
void exgcd(int a, int b, int&x, int &y){
if (!b){
x = 1, y = 0;
return;
}
exgcd(b, a % b, y, x);
y -= (a / b) * x;
}
int a, b;
int main(){
int x, y;
cin >> a >> b;
exgcd(a, b ,x, y);
cout << (x % b + b) % b << endl;
}