結果

問題 No.186 中華風 (Easy)
ユーザー yoshnaryyoshnary
提出日時 2020-05-10 13:12:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,284 bytes
コンパイル時間 2,395 ms
コンパイル使用メモリ 68,936 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 20:47:59
合計ジャッジ時間 1,908 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <tuple>
#include <utility>

// Find x and y such that a*x + b*y = gcd(a, b)
// Return gcd(a, b)
long long extgcd(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1, y = 0;
        return a;
    }
    long long ret = extgcd(b, a%b, x, y);
    std::tie(x, y) = std::make_pair(y, x - a / b * y);
    return ret;
}

// Chinese Remainder Theorem
// Find rem such that 0 <= rem < lcm(m1, m2) and
// rem % m1 = r1 and rem % m2 = r2
// Return { -1, -1 } if such rem doesn't exist,
// otherwise return { lcm(m1, m2), rem }
std::pair<long long, long long>
crt(long long m1, long long r1, long long m2, long long r2) {
    long long x = 0, y = 0;
    long long d = extgcd(m1, m2, x, y);
    if (r1%d != r2%d) {
        return { -1, -1 };
    }
    long long lcm = m1 / d * m2;
    long long rem = ((r2 - r1) / d * x % (m2 / d)) * m1;
    rem = ((rem + r1) % lcm + lcm) % lcm;
    return { lcm, rem };
}

int main() {
    long long x, y; std::cin >> x >> y;
    for (int i = 0; i < 2; i++) {
        long long a, b; std::cin >> a >> b;
        std::tie(y, x) = crt(y, x, b, a);
        if (x == -1) {
            std::cout << -1 << std::endl;
            return 0;
        }
    }
    std::cout << x << std::endl;
    return 0;
}
0