結果

問題 No.2117 中国剰余定理入門
ユーザー tokumini_sstokumini_ss
提出日時 2022-11-04 21:31:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 784 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 199,424 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 23:44:21
合計ジャッジ時間 3,385 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int64_t extendedGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    } else {
        const int64_t g = extendedGcd(b, a % b, y, x);
        y -= (a / b) * x;
        return g;
    }
}

int main() {
    int64_t B0, C0, B1, C1;
    cin >> B0 >> C0 >> B1 >> C1;
    const int64_t g = gcd(B0, B1);
    if (g != 1) {
        cout << "NaN" << endl;
        return 0;
    }
    (C0 += B0 * (abs(C0) / B0 + 1)) %= B0;
    (C1 += B1 * (abs(C1) / B1 + 1)) %= B1;

    int64_t p, q;
    extendedGcd(B0, B1, p, q);
    int64_t x = C0 * q * B1 + C1 * p * B0;
    if (x < 0) {
        x += B0 * B1;
    }
    assert(x % B0 == C0);
    assert(x % B1 == C1);
    cout << x << endl;
}
0