結果

問題 No.186 中華風 (Easy)
ユーザー 303Yuyu303Yuyu
提出日時 2021-01-01 23:50:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,195 bytes
コンパイル時間 972 ms
コンパイル使用メモリ 102,524 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 20:06:24
合計ジャッジ時間 1,692 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>
#include <deque>
#include <complex>
#include <bitset>
#include <functional>
#include <cassert>
//#include <atcoder/all>

using namespace std;
//using namespace atcoder;

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using ld = long double;
using vld = vector<ld>;
using vb = vector<bool>;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define dbg(x) true
#endif

template <class T> void chmin(T& a, T b) { a = min(a, b);}
template <class T> void chmax(T& a, T b) { a = max(a, b);}
template <class T> ostream& operator<<(ostream& s, const vector<T>& a) {
    for(auto i : a) s << i << ' ';
    return s;
}

constexpr ll INFL = 1LL << 60;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

ll ext_gcd(ll a, ll b, ll& x, ll& y) {
    if(b == 0) { x = 1; y = 0; return a; }
    ll g = ext_gcd(b, a % b, y, x);
    y -= a/b * x;
    return g;
}

pair<ll, ll> crt_extgcd(const vector<ll>& r, const vector<ll>& m) {
    pair<ll, ll> ret;
    ll& rem = ret.first;
    ll& mod = ret.second;
    rem = 0, mod = 1;
    for(int i = 0; i < (int)r.size(); ++i) {
        ll inv_mod, _;
        ll div = ext_gcd(mod, m[i], inv_mod, _);    // mod*inv_mod = 1 (mod m[i]/div)
        if((r[i] - rem) % div != 0) return make_pair(0, -1);
        ll tmp = (r[i] - rem) / div * inv_mod % (m[i] / div);
        rem += mod * tmp;
        mod *= m[i] / div;    // lcm(m, m[i])
    }
    rem = (rem % mod + mod) % mod;
    return ret;
}

void solve() {
    vll r(3), m(3);
    rep(i, 3) cin >> r[i] >> m[i];
    pll p = crt_extgcd(r, m);
    ll ans = p.first;
    if(ans == 0) ans = p.second;
    cout << ans << endl;
    return;
}

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