結果

問題 No.186 中華風 (Easy)
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2022-12-26 01:09:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,079 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 114,932 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-12 17:01:34
合計ジャッジ時間 2,020 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;

tuple<long long, long long, long long> ext_gcd(long long a, long long b){
    if (b == 0) return make_tuple(a, 1, 0);

    long long g, x, y;
    tie(g, y, x) = ext_gcd(b, a % b);
    y -= a / b * x;
    return make_tuple(g, x, y);
}

pair<long long, long long> CRT(vector<long long> &b, vector<long long> &m){
    long long r=0, lcm=1;
    for (int i=0; i<b.size(); i++){
        long long p, q, g;
        tie(g, p, q) = ext_gcd(lcm, m[i]);
        if ((b[i]-r) % g != 0) return make_pair(-1, 0); //No solution
        r += lcm * ((b[i]-r) / g * p % (m[i]/g));
        lcm *= m[i]/g;
    }

    return make_pair(((r%lcm)+lcm)%lcm, lcm);
}

int main(){

    vector<long long> b(3), m(3);
    for (int i=0; i<3; i++) cin >> b[i] >> m[i];
    long long x, l;
    tie(x, l) = CRT(b, m);
    cout << (x == 0 ? l : x) << endl;

    return 0;
}
0