結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-09-12 14:29:49 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,486 bytes |
| コンパイル時間 | 935 ms |
| コンパイル使用メモリ | 85,684 KB |
| 最終ジャッジ日時 | 2025-02-16 21:53:36 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 7 WA * 18 |
ソースコード
#line 1 "test\\yukicoder\\448.test.cpp"
#include <algorithm>
#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 5 "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<i32> pp(n + 1, 1), res(n + 1);
for (size_t i = 0; i < n; ++i) {
i64 u = safe_mod((safe_mod(a[i], m_cpy[i]) - res[i]) * (i64)std::gcd(pp[i], m_cpy[i]), m_cpy[i]);
for (size_t j = i + 1; j <= n; ++j) {
res[j] = (i32)((res[j] + 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 5 "test\\yukicoder\\448.test.cpp"
#include <iostream>
#line 8 "test\\yukicoder\\448.test.cpp"
int main() {
size_t n;
std::cin >> n;
std::vector<tifa_libs::i32> r(n);
std::vector<tifa_libs::u32> m(n);
for (size_t i = 0; i < n; ++i) std::cin >> r[i] >> m[i];
auto res = tifa_libs::math::crt_mod(r, m, 1'000'000'007);
if (!res) {
std::cout << "-1\n";
return 0;
}
std::cout << ((size_t)std::count(r.begin(), r.end(), 0) == n ? res->second : res->first) << '\n';
return 0;
}