結果

問題 No.1356 Split Tile2
ユーザー KoDKoD
提出日時 2021-01-14 18:22:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 14,504 bytes
コンパイル時間 830 ms
コンパイル使用メモリ 85,300 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2024-05-06 10:44:39
合計ジャッジ時間 2,217 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 8 ms
5,376 KB
testcase_14 AC 66 ms
5,656 KB
testcase_15 AC 65 ms
5,924 KB
testcase_16 AC 31 ms
5,376 KB
testcase_17 AC 32 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 31 ms
5,376 KB
testcase_20 AC 31 ms
5,376 KB
testcase_21 AC 32 ms
5,376 KB
testcase_22 AC 8 ms
5,376 KB
testcase_23 AC 32 ms
5,376 KB
testcase_24 AC 31 ms
5,376 KB
testcase_25 AC 16 ms
5,376 KB
testcase_26 AC 32 ms
5,376 KB
testcase_27 AC 15 ms
5,376 KB
testcase_28 AC 31 ms
5,376 KB
testcase_29 AC 31 ms
5,376 KB
testcase_30 AC 16 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 65 ms
6,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>

template <uint32_t Modulus>
class modular {
public:
  using value_type = uint32_t;
  using max_type = uint64_t;

  static constexpr value_type mod = Modulus;
  static constexpr value_type get_mod() { return mod; }
  static_assert(mod >= 2, "invalid mod :: smaller than 2");
  static_assert(mod < (value_type(1) << 31), "invalid mod :: over 2^31");

  template <class T>
  static constexpr value_type normalize(T value_) {
    if (value_ < 0) {
      value_ = -value_;
      value_ %= mod;
      if (value_ == 0) return 0;
      return mod - value_;
    }
    return value_ % mod;
  }

private:
  value_type value;

public:
  constexpr modular(): value(0) { }
  template <class T>
  explicit constexpr modular(T value_): value(normalize(value_)) { }
  template <class T>
  explicit constexpr operator T() { return static_cast<T>(value); }

  constexpr value_type get() const { return value; }
  constexpr modular operator - () const { return modular(mod - value); }
  constexpr modular operator ~ () const { return inverse(); }

  constexpr value_type &extract() { return value; }
  constexpr modular inverse() const { return power(mod - 2); }
  constexpr modular power(max_type exp) const {
    modular res(1), mult(*this);
    while (exp > 0) {
      if (exp & 1) res *= mult;
      mult *= mult;
      exp >>= 1;
    }
    return res;
  }

  constexpr modular operator + (const modular &rhs) const { return modular(*this) += rhs; }
  constexpr modular& operator += (const modular &rhs) { 
    if ((value += rhs.value) >= mod) value -= mod; 
    return *this; 
  }

  constexpr modular operator - (const modular &rhs) const { return modular(*this) -= rhs; }
  constexpr modular& operator -= (const modular &rhs) { 
    if ((value += mod - rhs.value) >= mod) value -= mod; 
    return *this; 
  }

  constexpr modular operator * (const modular &rhs) const { return modular(*this) *= rhs; }
  constexpr modular& operator *= (const modular &rhs) { 
    value = (max_type) value * rhs.value % mod;
    return *this;
  }

  constexpr modular operator / (const modular &rhs) const { return modular(*this) /= rhs; }
  constexpr modular& operator /= (const modular &rhs) { return (*this) *= rhs.inverse(); }

  constexpr bool zero() const { return value == 0; }
  constexpr bool operator == (const modular &rhs) const { return value == rhs.value; }
  constexpr bool operator != (const modular &rhs) const { return value != rhs.value; }
  friend std::ostream& operator << (std::ostream &stream, const modular &rhs) {
    return stream << rhs.value;
  }

};

namespace ntt_detail {

  constexpr uint32_t calc_primitive_root(uint32_t mod) {
    uint32_t exp[32] = {};
    uint32_t cur = mod - 1;
    size_t size = 0;
    for (uint32_t i = 2; i * i <= cur; ++i) {
      if (cur % i == 0) {
        exp[size++] = (mod - 1) / i;
        while (cur % i == 0) cur /= i;
      }
    }
    if (cur != 1) {
      exp[size++] = (mod - 1) / cur;
    }
    uint32_t res = 2;
    while (true) {
      bool ok = true;
      for (size_t i = 0; i < size; ++i) {
        uint64_t a = res, e = exp[i], x = 1;
        while (e > 0) {
          if (e & 1) (x *= a) %= mod;
          (a *= a) %= mod;
          e >>= 1;
        }
        if (x == 1) {
          ok = false;
          break;
        }
      }
      if (ok) break;
      ++res;
    }
    return res;
  };

  template <size_t N, class T>
  constexpr std::array<T, N> calculate_roots(T omega) {
    std::array<T, N> res;
    res[N - 1] = omega;
    for (size_t i = N - 1; i > 0; --i) {
      res[i - 1] = res[i] * res[i];
    }
    return res;
  }

  template <class OtherModular, class Modular>
  constexpr OtherModular convert_mod(Modular x) {
    return OtherModular(x.get());
  }

  template <class OtherModular, class Modular>
  std::vector<OtherModular> convert_mod_vec(const std::vector<Modular> &vec) {
    std::vector<OtherModular> res(vec.size());
    std::transform(vec.cbegin(), vec.cend(), res.begin(), convert_mod<OtherModular, Modular>);
    return res;
  }

  namespace bit_operation {
    constexpr uint32_t b16 = 0b00000000000000001111111111111111;
    constexpr uint32_t  b8 = 0b00000000111111110000000011111111;
    constexpr uint32_t  b4 = 0b00001111000011110000111100001111;
    constexpr uint32_t  b2 = 0b00110011001100110011001100110011;
    constexpr uint32_t  b1 = 0b01010101010101010101010101010101;
    constexpr size_t reverse(size_t x) {
      x = ((x >> 16) & b16) | ((x & b16) << 16);
      x = ((x >>  8) &  b8) | ((x &  b8) <<  8);
      x = ((x >>  4) &  b4) | ((x &  b4) <<  4);
      x = ((x >>  2) &  b2) | ((x &  b2) <<  2);
      x = ((x >>  1) &  b1) | ((x &  b1) <<  1);
      return x;
    }
  };

  namespace garner_mod {
    constexpr uint32_t m0 = 754974721;
    constexpr uint32_t m1 = 167772161;
    constexpr uint32_t m2 = 469762049;
    constexpr uint64_t m0m1 = (uint64_t) m0 * m1;
    constexpr auto im0_m1 = modular<m1>(m0).inverse();
    constexpr auto im0m1_m2 = modular<m2>(m0m1).inverse();
  };

  /*
    prime numbers for ntt
    [ 1051721729 ]  [ 2^20 ]
    [ 1045430273 ]  [ 2^20 ]
    [ 1007681537 ]  [ 2^20 ]
    [  962592769 ]  [ 2^21 ]
    [  924844033 ]  [ 2^21 ]
    [  985661441 ]  [ 2^22 ]
    [  943718401 ]  [ 2^22 ]
    [  935329793 ]  [ 2^22 ]
    [  998244353 ]  [ 2^23 ]
    [  754974721 ]  [ 2^24 ]
    [  167772161 ]  [ 2^25 ]
    [  469762049 ]  [ 2^26 ]
  */

}

template <uint32_t Modulus, class Modular = modular<Modulus>>
class number_theoretic_transform {
public:
  using value_type = Modular;
  static constexpr uint32_t mod = Modulus;
  static constexpr uint32_t prim = ntt_detail::calc_primitive_root(mod);

private:
  static constexpr size_t level = __builtin_ctz(mod - 1);
  static constexpr value_type unit = value_type(1);
  static constexpr value_type omega = value_type(prim).power((mod - 1) >> level); 
  static constexpr auto roots = ntt_detail::calculate_roots<level>(omega);
  static constexpr auto inv_roots = ntt_detail::calculate_roots<level>(omega.inverse());

protected:
  void M_transform(std::vector<value_type> &F) const {
    size_t size = F.size();
    size_t logn = __builtin_ctz(size);
    for (size_t i = 0; i < size; ++i) {
      size_t j = ntt_detail::bit_operation::reverse(i) >> (32 - logn);
      if (i < j) {
        std::swap(F[i], F[j]);
      }
    }
    value_type coeff = unit;
    for (size_t s = 0; s < logn; ++s) {
      size_t mh = 1 << s;
      size_t m = mh << 1;
      for (size_t i = 0; i < size; i += m) {
        coeff = unit;
        for (size_t j = i; j < i + mh; ++j) {
          auto a = F[j];
          auto b = F[j + mh] * coeff;
          F[j] = a + b;
          F[j + mh] = a - b;
          coeff *= roots[s];
        }
      }
    }
  }

  void M_inv_transform(std::vector<value_type> &F) const {
    size_t size = F.size();
    size_t logn = __builtin_ctz(size);
    for (size_t i = 0; i < size; ++i) {
      size_t j = ntt_detail::bit_operation::reverse(i) >> (32 - logn);
      if (i < j) {
        std::swap(F[i], F[j]);
      }
    }
    value_type coeff = unit;
    for (size_t s = 0; s < logn; ++s) {
      size_t mh = 1 << s;
      size_t m = mh << 1;
      for (size_t i = 0; i < size; i += m) {
        coeff = unit;
        for (size_t j = i; j < i + mh; ++j) {
          auto a = F[j];
          auto b = F[j + mh] * coeff;
          F[j] = a + b;
          F[j + mh] = a - b;
          coeff *= inv_roots[s];
        }
      }
    }
    coeff = value_type(size).inverse();
    for (auto &x: F) {
      x *= coeff;
    }
  }

public:
  std::vector<value_type> convolve(
    std::vector<value_type> A, 
    std::vector<value_type> B, 
    bool same = false
  ) const {
    if (A.empty() || B.empty()) return { };
    size_t res_size = A.size() + B.size() - 1;
    size_t fix_size = 1 << (31 - __builtin_clz(2 * res_size - 1));
    if (same) {
      A.resize(fix_size);
      M_transform(A);
      for (size_t i = 0; i < fix_size; ++i) {
        A[i] *= A[i];
      }
    }
    else {
      A.resize(fix_size);
      B.resize(fix_size);
      M_transform(A);
      M_transform(B);
      for (size_t i = 0; i < fix_size; ++i) {
        A[i] *= B[i];
      }
    }
    M_inv_transform(A);
    A.resize(res_size);
    return A;
  }

  template <class OtherModular>
  std::vector<value_type> convolve_convert(
    const std::vector<OtherModular> &A, 
    const std::vector<OtherModular> &B,
    bool same = false
  ) const {
    return convolve(
      ntt_detail::convert_mod_vec<value_type>(A), 
      ntt_detail::convert_mod_vec<value_type>(B),
      same
    );
  }

};

template <class Modular>
std::vector<Modular> convolve_arbitrary_mod(
  const std::vector<Modular> &A, 
  const std::vector<Modular> &B, 
  bool same = false
) {
  using namespace ntt_detail::garner_mod;
  number_theoretic_transform<m0> ntt0;
  number_theoretic_transform<m1> ntt1;
  number_theoretic_transform<m2> ntt2;
  auto X = ntt0.convolve_convert(A, B, same);
  auto Y = ntt1.convolve_convert(A, B, same);
  auto Z = ntt2.convolve_convert(A, B, same);
  size_t size = X.size();
  std::vector<Modular> res(size);
  for (size_t i = 0; i < size; ++i) {
    uint32_t s = (uint32_t) X[i];
    uint64_t t = (uint64_t) ((Y[i] - modular<m1>(s)) * im0_m1) * m0 + s;
    res[i] = Modular((__uint128_t) ((Z[i] - modular<m2>(t)) * im0m1_m2) * m0m1 + t);
  }
  return res;
}

template <uint32_t Modulus, class Modular = modular<Modulus>>
class formal_power_series: public number_theoretic_transform<Modulus, Modular> {
public:
  using value_type = Modular;
  using size_type = size_t;

private:
  std::vector<value_type> M_data;

public:
  template <class... Args>
  formal_power_series(Args... args): M_data(args...) { }
  formal_power_series(std::initializer_list<value_type> data_): M_data(data_.begin(), data_.end()) { }

  formal_power_series operator + (const formal_power_series &rhs) const { return formal_power_series(*this) += rhs; }
  formal_power_series& operator += (const formal_power_series &rhs) {
    if (M_data.size() < rhs.M_data.size()) M_data.resize(rhs.M_data.size());
    for (size_type i = 0; i < rhs.M_data.size(); ++i) M_data[i] += rhs.M_data[i];
    return *this;
  }

  formal_power_series operator - (const formal_power_series &rhs) const { return formal_power_series(*this) -= rhs; }
  formal_power_series& operator -= (const formal_power_series &rhs) {
    if (M_data.size() < rhs.M_data.size()) M_data.resize(rhs.M_data.size());
    for (size_type i = 0; i < rhs.M_data.size(); ++i) M_data[i] -= rhs.M_data[i];
    return *this;
  }

  formal_power_series operator * (const formal_power_series &rhs) const { return formal_power_series(*this) *= rhs; }
  formal_power_series& operator *= (const formal_power_series &rhs) {
    M_data = this -> convolve(M_data, rhs.M_data);
    return *this;
  }

  formal_power_series operator + (const value_type &rhs) const { return formal_power_series(*this) += rhs; }
  formal_power_series& operator += (const value_type &rhs) { M_data[0] += rhs; return *this; }

  formal_power_series operator - (const value_type &rhs) const { return formal_power_series(*this) -= rhs; }
  formal_power_series& operator -= (const value_type &rhs) { M_data[0] -= rhs; return *this; }

  formal_power_series operator * (const value_type &rhs) const { return formal_power_series(*this) *= rhs; }
  formal_power_series& operator *= (const value_type &rhs) { for (auto &x: M_data) x *= rhs; return *this; }

  formal_power_series operator / (const value_type &rhs) const { return formal_power_series(*this) /= rhs; }
  formal_power_series& operator /= (const value_type &rhs) { return (*this) *= rhs.inverse(); }

  formal_power_series lower(size_type size) const { 
    return formal_power_series(M_data.begin(), M_data.begin() + std::min(M_data.size(), size)); 
  }
  formal_power_series square() const {
    return formal_power_series(this -> convolve(M_data, M_data, true));
  }
  formal_power_series diff() const {
    if (M_data.size() < 1) return formal_power_series();
    formal_power_series res(M_data.size() - 1);
    for (size_type i = 0; i + 1 < M_data.size(); ++i) {
      res.M_data[i] = M_data[i + 1] * value_type(i + 1);
    }
    return res;
  }
  formal_power_series inte() const {
    formal_power_series res(M_data.size() + 1);
    value_type cur(1);
    for (size_type i = 0; i < M_data.size(); ++i) {
      res.M_data[i + 1] = M_data[i] * cur;
      cur *= value_type(i + 1);
    }
    cur = cur.inverse();
    for (size_type i = M_data.size(); i > 0; --i) {
      res.M_data[i] *= cur;
      cur *= value_type(i);
    }
    return res;
  }
  formal_power_series inverse(size_type m) const {
    formal_power_series res(m);
    res.M_data[0] = M_data[0].inverse();
    for (size_type d = 1; d < m; d <<= 1) {
      formal_power_series f = lower(d + d);
      if (f.M_data.size() < d + d) f.M_data.resize(d + d);
      this -> M_transform(f.M_data);
      formal_power_series g = res.lower(d + d);
      if (g.M_data.size() < d + d) g.M_data.resize(d + d);
      this -> M_transform(g.M_data);
      for (size_type i = 0; i < d + d; ++i) f.M_data[i] *= g.M_data[i];
      this -> M_inv_transform(f.M_data);
      for (size_type i = 0; i < d; ++i) f.M_data[i] = value_type();
      this -> M_transform(f.M_data);
      for (size_type i = 0; i < d + d; ++i) f.M_data[i] *= g.M_data[i];
      this -> M_inv_transform(f.M_data);
      size_type right = std::min(d + d, m);
      for (size_type i = d; i < right; ++i) res.M_data[i] = -f.M_data[i];
    }
    return res;
  }
  formal_power_series log(size_type m) const {
    return (lower(m).diff() * inverse(m - 1)).low(m - 1).inte();
  }

  value_type get(size_type i) const { return i >= M_data.size() ? value_type() : M_data[i]; }
  value_type& extract(size_type i) { return M_data[i]; }
  size_type size() const { return M_data.size(); }
  bool empty() const { return M_data.empty(); }

};

int main() {
  using m32 = modular<998244353>;
  using fps = formal_power_series<m32::mod>;
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  size_t N;
  std::cin >> N;
  assert(2 <= N && N <= 100000);
  std::vector<m32> vec(N + 1);
  {
    m32 cur(1);
    for (size_t i = 0; i < N; ++i) {
      cur *= m32(i + 1);
      vec[i] = cur;
    }
  }
  auto ans = fps(vec).inverse(N + 1);
  m32 sum;
  for (size_t i = 1; i < N; ++i) {
    sum += ans.get(i);
  }
  std::cout << -sum << '\n';
  return 0;
}
0