結果

問題 No.186 中華風 (Easy)
ユーザー ebi_flyebi_fly
提出日時 2023-07-05 01:31:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,503 bytes
コンパイル時間 2,314 ms
コンパイル使用メモリ 206,968 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-26 00:27:33
合計ジャッジ時間 3,336 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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"

#line 2 "math/ext_gcd.hpp"

#line 4 "math/ext_gcd.hpp"

// https://github.com/maspypy/library/blob/main/nt/extgcd.hpp

namespace lib {

tuple<ll, ll, ll, ll> Farey_lr(ll a, ll b) {
    assert(a > 0 && b > 0);
    /*
    Farey 数列で、a/b が最初に現れるときの、左右を求める。
    a/b = 19/12 → (x1/y1, x2/y2) = (11/7, 8/5) → (11,7,8,5) を返す。
    */
    if (a == b) return {0, 1, 1, 0};
    ll q = (a - 1) / b;
    auto [x1, y1, x2, y2] = Farey_lr(b, a - q * b);
    return {q * x2 + y2, x2, q * x1 + y1, x1};
}

tuple<ll, ll, ll> extgcd(ll a, ll b) {
    // ax + by = d の最小解 (x, y, d) を返す。
    // (|x|+|y|, x) に関する辞書順最小とする。
    auto [x1, y1, x2, y2] = Farey_lr(a, b);
    tie(x1, y1) = make_pair(y1, -x1);
    tie(x2, y2) = make_pair(-y2, x2);
    ll g = a * x1 + b * y1;
    pair<ll,ll> key1 = make_pair(abs(x1) + abs(y1), x1);
    pair<ll,ll> key2 = make_pair(abs(x2) + abs(y2), x2);
    return (key1 < key2 ? make_tuple(x1, y1, g) : make_tuple(x2, y2, g));
}

}  // namespace lib
#line 5 "math/crt.hpp"

namespace lib {

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())) {
        auto [p, q, d] = extgcd(mod, m[i]);
        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};
}

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