现在的位置: 首页 > 综合 > 正文

hdu – 2669 – Romantic(扩展欧几里得)

2019年08月30日 ⁄ 综合 ⁄ 共 462字 ⁄ 字号 评论关闭

题意:给出a, b(0<a, b<=2^31),求X * a + Y * b = 1 的一组解x1, x2,满足x1 >= 0。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2669

——>>在找乘法逆元的题目,看到这题简单顺手A了。。

#include <cstdio>

typedef long long LL;

void Gcd(LL a, LL b, LL& d, LL& x, LL& y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        Gcd(b, a % b, d, y, x);
        y -= a / b * x;
    }
}

int main()
{
    int a, b;
    LL d = 0, x = 0, y = 0;

    while (scanf("%d%d", &a, &b) == 2)
    {
        Gcd(a, b, d, x, y);
        if (d != 1)
        {
            puts("sorry");
        }
        else
        {
            if (x >= 0)
            {
                printf("%I64d %I64d\n", x, y);
            }
            else
            {
                LL t = - x / b;
                if (x % b)
                {
                    ++t;
                }
                printf("%I64d %I64d\n", x + b * t, y - a * t);
            }
        }
    }

    return 0;
}

抱歉!评论已关闭.