一道扩展欧几里得模板的题,这几天刚好练习一下简单数论的知识。思路:由于X*A + Y*B = 1,所以一定有gcd(A,B) = 1;
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
void ex_gcd(__int64 a, __int64 b, __int64 &d, __int64 &x, __int64 &y)
{
if(!b){ d = a; x = 1; y = 0;}
else
{
ex_gcd(b, a%b, d, y, x);
y -= x*(a/b);
}
} //ex_gcd
int main()
{
__int64 a, b;
__int64 x, y, d;
while(~scanf("%I64d%I64d", &a, &b))
{
ex_gcd(a, b, d, x, y);
if(d != 1)
{
printf("sorry\n"); continue; //存在整数解的话那么gcd(a, b)一定为1,因为一定是gcd(a, b)的整数倍
}
__int64 k = b/d; //b1/gcd(a,b);
x = (x%k+k)%k; //求满足方程的最小值 x
y = (1-x*a)/b; //y
printf("%I64d %I64d\n", x, y);
}
return 0;
}
#include <stdlib.h>
#include <string.h>
using namespace std;
void ex_gcd(__int64 a, __int64 b, __int64 &d, __int64 &x, __int64 &y)
{
if(!b){ d = a; x = 1; y = 0;}
else
{
ex_gcd(b, a%b, d, y, x);
y -= x*(a/b);
}
} //ex_gcd
int main()
{
__int64 a, b;
__int64 x, y, d;
while(~scanf("%I64d%I64d", &a, &b))
{
ex_gcd(a, b, d, x, y);
if(d != 1)
{
printf("sorry\n"); continue; //存在整数解的话那么gcd(a, b)一定为1,因为一定是gcd(a, b)的整数倍
}
__int64 k = b/d; //b1/gcd(a,b);
x = (x%k+k)%k; //求满足方程的最小值 x
y = (1-x*a)/b; //y
printf("%I64d %I64d\n", x, y);
}
return 0;
}