当前位置: 代码迷 >> 综合 >> HDU 2669 Romantic(扩展欧几里德)
  详细解决方案

HDU 2669 Romantic(扩展欧几里德)

热度:82   发布时间:2023-12-08 10:41:49.0

题目链接:
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;
}