同余方程(扩欧模板)

洛咕

(axequiv1pmod{b})的最小正整数解.

((x_0mod b+b)mod b).

#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline LL read(){
   LL s=0,w=1;char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
   return s*w;
}
LL exgcd(int a,int b,LL &x,LL &y){
    if(b==0){x=1;y=0;return a;}
    int d=exgcd(b,a%b,y,x);
    y-=x*(a/b);
    return d;
}
int main(){
    LL a=read(),b=read(),x,y;
    exgcd(a,b,x,y);
    printf("%lld
",(x%b+b)%b);
    return 0;
}