Language: C++


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

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

struct range {
  using int_ = int_fast64_t;
  struct itr {
    int_ i;
    constexpr itr(int_ i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int_ operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int_ l_, int_ r_): l(l_), r(std::max(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

struct revrange {
  using int_ = int_fast64_t;
  struct itr {
    int_ i;
    constexpr itr(int_ i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int_ operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int_ l_, int_ r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

using i32 = int_fast32_t;
using i64 = int_fast64_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

template <uint_fast32_t Modulus>
class modular {
public:
  using value_type = uint_fast32_t;
  using max_type = uint_fast64_t;

  static constexpr value_type mod = Modulus;
  static constexpr value_type mod_min = 1;
  static constexpr value_type mod_max = 2147483647;
  static_assert(mod >= mod_min, "invalid mod :: too small");
  static_assert(mod <= mod_max, "invalid mod :: too big");

  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 operator () () 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;
  }

};

template <class T, std::size_t N>
class factorials {
public:
  using value_type = T;
  static constexpr std::size_t size = N;

public:
  std::array<value_type, size + 1> fact{};
  std::array<value_type, size + 1> fact_inv{};

  factorials() {
    fact.front() = value_type(1);
    for (std::size_t i = 1; i <= size; ++i) {
      fact[i] = fact[i - 1] * value_type(i);
    }
    fact_inv.back() = ~fact.back();
    for (std::size_t i = size; i > 0; --i) {
      fact_inv[i - 1] = fact_inv[i] * value_type(i);
    }
  }

  value_type operator () (std::size_t n, std::size_t r) const {
    return fact[n] * fact_inv[n - r] * fact_inv[r];
  }

};

using m32 = modular<924844033>;
factorials<m32, 1000> fact;

template <class T, int N, int I = 0>
auto gen_vec(const int (&list)[N], typename std::enable_if<(I == N), const T&>::type value = T()) {
  return value;
}
 
template <class T, int N, int I = 0>
auto gen_vec(const int (&list)[N], typename std::enable_if<(I != N), const T&>::type value = T()) {
  return std::vector<decltype(gen_vec<T, N, I + 1>(list, value))>(list[I], gen_vec<T, N, I + 1>(list, value));
}

int main() {
  i32 N, K;
  std::cin >> N >> K;
  if (K > (N - 1) / 2) {
    m32 ans;
    i32 size = 2 * (N - K);
    for (auto i: range(0, size + 1)) {
      if (i & 1) ans -= fact(size, i) * fact.fact[N - i];
      else ans += fact(size, i) * fact.fact[N - i];
    }
    std::cout << ans << '\n';
  }
  else {
    auto dp = gen_vec<m32>({ 2 * K + 1, N + 1 });
    dp[0][0] = (m32) 1;
    for (auto i: range(0, 2 * K)) {
      i32 size = N / (2 * K) + i32(N % (2 * K) > i);
      auto ndp = gen_vec<m32>({ size, size + 1, 2 });
      ndp[0][1][1] = (m32) 1;
      ndp[0][0][0] = (m32) 1;
      if (i >= K) ndp[0][1][0] = (m32) 1;
      for (auto j: range(0, size - 1)) {
        for (auto k: range(0, j + 2)) {
          ndp[j + 1][k][0] += ndp[j][k][0];
          ndp[j + 1][k][0] += ndp[j][k][1];
          ndp[j + 1][k + 1][0] += ndp[j][k][0];
          if (i + (j + 1) * (2 * K) + K < N) {
            ndp[j + 1][k + 1][1] += ndp[j][k][0];
            ndp[j + 1][k + 1][1] += ndp[j][k][1];
          }
        }
      }
      for (auto j: range(0, N + 1)) {
        for (auto k: range(0, size + 1)) {
          if (j + k <= N) {
            dp[i + 1][j + k] += dp[i][j] * (ndp[size - 1][k][0] + ndp[size - 1][k][1]);
          }
        }
      }
    }
    m32 ans;
    for (auto i: range(0, N + 1)) {
      if (i & 1) ans -= dp[2 * K][i] * fact.fact[N - i];
      else ans += dp[2 * K][i] * fact.fact[N - i];
    }
    std::cout << ans << '\n';
  }
  return 0;
}


Compiler output:
a.cc:22:16: error: 'int_fast64_t' does not name a type
   22 |   using int_ = int_fast64_t;
      |                ^~~~~~~~~~~~
a.cc:24:5: error: 'int_' does not name a type; did you mean 'int'?
   24 |     int_ i;
      |     ^~~~
      |     int
a.cc:25:23: error: expected ')' before 'i_'
   25 |     constexpr itr(int_ i_): i(i_) { }
      |                  ~    ^~~
      |                       )
a.cc:27:15: error: 'int_' does not name a type; did you mean 'int'?
   27 |     constexpr int_ operator * () const { return i; }
      |               ^~~~
      |               int
a.cc:31:23: error: expected ')' before 'l_'
   31 |   constexpr range(int_ l_, int_ r_): l(l_), r(std::max(l_, r_)) { }
      |                  ~    ^~~
      |                       )
a.cc: In member function 'constexpr void range::itr::operator++()':
a.cc:26:39: error: 'i' was not declared in this scope
   26 |     constexpr void operator ++ () { ++i; }
      |                                       ^
a.cc: In member function 'constexpr bool range::itr::operator!=(range::itr) const':
a.cc:28:55: error: 'i' was not declared in this scope
   28 |     constexpr bool operator != (itr x) const { return i != x.i; }
      |                                                       ^
a.cc:28:62: error: 'struct range::itr' has no member named 'i'
   28 |     constexpr bool operator != (itr x) const { return i != x.i; }
      |                                                              ^
a.cc: At global scope:
a.cc:37:16: error: 'int_fast64_t' does not name a type
   37 |   using int_ = int_fast64_t;
      |                ^~~~~~~~~~~~
a.cc:39:5: error: 'int_' does not name a type; did you mean 'int'?
   39 |     int_ i;
      |     ^~~~
      |     int
a.cc:40:23: error: expected ')' before 'i_'
   40 |     constexpr itr(int_ i_): i(i_) { }
      |                  ~    ^~~
      |                       )
a.cc:42:15: error: 'int_' does not name a type; did you mean 'int'?
   42 |     constexpr int_ operator * () const { return i; }
      |               ^~~~
      |               int
a.cc:46:26: error: expected ')' before 'l_'
   46 |   constexpr revrange(int_ l_, int_ r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
      |                     ~    ^~~
      |                          )
a.cc: In member function 'constexpr void revrange::itr::operator++()':
a.cc:41:39: error: 'i' was not declared in this scope
   41 |     constexpr void operator ++ () { --i; }
      |                                       ^
a.cc: In member function 'constexpr bool revrange::itr::operator!=(revrange::itr) const':
a.cc:43:55: error: 'i' was not declared in this scope
   43 |     constexpr bool operator != (itr x) const { return i != x.i; }
      |                                                       ^
a.cc:43:62: error: 'struct revrange::itr' has no member named 'i'
   43 |     constexpr bool operator != (itr x) const { return i != x.i; }
      |                                                              ^
a.cc: At global scope:
a.cc:51:13: error: 'int_fast32_t' does not name a type
   51 | using i32 = int_fast32_t;
      |             ^~~~~~~~~~~~
a.cc:52:13: error: 'int_fast64_t' does not name a type
   52 | using i64 = int_fast64_t;
      |             ^~~~~~~~~~~~
a.cc:53:13: error: 'uint_fast32_t' does not name a type
   53 | using u32 = uint_fast32_t;
      |             ^~~~~~~~~~~~~
a.cc:54:13: error: 'uint_fast64_t' does not name a type
   54 | using u64 = uint_fast64_t;
      |             ^~~~~~~~~~~~~
a.cc:56:11: error: 'i32' does not name a type
   56 | constexpr i32 inf32 = (i32(1) << 30) - 1;
      |           ^~~
a.cc:57:11: error: 'i64' does not name a type
   57 | constexpr i64 inf64 = (i64(1) << 62) - 1;
      |           ^~~
a.cc:59:11: error: 'uint_fast32_t' has not been declared
   59 | template <uint_fast32_t Modulus>
      |           ^~~~~~~~~~~~~
a.cc:62:22: error: 'uint_fast32_t' does not name a type
   62 |   using value_type = uint_fast32_t;
      |                      ^~~~~~~~~~~~~
a.cc:63:20: error: 'uint_fast64_t' does not name a type
   63 |   using max_type = uint_fast64_t;
      |                    ^~~~~~~~~~~~~
a.cc:65:20: error: 'value_type' does not name a type
   65 |   static constexpr value_type mod = Modulus;
      |                    ^~~~~~~~~~
a.cc:66:20: error: 'value_type' does not name a type
   66 |   static constexpr value_type mod_min = 1;
      |                    ^~~~~~~~~~
a.cc:67:20: error: 'value_type' does not name a type
   67 |   static constexpr value_type mod_max = 2147483647;
      |                    ^~~~~~~~~~
a.cc:68:17: error: 'mod' was not declared in this scope
   68 |   static_assert(mod >= mod_min, "invalid mod :: too small");
      |                 ^~~
a.cc:68:24: error: 'mod_min' was not declared in this scope
   68 |   static_assert(mod >= mod_min, "invalid mod :: too small");
      |                        ^~~~~~~
a.cc:69:17: error: 'mod' was not declared in this scope
   69 |   static_assert(mod <= mod_max, "invalid mod :: too big");
      |                 ^~~
a.cc:69:24: error: 'mod_max' was not declared in this scope
   69 |   static_assert(mod <= mod_max, "invalid mod :: too big");
      |                        ^~~~~~~
a.cc:72:20: error: 'value_type' does not name a type
   72 |   static constexpr value_type normalize(T value_) {
      |                    ^~~~~~~~~~
a.cc:83:3: error: 'value_type' does not name a type
   83 |   value_type value;
      |   ^~~~~~~~~~
a.cc:92:13: error: 'value_type' does not name a type
   92 |   constexpr value_type operator () () const { return value; }
      |             ^~~~~~~~~~
a.cc:96:13: error: 'value_type' does not name a type
   96 |   constexpr value_type &extract() { return value; }
      |             ^~~~~~~~~~
a.cc:98:27: error: 'max_type' has not been declared
   98 |   constexpr modular power(max_type exp) const {
      |                           ^~~~~~~~
a.cc:166:12: error: 'm32' was not declared in this scope
  166 | factorials<m32, 1000> fact;
      |            ^~~
a.cc:166:21: error: template argument 1 is invalid
  166 | factorials<m32, 1000> fact;
      |                     ^
a.cc: In function 'int main()':
a.cc:179:3: error: 'i32' was not declared in this scope
  179 |   i32 N, K;
      |   ^~~
a.cc:180:15: error: 'N' was not declared in this scope
  180 |   std::cin >> N >> K;
      |               ^
a.cc:180:20: error: 'K' was not declared in this scope
  180 |   std::cin >> N >> K;
      |                    ^
a.cc:182:5: error: 'm32' was not declared in this scope
  182 |     m32 ans;
      |     ^~~
a.cc:183:8: error: expected ';' before 'size'
  183 |     i32 size = 2 * (N - K);
      |        ^~~~~
      |        ;
a.cc:184:27: error: 'size' was not declared in this scope; did you mean 'std::size'?
  184 |     for (auto i: range(0, size + 1)) {
      |                           ^~~~
      |                           std::size
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/range_access.h:272:5: note: 'std::size' declared here
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
a.cc:185:18: error: 'ans' was not declared in this scope; did you mean 'abs'?
  185 |       if (i & 1) ans -= fact(size, i) * fact.fact[N - i];
      |                  ^~~
      |                  abs
a.cc:185:29: error: 'fact' cannot be used as a function
  185 |       if (i & 1) ans -= fact(size, i) * fact.fact[N - i];
      |                         ~~~~^~~~~~~~~
a.cc:185:46: error: request for member 'fact' in 'fact', which is of non-class type 'int'
  185 |       if (i & 1) ans -= fact(size, i) * fact.fact[N - i];
      |                                              ^~~~
a.cc:186:12: error: 'ans' was not declared in this scope; did you mean 'abs'?
  186 |       else ans += fact(size, i) * fact.fact[N - i];
      |            ^~~
      |            abs
a.cc:186:23: error: 'fact' cannot be used as a function
  186 |       else ans += fact(size, i) * fact.fact[N - i];
      |                   ~~~~^~~~~~~~~
a.cc:186:40: error: request for member 'fact' in 'fact', which is of non-class type 'int'
  186 |       else ans += fact(size, i) * fact.fact[N - i];
      |                                        ^~~~
a.cc:188:18: error: 'ans' was not declared in this scope; did you mean 'abs'?
  188 |     std::cout << ans << '\n';
      |                  ^~~
      |                  abs
a.cc:191:23: error: 'm32' was not declared in this scope
  191 |     auto dp = gen_vec<m32>({ 2 * K + 1, N + 1 });
      |                       ^~~
a.cc:191:27: error: no matching function for call to 'gen_vec<<expression error> >(<brace-enclosed initializer list>)'
  191 |     auto dp = gen_vec<m32>({ 2 * K + 1, N + 1 });
      |               ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:169:6: note: candidate: 'template<class T, int N, int I> auto gen_vec(const int (&)[N], typename std::enable_if<(I == N), const T&>::type)'
  169 | auto gen_vec(const int (&list)[N], typename std::enable_if<(I == N), const T&>::type value = T()) {
      |      ^~~~~~~
a.cc:169:6: note:   template argument deduction/substitution failed:
a.cc:191:27: error: template argument 1 is invalid
  191 |     auto dp = gen_vec<m32>({ 2 * K + 1, N + 1 });
      |               ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:174:6: note: candidate: 'template<class T, int N, int I> auto gen_vec(const int (&)[N], typename std::enable_if<(I != N), const T&>::type)'
  174 | auto gen_vec(const int (&list)[N], typename std::enable_if<(I != N), const T&>::type value = T()) {
      |      ^~~~~~~
a.cc:174:6: note:   template argument deduction/substitution failed:
a.cc:191:27: error: template argument 1 is invalid
  191 |     auto dp = gen_vec<m32>({ 2 * K + 1, N + 1 });
      |               ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:194:10: error: expected ';' before 'size'
  194 |       i32 size = N