結果

問題 No.186 中華風 (Easy)
ユーザー Ricky_ponRicky_pon
提出日時 2023-06-27 10:13:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,024 bytes
コンパイル時間 799 ms
コンパイル使用メモリ 80,276 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-17 07:12:57
合計ジャッジ時間 1,801 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using lint = ll;
using ulint = unsigned long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
#define FOR(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(i, n) FOR(i, 0, n)
template <class T, class U>
bool chmin(T &a, U &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
bool chmax(T &a, U &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
ostream &operator<<(ostream &os, const V<T> &v) {
    os << "[";
    for (auto d : v) os << d << ", ";
    return os << "]";
}

struct EG {
    lint g, x, y;
};
EG ext_gcd(ll a, ll b) {
    if (b == 0) {
        if (a >= 0) {
            return EG{a, 1, 0};
        } else {
            return EG{-a, -1, 0};
        }
    }
    auto e = ext_gcd(b, a % b);
    return EG{e.g, e.y, e.x - a / b * e.y};
}

lint inv_mod(ll x, ll md) {
    auto z = ext_gcd(x, md).x;
    return (z % md + md) % md;
}

lint pow_mod(lint x, lint n, lint mod) {
    auto r = 1 % mod;
    x %= mod;
    while (n) {
        if (n & 1) r = (r * x) % mod;
        x = (x * x) % mod;
        n >>= 1;
    }
    return r;
}

pll crt(const V<ll> &rs, const V<ll> ms) {
    int n = int(rs.size());
    ll r = 0, m = 1;
    rep(i, n) {
        auto [g, im, _] = ext_gcd(m, ms[i]);
        if ((rs[i] - r) % g) return {0, -1};
        auto tmp = (rs[i] - r) / g * im % (ms[i] / g);
        r += m * tmp;
        m *= ms[i] / g;
    }
    return {(r % m + m) % m, m};
}

int main() {
    vector<lint> rs(3), ms(3);
    rep(i, 3) cin >> rs[i] >> ms[i];
    auto [r, m] = crt(rs, ms);
    if (m < 0) {
        puts("-1");
        return 0;
    }
    if (r == 0) {
        r = 1;
        rep(i, 3) r = r / gcd(r, ms[i]) * ms[i];
    }
    cout << r << endl;
}
0