結果

問題 No.186 中華風 (Easy)
ユーザー 👑 emthrmemthrm
提出日時 2021-09-24 15:34:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,234 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 81,744 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 20:15:52
合計ジャッジ時間 2,171 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <tuple>
#include <utility>
#include <vector>

long long mod_inv(long long a, int m) {
  if ((a %= m) < 0) a += m;
  if (std::__gcd(a, static_cast<long long>(m)) != 1) return -1;
  long long b = m, x = 1, u = 0;
  while (b > 0) {
    long long q = a / b;
    std::swap(a -= q * b, b);
    std::swap(x -= q * u, u);
  }
  x %= m;
  return x < 0 ? x + m : x;
}

template <typename T>
std::pair<T, T> simultaneous_linear_congruence(const std::vector<T> &a, const std::vector<T> &b, const std::vector<T> &m) {
  const int n = a.size();
  T x = 0, md = 1;
  for (int i = 0; i < n; ++i) {
    T l = md * a[i], r = -x * a[i] + b[i], g = std::__gcd(l, m[i]);
    if (r % g != 0) return {0, -1};
    x += md * (r / g * mod_inv(l / g, m[i] / g) % (m[i] / g));
    md *= m[i] / g;
  }
  return {x < 0 ? x + md : x, md};
}

int main() {
  constexpr int N = 3;
  std::vector<long long> x(N), y(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> x[i] >> y[i];
  }
  long long ans, mod;
  std::tie(ans, mod) = simultaneous_linear_congruence(std::vector<long long>(N, 1), x, y);
  if (mod == 0) {
    std::cout << "-1\n";
  } else {
    std::cout << (ans == 0 ? mod : ans) << '\n';
  }
  return 0;
}
0