結果

問題 No.186 中華風 (Easy)
ユーザー shinnshinnshinnshinn
提出日時 2023-01-26 16:13:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,234 bytes
コンパイル時間 2,043 ms
コンパイル使用メモリ 203,080 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-09 17:03:20
合計ジャッジ時間 3,093 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
4,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
4,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 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(vector<T> rs, vector<T> ms){
    assert(rs.size() == ms.size());

    int n = rs.size();
    T res_r = 0;
    T res_m = 1;
    for(int i = 0;i < n; ++i){
        T g = __gcd(res_m, ms[i]);
        if(res_r%g != rs[i]%g != 0){
            return pair<T,T>(-1, -1);
        }

        auto[x, y] = extgcd(res_m, ms[i]);
        T l = lcm(res_m, ms[i]);
        res_r = rs[i]*res_m/g%l*x%l + res_r*ms[i]/g%l*y%l;
        res_r = (res_r%l + l)%l;
        res_m = l;
    }

    return pair<T,T>(res_r, res_m);
}

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);
    cout << ar << endl;
}

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