您的位置:首页 > 其它

HDU 2669 Romantic(扩展欧几里德)

2016-05-31 01:14 295 查看
题目链接:

HDU 2669 Romantic

题意:

求X*a + Y*b = 1最小非负整数解x和相应的y.

分析:

扩展欧几里德求出来基础解后稍微处理下就好了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;

ll ex_gcd(ll a, ll b, ll& x, ll& y)
{
if(b == 0){
x = 1, y = 0;
return a;
}
ll d = ex_gcd(b, a % b, y, x);
y -= a / b * x;
return d;
}

int main()
{
ll a, b, x, y, d;
while(~scanf("%lld%lld", &a, &b)){
d = ex_gcd(a, b, x, y);
if(1 % d) printf("sorry\n");
else {
ll extra = 0;
if(x < 0) extra = 1;
ll t = x / b - extra;
printf("%lld %lld\n", x - t * b, y + t * a);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息