結果

問題 No.186 中華風 (Easy)
ユーザー predpred
提出日時 2021-03-30 16:28:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,409 bytes
コンパイル時間 2,079 ms
コンパイル使用メモリ 203,940 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 13:02:05
合計ジャッジ時間 3,218 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

std::pair<long long, long long> crt(std::vector<long long> &a, std::vector<long long> &m){
    assert(a.size() == m.size());
    long long ax = a[0], mx = m[0];

    auto extgcd = [](long long r, long long s) -> std::tuple<long long, long long, long long>{
        bool rev = false;
        if(s < r){
            std::swap(r, s);
            rev = true;
        }
        long long a = 1, b = 0, c = 0, d = 1, k;
        while(s){
            k = r / s;
            a -= k * c;
            b -= k * d;
            std::swap(a, c);
            std::swap(b, d);
            r %= s;
            std::swap(r, s);
        }
        if(rev) return std::make_tuple(b, a, r);
        else return std::make_tuple(a, b, r);
    };

    for(int i = 1, sz = a.size(); i < sz; i++){
        long long my = m[i];
        long long ay = (a[i] % my + my) % my;

        auto [rx, ry, g] = extgcd(mx, my);
        if((ay - ax) % g != 0) return std::make_pair(0, 0);

        long long l = my / g;
        ax = ax + ((ay - ax) / g) % l * rx % l * mx;
        mx *= l;
        if(ax < 0) ax += mx;
    }

    return std::make_pair(ax, mx);
}

int main(){
    std::vector a(3, 0LL), b(3, 0LL);
    for(int i = 0; i < 3; i++) std::cin >> a[i] >> b[i];
    auto res = crt(a, b);
    if(res.second == 0) res.first = -1;
    if(res.first == 0) res.first = res.second;
    std::cout << res.first << "\n";
}
0