結果

問題 No.186 中華風 (Easy)
ユーザー ebi_fly
提出日時 2023-07-05 01:35:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,714 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 195,608 KB
最終ジャッジ日時 2025-02-15 06:05:22
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "test/math/yuki_447.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/447"

#line 2 "template/template.hpp"

#include <bits/stdc++.h>

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

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

namespace lib {

using namespace std;

}  // namespace lib

// using namespace lib;
#line 2 "math/crt.hpp"

// #include "../math/ext_gcd.hpp"
#line 5 "math/crt.hpp"

namespace lib {

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;  
}

std::pair<ll, ll> crt(const std::vector<ll> &b, const std::vector<ll> &m) {
    ll r = 0, mod = 1;
    rep(i, 0, int(b.size())) {
        ll p,q;
        ll d = extGcd(mod, m[i], p, q);
        if ((b[i] - r) % d != 0) return {0, -1};
        ll x = (b[i] - r) / d * p % (m[i] / d);
        r += x * mod;
        mod *= m[i] / d;
    }
    return {(r % mod + mod) % mod, mod};
}

}  // namespace lib
#line 5 "test/math/yuki_447.test.cpp"

int main() {
    std::vector<ll> x(3), y(3);
    rep(i,0,3) {
        std::cin >> x[i] >> y[i];
    }
    auto [r, m] = lib::crt(x, y);
    if(m < 0) {
        std::cout << -1 << '\n';
    }
    else {
        if(r == 0) r += m;
        std::cout << r << '\n';
    }
}
0