結果
問題 | No.1482 Swap Many Permutations |
ユーザー | noshi91 |
提出日時 | 2021-04-16 22:36:25 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 133 ms / 2,000 ms |
コード長 | 6,810 bytes |
コンパイル時間 | 2,529 ms |
コンパイル使用メモリ | 129,608 KB |
最終ジャッジ日時 | 2025-01-20 20:17:27 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 45 |
ソースコード
//#define NDEBUG #include <algorithm> #include <cassert> #include <cstddef> #include <cstdint> #include <iostream> #include <utility> #include <vector> namespace n91 { using i32 = std::int32_t; using i64 = std::int64_t; using u32 = std::uint32_t; using u64 = std::uint64_t; using isize = std::ptrdiff_t; using usize = std::size_t; struct rep { struct itr { usize i; constexpr itr(const usize i) noexcept : i(i) {} void operator++() noexcept { ++i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; const itr f, l; constexpr rep(const usize f, const usize l) noexcept : f(std::min(f, l)), l(l) {} constexpr auto begin() const noexcept { return f; } constexpr auto end() const noexcept { return l; } }; struct revrep { struct itr { usize i; constexpr itr(const usize i) noexcept : i(i) {} void operator++() noexcept { --i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; const itr f, l; constexpr revrep(const usize f, const usize l) noexcept : f(l - 1), l(std::min(f, l) - 1) {} constexpr auto begin() const noexcept { return f; } constexpr auto end() const noexcept { return l; } }; template <class T> auto md_vec(const usize n, const T& value) { return std::vector<T>(n, value); } template <class... Args> auto md_vec(const usize n, Args... args) { return std::vector<decltype(md_vec(args...))>(n, md_vec(args...)); } template <class T> constexpr T difference(const T& a, const T& b) noexcept { return a < b ? b - a : a - b; } template <class T> void chmin(T& a, const T& b) noexcept { if (b < a) a = b; } template <class T> void chmax(T& a, const T& b) noexcept { if (a < b) a = b; } template <class F> class rec_lambda { F f; public: rec_lambda(F&& f) : f(std::move(f)) {} template <class... Args> auto operator()(Args &&... args) const { return f(*this, std::forward<Args>(args)...); } }; template <class F> auto make_rec(F&& f) { return rec_lambda<F>(std::move(f)); } template <class T> T scan() { T ret; std::cin >> ret; return ret; } constexpr char eoln = '\n'; template <class T> T ceildiv(const T& l, const T& r) { return l / r + (l % r != 0 ? 1 : 0); } } // namespace n91 #include <vector> namespace n91 { template <class T> class mint_utils { using usize = std::size_t; std::vector<T> fact_, inv_fact_; public: mint_utils() : fact_(), inv_fact_() {} explicit mint_utils(const usize n) : fact_(n + 1), inv_fact_(n + 1) { fact_[0] = 1; for (usize i = 0; i != n; i += 1) { fact_[i + 1] = fact_[i] * (i + 1); } inv_fact_[n] = T(1) / fact_[n]; for (usize i = n; i != 0; i -= 1) { inv_fact_[i - 1] = inv_fact_[i] * i; } } T fact(const usize n) const { return fact_[n]; } T inv_fact(const usize n) const { return inv_fact_[n]; } T binom(const usize n, const usize r) const { return fact_[n] * inv_fact_[r] * inv_fact_[n - r]; } T homo(const usize n, const usize r) const { return fact_[n + r - 1] * inv_fact_[n] * inv_fact_[r - 1]; } }; } // namespace n91 #include <atcoder/convolution> #include <atcoder/internal_math> #include <atcoder/modint> #include <map> #include <numeric> namespace n91 { void main_() { using mint = atcoder::static_modint<998244353>; //* std::ios::sync_with_stdio(false); std::cin.tie(nullptr); //*/ const usize n = scan<usize>(); const usize m = scan<usize>(); std::vector<usize> b(n), c(n); for (const usize i : rep(0, n)) { std::cin >> b[i] >> c[i]; } if (m == 2) { for (const usize i : rep(0, n)) { if (i == 0) { std::cout << 1 << eoln; } else if (i == 1) { if (b[0] == b[1]) { std::cout << 0 << eoln; } else { std::cout << 2 << eoln; } } else { std::cout << 0 << eoln; } } return; } const auto parity = [m](const usize b) -> bool { bool ret = false; std::vector<bool> used(m, false); for (const usize i : rep(0, m)) { if (used[i]) { continue; } usize count = 0; usize v = i; do { used[v] = true; count += 1; v = (b * v) % m; } while (v != i); if (count % 2 == 0) { ret = !ret; } } return ret; }; std::vector<bool> parities(m); { const usize pr = atcoder::internal::primitive_root_constexpr(m); std::vector<usize> log(m); { usize v = 1; for (const usize i : rep(0, m - 1)) { log[v] = i; v = v * pr % m; } } std::map<usize, bool> memo; // gcd, parity for (const usize i : rep(1, m)) { const usize g = std::gcd(m - 1, log[i]); if (!memo.count(g)) { memo[g] = parity(i); } parities[i] = memo[g]; } } u64 one = 0, zero = 0; for (const usize i : rep(1, m)) { if (parities[i]) { one += 1; } else { zero += 1; } } one *= m; zero *= m; const mint_utils<mint> util(n + 1); std::vector<mint> odd, even; { mint one_fact = 1, zero_fact = 1; std::vector<mint> one_odd(n + 1), one_even(n + 1), zero_a(n + 1); for (const usize i : rep(0, n + 1)) { if (i % 2 == 0) { one_even[i] = one_fact / util.fact(i); } else { one_odd[i] = one_fact / util.fact(i); } zero_a[i] = zero_fact / util.fact(i); one_fact *= mint(one) - i; zero_fact *= mint(zero) - i; } odd = atcoder::convolution(one_odd, zero_a); even = atcoder::convolution(one_even, zero_a); } bool current_p = false; for (const usize i : rep(0, n)) { if (parities[c[i]]) { current_p = !current_p; } if (i == 0) { std::cout << 1 << eoln; continue; } if (i == 1) { if (b[0] == b[1] && c[0] == c[1]) { std::cout << 0 << eoln; } else { std::cout << (mint(m) * mint(m - 1)).val() << eoln; } continue; } if (current_p) { std::cout << (odd[i + 1] * util.fact(i + 1)).val() << eoln; } else { std::cout << (even[i + 1] * util.fact(i + 1)).val() << eoln; } } } } // namespace n91 int main() { n91::main_(); return 0; }