結果

問題 No.1482 Swap Many Permutations
ユーザー noshi91noshi91
提出日時 2021-04-16 22:36:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 6,810 bytes
コンパイル時間 2,577 ms
コンパイル使用メモリ 137,540 KB
実行使用メモリ 10,596 KB
最終ジャッジ日時 2023-09-16 01:37:40
合計ジャッジ時間 8,300 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 49 ms
6,692 KB
testcase_13 AC 43 ms
6,700 KB
testcase_14 AC 95 ms
10,152 KB
testcase_15 AC 84 ms
9,324 KB
testcase_16 AC 22 ms
4,696 KB
testcase_17 AC 82 ms
9,136 KB
testcase_18 AC 24 ms
4,872 KB
testcase_19 AC 47 ms
6,176 KB
testcase_20 AC 86 ms
9,604 KB
testcase_21 AC 107 ms
9,984 KB
testcase_22 AC 112 ms
10,272 KB
testcase_23 AC 118 ms
10,400 KB
testcase_24 AC 103 ms
10,444 KB
testcase_25 AC 107 ms
10,324 KB
testcase_26 AC 111 ms
10,468 KB
testcase_27 AC 143 ms
10,404 KB
testcase_28 AC 104 ms
10,524 KB
testcase_29 AC 98 ms
10,300 KB
testcase_30 AC 124 ms
10,480 KB
testcase_31 AC 99 ms
10,500 KB
testcase_32 AC 111 ms
10,424 KB
testcase_33 AC 143 ms
10,304 KB
testcase_34 AC 103 ms
10,268 KB
testcase_35 AC 98 ms
10,456 KB
testcase_36 AC 107 ms
10,596 KB
testcase_37 AC 4 ms
4,380 KB
testcase_38 AC 16 ms
4,376 KB
testcase_39 AC 6 ms
4,384 KB
testcase_40 AC 5 ms
4,380 KB
testcase_41 AC 28 ms
4,376 KB
testcase_42 AC 10 ms
4,380 KB
testcase_43 AC 11 ms
4,380 KB
testcase_44 AC 10 ms
4,376 KB
testcase_45 AC 13 ms
4,376 KB
testcase_46 AC 13 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#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;
}
0