結果

問題 No.187 中華風 (Hard)
ユーザー TifaTifa
提出日時 2023-09-12 11:25:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,508 bytes
コンパイル時間 781 ms
コンパイル使用メモリ 83,748 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-12 11:25:41
合計ジャッジ時間 2,963 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#line 1 "test/yukicoder/448.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/448"

#line 1 "lib/math/crt_mod.hpp"



#line 1 "lib/util.hpp"



#include <cassert>
#include <cstddef>
#include <cstdint>
#include <type_traits>

namespace tifa_libs {

using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;

}  // namespace tifa_libs


#line 5 "lib/math/crt_mod.hpp"

#line 1 "lib/math/safe_mod.hpp"



#line 6 "lib/math/safe_mod.hpp"

namespace tifa_libs::math {

template <class T, std::enable_if_t<std::is_signed_v<T>, int> = 0>
constexpr T safe_mod(T x, std::make_unsigned_t<T> mod) { return ((x %= (T)mod) < 0 ? x + (T)mod : x); }

}  // namespace tifa_libs::math


#line 7 "lib/math/crt_mod.hpp"

#include <numeric>
#include <optional>
#include <vector>

namespace tifa_libs::math {

inline std::optional<std::pair<u32, u32>> crt_mod(const std::vector<i32> &a, const std::vector<u32> &m, const u32 mod) {
  assert(a.size() == m.size());
  const size_t n = a.size();
  std::vector<u32> m_cpy(m);
  for (size_t i = 0; i < n; ++i) {
    u32 &mi = m_cpy[i];
    for (size_t j = 0; j < i; ++j) {
      u32 &mj = m_cpy[j], d = std::gcd(mi, mj);
      if (d == 1) continue;
      if (safe_mod(a[i], d) != safe_mod(a[j], d)) return {};
      mi /= d;
      mj /= d;
      if (u32 k = std::gcd(mi, d); k != 1)
        while (d % k == 0) {
          mi *= k;
          d /= k;
        }
      mj *= d;
    }
  }
  m_cpy.push_back(mod);
  std::vector<int> pp(n + 1, 1), res(n + 1);
  for (size_t i = 0; i < n; ++i) {
    i32 u = (i32)((safe_mod(a[i], m_cpy[i]) - res[i]) * (i64)std::gcd(pp[i], m_cpy[i]) % m_cpy[i]);
    if (u < 0) u += m_cpy[i];
    for (size_t j = i + 1; j <= n; ++j) {
      res[j] = (i32)((res[j] + (i64)u * pp[j]) % m_cpy[j]);
      pp[j] = (i32)((i64)(pp[j]) * m_cpy[i] % m_cpy[j]);
    }
  }
  return std::make_pair(res.back(), pp.back());
}

}  // namespace tifa_libs::math


#line 4 "test/yukicoder/448.test.cpp"

#include <iostream>
#line 7 "test/yukicoder/448.test.cpp"

int main() {
  tifa_libs::i64 n;
  std::cin >> n;
  std::vector<tifa_libs::i32> r;
  std::vector<tifa_libs::u32> m;
  r.reserve((size_t)n);
  m.reserve((size_t)n);
  while (n--) {
    tifa_libs::i32 x;
    tifa_libs::u32 y;
    std::cin >> x >> y;
    r.push_back(x);
    m.push_back(y);
  }
  auto res = tifa_libs::math::crt_mod(r, m, 1'000'000'007);
  std::cout << (res ? (tifa_libs::i32)res->first : -1) << '\n';
  return 0;
}
0