結果

問題 No.186 中華風 (Easy)
ユーザー Tomokazu SaitoTomokazu Saito
提出日時 2021-02-28 22:59:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,694 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 76,680 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-12 10:57:50
合計ジャッジ時間 1,243 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <vector>
#include <cassert>
#include <math.h>
#include <utility>
#include <iostream>

using namespace std;
using ll = int64_t;

template<typename T>
inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } // a > b

template<typename T>
inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } // a < b

template<typename T>
T safe_mod(const T x, const T m) {
    T ret = x % m;
    if (ret < 0) ret += m;
    return ret;
}

template<typename T>
T extGCD(const T a, const T b, T &p, T &q) {
    if (b == 0) { p = 1; q = 0; return a; }
    T d = extGCD(b, a % b, q, p);
    q -= a / b * p;
    return d;
}

template<typename T>
std::pair<T, T> crt(T b1, T m1, T b2, T m2) {
    T p, q;
    T d = extGCD(m1, m2, p, q);
    if ((b2 - b1) % d != 0) return std::make_pair<T, T>(0, -1);
    T m = m1 * (m2 / d);
    T tmp = (b2 - b1) / d * p % (m2 / d);
    T r = safe_mod(b1 + tmp * m1, m);
    return std::pair<T, T>(r, m);
}


// 拡張ユークリッド互除法による中国余剰定理の計算
template<typename T>
std::pair<T, T> crt(const std::vector<T> &r, const std::vector<T> &m) {
    assert(r.size() == m.size());
    T r_ret = 0, m_ret = 1;
    for (int64_t i = 0; i < r.size(); i++) {
        std::pair<T, T> pair = crt<T>(r_ret, m_ret, r[i], m[i]);
        r_ret = pair.first;
        m_ret = pair.second;

        if (m_ret == -1) return std::make_pair<T, T>(0, -1);
    }
    return std::pair<T, T>(r_ret, m_ret);
}

int main() {
    vector<ll> X(3), Y(3);
    for (int i = 0; i < 3; i++) {
        cin >> X[i] >> Y[i];
    }
    pair<ll, ll> ret = crt(X, Y);
    cout << ((ret.second == -1) ? -1 : ret.first) << endl;
}
0