拡張ユークリッド--hdu2669

HDU-2669

スカイは、Spriteです。
鳥が空を飛ぶです。
風が素晴らしいです。
木投げ吹き
、木が揺れているの葉が落ちています。
恋人は通過を歩くと、そうあなたです。
................................ yifenfeiによって英語の授業で書きます

IMG

女の子は賢いと明るいです。HDUで数学のようなすべての女の子。すべての女の子は、数学の問題を解決したいです!
今、次の2つの非負の整数aとbのを教えてください。Xの満足させるために、非負整数XとYの整数を探すA + Yの代わりに、そのような答えプリント「ごめん」した場合、B = 1を。

入力

入力は複数のテストケースが含まれています。
各ケース2つの非負整数A、B(0 <、<= 2 ^ 31 B)

出力

出力非負整数Xと整数Y、X小さい方を選びましされるよりも多くの回答がある場合。何の答えは、代わりに「ごめん」を入れていない場合。

サンプル入力

77 51
10 44
34 79

サンプル出力

2 -3
sorry
7 -3

問題の意味

= X 1で肯定プレゼンス斧+は、Y、出力非負整数x、整数y、そうでなければ出力申し訳ありませんがある場合

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <iomanip>
#include <cstdio>

using namespace std;
typedef long long LL;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;

const LL N = 1e6+50;
const LL MOD = 1e9+7;
const LL INF = 0x3f3f3f3f;

#define lson l, m, rt>>1
#define rson m+1, r, rt>>1|1

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    if(!b)
    {
        x = 1;y = 0;
        return a;
    }
    LL ret = exgcd(b, a%b, y, x);
    y -= x*(a/b);
    return ret;
}

LL gcd(LL a, LL b)
{
    return b?gcd(b, a%b):a;
}

int main()
{
    LL a, b, x, y;
    while(cin >> a >> b)
    {
        if(gcd(a, b) != 1) puts("sorry");
        else
        {
            exgcd(a, b, x, y);
            while(x < 0)
            {
                x += b;y -= a;
            }
            cout << x << " " << y << endl;
        }
    }
    return 0;
}

おすすめ

転載: www.cnblogs.com/shuizhidao/p/11681228.html