結果

問題 No.186 中華風 (Easy)
ユーザー Tomokazu SaitoTomokazu Saito
提出日時 2021-02-28 23:07:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,837 bytes
コンパイル時間 864 ms
コンパイル使用メモリ 77,288 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-12 10:58:02
合計ジャッジ時間 1,690 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "../math.h"

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

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);
    if (ret.second == -1) {
        cout << -1 << endl;
    } else if (ret.first == 0) {
        cout << ret.second << endl;
    } else {
        cout << ret.first << endl;
    }
}
0