http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1256
#include <iostream>
using namespace std;
typedef long long LL;
void exgcd(LL a, LL b, LL &x, LL &y)//扩展欧几里得算法
{
if(b == 0)
{
x=1;
y=0;
return ;
}
exgcd(b, a%b, x, y);
LL tmp=x;
x=y;
y=tmp - (a/b)*y;
}
int main()
{
LL m,n,x,y;
while(cin>>m>>n)
{
exgcd(m, n, x, y);
while(x<0)//当x<0时还要转换为正数
x+=n;
cout<<x<<endl;
}
return 0;
}