模板总结归纳:
//扩展欧几里得算法
//O(logN)
/*int extend_gcd(int a, int b, int &x, int &y)
{
if(b == 0)
{
x = 1; y = 0;
return a;
}
else
{
int r = extend_gcd(b, a % b, y, x);
y -= x * (a / b);
return r;
}
}*/
实战模板题 :HDU2669 Romantic
#include<iostream>
#include<cstdio>
using namespace std;
long long extend_gcd(long long a, long long b, long long &x, long long &y)
{
if(b == 0)
{
x = 1; y = 0;
return a;
}
else
{
long long r = extend_gcd(b, a % b, y, x);
y -= x * (a / b);
return r;
}
}
int main()
{
long long a, b, x, y;
while(cin >> a >> b)
{
if(extend_gcd(a, b, x, y) == 1)
{
while (x <= 0) // 保证x非负
{
x+= b;
y-= a;
}
cout << x << " " << y << endl;
}
else
cout << "sorry" << endl;
}
return 0;
}