HDU 2669 Romantic(扩展欧几里德)
2016-05-31 01:14
295 查看
题目链接:
HDU 2669 Romantic、
题意:
求X*a + Y*b = 1最小非负整数解x和相应的y.
分析:
扩展欧几里德求出来基础解后稍微处理下就好了。
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; }
相关文章推荐
- 1.m分解阶乘之和
- 2.几种递推数
- 3.欧拉函数
- 4.快速幂模m算法
- 5.扩展欧几里得&&中国剩余定理
- 6.数论_web
- Project Euler Problem 387 - Harshad Numbers - 深度优先
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- 编程之美2015初赛A
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016