結果

問題 No.186 中華風 (Easy)
ユーザー shinnshinnshinnshinn
提出日時 2023-01-26 11:53:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,187 bytes
コンパイル時間 1,859 ms
コンパイル使用メモリ 201,344 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 14:34:31
合計ジャッジ時間 3,037 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,380 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 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(const vector<T>& rs, const 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){
        auto[x, y] = extgcd(res_m, ms[i]);
        T g = __gcd(res_m, ms[i]);
        if((rs[i] - res_r)%g != 0)return pair<T,T>(-1, -1);
        T tmp = (rs[i] - res_r) / g * x%(ms[i]/g);
        res_r += res_m * tmp;
        res_m *= ms[i] / g;
    }

    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