如果有多个解,输出x为非负数最小的解。。。。
扩展欧几里得+线性同余
注意long long
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
long long int exgcd(long long int a, long long int b, long long int &x, long long int &y)
{
if(b==0)
{
x = 1;
y = 0;
return a;
}
else
{
long long int r = exgcd(b, a%b, x, y);
long long int t = x;
x = y;
y = t - a/b*y;
return r;
}
}
int main()
{
long long int a, b;
while(~scanf("%lld %lld", &a, &b))
{
long long int x, y;
long long int k = exgcd(a, b, x, y);
if(k==1)
{
x = x / k;
long long int mod = b/k;
x = (x%mod+mod)%mod;
long long int y = (1-a*x)/b;
printf("%lld %lld\n", x, y);
}
else
printf("sorry\n");
}
return 0;
}