結果

問題 No.186 中華風 (Easy)
ユーザー kyunakyuna
提出日時 2020-02-07 01:20:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,100 bytes
コンパイル時間 568 ms
コンパイル使用メモリ 73,680 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 00:58:18
合計ジャッジ時間 1,499 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

long long extgcd(long long a, long long b, long long &x, long long &y) {
    for (long long u = y = 1, v = x = 0; a; ) {
        long long q = b / a;
        swap(x -= q * u, u);
        swap(y -= q * v, v);
        swap(b -= q * a, a);
    }
    return b;
}

pair<long long, long long> chinese_rem(vector<long long> b, vector<long long> m) {
    long long r = 0, M = 1;
    for (int i = 0; i < (int)b.size(); ++i) {
        long long p, q, d = extgcd(M, m[i], p, q);
        if ((b[i] - r) % d != 0) return {0, -1};
        r += (b[i] - r) / d * p % (m[i] / d) * M;
        M *= m[i] / d;
    }
    return {(r % M + M) % M, M};
}

int main() {
    vector<long long> b(3), m(3);
    bool exist_non_zero = false;
    for (int i = 0; i < 3; ++i) {
        cin >> b[i] >> m[i];
        if (b[i]) exist_non_zero = true;
    }
    pair<long long, long long> res = chinese_rem(b, m);
    if (res.second == -1) cout << -1 << endl;
    else if (exist_non_zero) cout << res.first << endl;
    else cout << res.second << endl;
}
0