求关于x的同余方程ax≡1(modb)的最小正整数解。
用拓展欧几里得定理求解即可,证明参考:https://zhuanlan.zhihu.com/p/103410252
代码:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 10;
const int mod  = 1e9 + 7;
ll exgcd(ll a,ll b,ll &x,ll &y){
    if(!b){
        x = 1;
        y = 0;
        return a;
    }
    ll d = exgcd(b,a%b,y,x);
    y -= a/b * x;
    return d;
}
void solve(){
    ll a,b;
    cin>>a>>b;
    ll x = 0,y = 0;
    ll d = exgcd(a,b,x,y);
    cout<<(x+b)%b<<endl;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    //cin>>t;
    while(t--){
        solve();
    }
    return 0 ;
}