传送门--乘法逆元
模板题
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int exgcd(int a, int b, int &x, int &y) {
if (!b) {
x = 1, y = 0;
return a;
}
int t, ret;
ret = exgcd(b, a%b, x, y);
t = x, x = y, y = t - a / b*y;
return ret;
}
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)!=-1)
{
int x,y;
exgcd(a,b,x,y);
while(x<=0)
{
x+=b;
y-=a;
}
cout<<x<<endl;
}
return 0;
}