結果

問題 No.186 中華風 (Easy)
ユーザー Manuel1024Manuel1024
提出日時 2021-03-01 23:49:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,181 bytes
コンパイル時間 594 ms
コンパイル使用メモリ 71,032 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 04:03:03
合計ジャッジ時間 1,494 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

ll myExtGCD(ll a, ll b, ll& x, ll& y){
    if(a % b == 0){
        x = 0;
        y = 1;
        return b;
    }
    
    ll d = myExtGCD(b, a % b, x, y);
    ll tmp = -y * (a / b) + x;
    x = y;
    y = tmp;
    return d;
}

P CRT(const vector<ll> &b, const vector<ll> &mo){
    ll r = 0, m = 1;
    for(int i = 0; i < b.size(); i++){
        ll p, q;
        ll d = myExtGCD(m, mo[i], p, q);
        if((b[i]-r)%d != 0) return make_pair(0, 0);
        else{
            ll tmp = (b[i]-r)/d * p % (mo[i]/d);
            r = r + tmp * m;
            m *= mo[i]/d;
            while(r < 0) r += m;
            r %= m;
        }
        // cerr << r << " " << m << endl;
    }
    return make_pair(r, m);
}

int main(){
    vector<ll> x(3), y(3);
    bool is_allzero = true;
    for(int i = 0; i < 3; i++){
        cin >> x[i] >> y[i];
        if(x[i] != 0) is_allzero = false;
    }
    P ans = CRT(x, y);
    if(ans.first == 0 && ans.second == 0) cout << -1 << endl;
    else if(is_allzero) cout << ans.second << endl;
    else cout << ans.first << endl;
    return 0;
}
0