結果

問題 No.186 中華風 (Easy)
ユーザー srjywrdnprkt
提出日時 2022-12-26 01:09:54
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,079 bytes
コンパイル時間 979 ms
コンパイル使用メモリ 113,000 KB
最終ジャッジ日時 2025-02-09 21:00:32
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;

tuple<long long, long long, long long> ext_gcd(long long a, long long b){
    if (b == 0) return make_tuple(a, 1, 0);

    long long g, x, y;
    tie(g, y, x) = ext_gcd(b, a % b);
    y -= a / b * x;
    return make_tuple(g, x, y);
}

pair<long long, long long> CRT(vector<long long> &b, vector<long long> &m){
    long long r=0, lcm=1;
    for (int i=0; i<b.size(); i++){
        long long p, q, g;
        tie(g, p, q) = ext_gcd(lcm, m[i]);
        if ((b[i]-r) % g != 0) return make_pair(-1, 0); //No solution
        r += lcm * ((b[i]-r) / g * p % (m[i]/g));
        lcm *= m[i]/g;
    }

    return make_pair(((r%lcm)+lcm)%lcm, lcm);
}

int main(){

    vector<long long> b(3), m(3);
    for (int i=0; i<3; i++) cin >> b[i] >> m[i];
    long long x, l;
    tie(x, l) = CRT(b, m);
    cout << (x == 0 ? l : x) << endl;

    return 0;
}
0