【题目链接】:​​click here~~​

【题目大意】:

Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.

求满足方程X*a + Y*b = 1.的解

【思路】:

通过扩展欧几里得算法求得x,y之后,要求所有的x和y的解的话,得求得通项公式:(x+k*gx , y-k*gy) gx= b/gcd(a,b),gy = a/gcd(a,b)互素,k为任意整数,即为答案

代码:

/* 
* Problem: HDU No.2669
* Running time: 15MS
* Complier: C++
* Author: javaherongwei
* Create Time: 20:25 2015/9/2 星期三
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a,b,c;
LL ext_gcd(LL &a,LL &b,LL n,LL m)
{
if(m==0)
{
a=1;
b=0;
return n;
}
LL d=ext_gcd(b,a,m,n%m);
b-=n/m*a;
return d;
}
int main()
{
while(scanf("%lld %lld",&a,&b)!=EOF)
{
LL x,y,g;
g=ext_gcd(x,y,a,b);
if(g==1){
while(x<0)
{
x+=b/1;
y-=a/1;
}
printf("%lld %lld\n",x,y);
}
else puts("sorry");
} return 0;
}