結果

問題 No.186 中華風 (Easy)
ユーザー ta7uwta7uw
提出日時 2020-09-22 15:19:29
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,316 bytes
コンパイル時間 1,528 ms
コンパイル使用メモリ 168,400 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 11:12:41
合計ジャッジ時間 2,913 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
// #include <atcoder/all>

using namespace std;
// using namespace atcoder;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG
/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#else
#define print(...)
#endif

inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}

/**
 * 拡張 Euclid の互除法
 * @return ap + bq = gcd(a, b) となる (p, q) を求め、d = gcd(a, b) をリターンします
 */
long long extGcd(long long a, long long b, long long &p, long long &q) {
    if (b == 0) {
        p = 1;
        q = 0;
        return a;
    }
    long long d = extGcd(b, a % b, q, p);
    q -= a / b * p;
    return d;
}

/**
 * Chinese remainder theorem (中国剰余定理)
 * ref. https://qiita.com/drken/items/ae02240cd1f8edfc86fd
 * @return リターン値を (r, m) とすると解は x ≡ r (mod. m), 解なしの場合は (0, -1) をリターン
 */
pair<long long, long long> chinese_remainder_theorem(const vector<long long> &b, const vector<long long> &m) {
    long long r = 0, M = 1;
    for (int i = 0; i < (int) b.size(); ++i) {
        long long p, q;
        long long d = extGcd(M, m[i], p, q); // p is inv of M/d (mod. m[i]/d)
        if ((b[i] - r) % d != 0) return make_pair(0, -1);
        long long tmp = (b[i] - r) / d * p % (m[i] / d);
        r += M * tmp;
        M *= m[i] / d;
    }
    return make_pair(mod(r, M), M);
}


void solve() {
    ll X1, Y1, X2, Y2, X3, Y3;
    cin >> X1 >> Y1 >> X2 >> Y2 >> X3 >> Y3;
    vector<ll> b = {X1, X2, X3};
    vector<ll> m = {Y1, Y2, Y3};
    auto ans = chinese_remainder_theorem(b, m);
    if (ans.first == 0) {
        cout << -1 << '\n';
    } else {
        cout << ans.first << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}
0