結果

問題 No.186 中華風 (Easy)
ユーザー shinnshinnshinnshinn
提出日時 2023-01-26 19:51:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,550 bytes
コンパイル時間 1,985 ms
コンパイル使用メモリ 202,192 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-09 19:00:54
合計ジャッジ時間 2,973 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

template<typename T>
T lcm(T a, T b){
    return a / __gcd(a, b) * b;
}

template<typename T>
pair<T,T> extgcd(T a, T b){
    // ax + by = gcd(a, b)
    if(a == 0){
        return pair<T,T>(0, 1);
    }else if(b == 0){
        return pair<T,T>(1, 0);
    }

    T q = a/b;
    T r = a%b;

    auto [s, t] = extgcd(b, r);

    T y = s - q*t;
    return pair<T,T>(t, y);
}

template<typename T>
pair<T,T> ctr(const vector<T>& rs, const vector<T>& ms){
    assert(rs.size() == ms.size());

    int n = rs.size();
    T r0 = 0;
    T m0 = 1;
    for(int i = 0;i < n; ++i){
        T m1 = ms[i];
        T r1 = rs[i];
        r1 = (r1%m1 + m1)%m1;

        if(r0 < r1){
            swap(r0, r1);
            swap(m0, m1);
        }

        T g = __gcd(m0, m1);
        if(r0%g != r1%g){
            return pair<T,T>(-1, -1);
        }

        auto[x, y] = extgcd(m0, m1);
        long long u1 = (m1 / g);

        long long v = (r1 - r0) / g % u1 * x % u1;

        r0 += v * m0;
        m0 *= u1;
        if (r0 < 0) r0 += m0;
    }

    return pair<T,T>(r0, m0);
}

void solve(){
    vector<long long> r(3), m(3);
    for(int i = 0;i < 3; ++i){
        cin >> r[i] >> m[i];
    }

    auto[ar, am] = ctr(r, m);
    if(ar == 0){
        long long l = 1;
        for(int i = 0;i < 3; ++i){
            l = lcm(l, m[i]);
        }
        cout << l << endl;
        return;
    }
    cout << ar << endl;
}

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    solve();
    return 0;
}
0