EXGCD
ax≡1( mod b) 等价于 ax+by=1 。那么题目就转化成求这个表达式x的最小值。
可以把1看成gcd(a,b),于是就变成求解EXGCD了。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL a,b;
int exgcd(int a,int &x,int b,int &y){
if (b==0){
x=1; y=0; return a;
}
int r=exgcd(b,x,a%b,y);
int k=x; x=y; y=k-a/b*y;
return r;
}
int main(){
scanf("%d%d",&a,&b);
int x,y;
exgcd(a,x,b,y);
printf("%d\n",(x+b)%b);
return 0;
}