結果

問題 No.186 中華風 (Easy)
ユーザー kyo1kyo1
提出日時 2020-09-25 10:17:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,201 bytes
コンパイル時間 3,216 ms
コンパイル使用メモリ 204,200 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-27 01:03:23
合計ジャッジ時間 3,192 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,508 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 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>

using namespace std;

template <typename T>
std::tuple<T, T, T> egcd(T a, T b) {
  T x = 1, y = 0, u = 0, v = 1;
  while (b != 0) {
    T t = a / b;
    a -= t * b;
    x -= t * u;
    y -= t * v;
    std::swap(a, b);
    std::swap(x, u);
    std::swap(y, v);
  }
  return {a, x, y};
}

template <typename T>
std::optional<std::pair<T, T>> crt(const std::vector<T> &r, const std::vector<T> &m) {
  assert(r.size() == m.size());
  T x = 0, y = 1;
  for (std::size_t i = 0; i < r.size(); i++) {
    T u = r[i], v = m[i];
    if (y < v) {
      std::swap(x, u);
      std::swap(y, v);
    }
    auto [d, a, _] = egcd(y, v);
    if ((u - x) % d != 0) return std::nullopt;
    T t = v / d;
    x += (u - x) / d % t * a % t * y;
    y *= t;
  }
  if (x < 0) x += y;
  return std::make_pair(x, y);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  vector<int64_t> X(3), Y(3);
  for (int i = 0; i < 3; i++) {
    cin >> X[i] >> Y[i];
  }
  auto res = crt(X, Y);
  if (res) {
    if (res.value().first == 0) {
      cout << res.value().second << '\n';
    } else {
      cout << res.value().first << '\n';
    }
  } else {
    cout << -1 << '\n';
  }
  return 0;
}
0