Language: C++


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

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, size_t N, size_t I = 0>
decltype(auto) gen_vec(const size_t (&list)[N], typename std::enable_if<(I == N), const T&>::type value = T()) { 
  return value; 
}

template <class T, size_t N, size_t I = 0>
decltype(auto) gen_vec(const size_t (&list)[N], typename std::enable_if<(I != N), const T&>::type value = T()) { 
  return std::vector(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:21:16: error: 'int_fast64_t' does not name a type
   21 |   using int_ = int_fast64_t;
      |                ^~~~~~~~~~~~
a.cc:23:5: error: 'int_' does not name a type; did you mean 'int'?
   23 |     int_ i;
      |     ^~~~
      |     int
a.cc:24:23: error: expected ')' before 'i_'
   24 |     constexpr itr(int_ i_): i(i_) { }
      |                  ~    ^~~
      |                       )
a.cc:26:15: error: 'int_' does not name a type; did you mean 'int'?
   26 |     constexpr int_ operator * () const { return i; }
      |               ^~~~
      |               int
a.cc:30:23: error: expected ')' before 'l_'
   30 |   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:25:39: error: 'i' was not declared in this scope
   25 |     constexpr void operator ++ () { ++i; }
      |                                       ^
a.cc: In member function 'constexpr bool range::itr::operator!=(range::itr) const':
a.cc:27:55: error: 'i' was not declared in this scope
   27 |     constexpr bool operator != (itr x) const { return i != x.i; }
      |                                                       ^
a.cc:27:62: error: 'struct range::itr' has no member named 'i'
   27 |     constexpr bool operator != (itr x) const { return i != x.i; }
      |                                                              ^
a.cc: At global scope:
a.cc:36:16: error: 'int_fast64_t' does not name a type
   36 |   using int_ = int_fast64_t;
      |                ^~~~~~~~~~~~
a.cc:38:5: error: 'int_' does not name a type; did you mean 'int'?
   38 |     int_ i;
      |     ^~~~
      |     int
a.cc:39:23: error: expected ')' before 'i_'
   39 |     constexpr itr(int_ i_): i(i_) { }
      |                  ~    ^~~
      |                       )
a.cc:41:15: error: 'int_' does not name a type; did you mean 'int'?
   41 |     constexpr int_ operator * () const { return i; }
      |               ^~~~
      |               int
a.cc:45:26: error: expected ')' before 'l_'
   45 |   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:40:39: error: 'i' was not declared in this scope
   40 |     constexpr void operator ++ () { --i; }
      |                                       ^
a.cc: In member function 'constexpr bool revrange::itr::operator!=(revrange::itr) const':
a.cc:42:55: error: 'i' was not declared in this scope
   42 |     constexpr bool operator != (itr x) const { return i != x.i; }
      |                                                       ^
a.cc:42:62: error: 'struct revrange::itr' has no member named 'i'
   42 |     constexpr bool operator != (itr x) const { return i != x.i; }
      |                                                              ^
a.cc: At global scope:
a.cc:50:13: error: 'int_fast32_t' does not name a type
   50 | using i32 = int_fast32_t;
      |             ^~~~~~~~~~~~
a.cc:51:13: error: 'int_fast64_t' does not name a type
   51 | using i64 = int_fast64_t;
      |             ^~~~~~~~~~~~
a.cc:52:13: error: 'uint_fast32_t' does not name a type
   52 | using u32 = uint_fast32_t;
      |             ^~~~~~~~~~~~~
a.cc:53:13: error: 'uint_fast64_t' does not name a type
   53 | using u64 = uint_fast64_t;
      |             ^~~~~~~~~~~~~
a.cc:55:11: error: 'i32' does not name a type
   55 | constexpr i32 inf32 = (i32(1) << 30) - 1;
      |           ^~~
a.cc:56:11: error: 'i64' does not name a type
   56 | constexpr i64 inf64 = (i64(1) << 62) - 1;
      |           ^~~
a.cc:58:11: error: 'uint_fast32_t' has not been declared
   58 | template <uint_fast32_t Modulus>
      |           ^~~~~~~~~~~~~
a.cc:61:22: error: 'uint_fast32_t' does not name a type
   61 |   using value_type = uint_fast32_t;
      |                      ^~~~~~~~~~~~~
a.cc:62:20: error: 'uint_fast64_t' does not name a type
   62 |   using max_type = uint_fast64_t;
      |                    ^~~~~~~~~~~~~
a.cc:64:20: error: 'value_type' does not name a type
   64 |   static constexpr value_type mod = Modulus;
      |                    ^~~~~~~~~~
a.cc:65:20: error: 'value_type' does not name a type
   65 |   static constexpr value_type mod_min = 1;
      |                    ^~~~~~~~~~
a.cc:66:20: error: 'value_type' does not name a type
   66 |   static constexpr value_type mod_max = 2147483647;
      |                    ^~~~~~~~~~
a.cc:67:17: error: 'mod' was not declared in this scope
   67 |   static_assert(mod >= mod_min, "invalid mod :: too small");
      |                 ^~~
a.cc:67:24: error: 'mod_min' was not declared in this scope
   67 |   static_assert(mod >= mod_min, "invalid mod :: too small");
      |                        ^~~~~~~
a.cc:68:17: error: 'mod' was not declared in this scope
   68 |   static_assert(mod <= mod_max, "invalid mod :: too big");
      |                 ^~~
a.cc:68:24: error: 'mod_max' was not declared in this scope
   68 |   static_assert(mod <= mod_max, "invalid mod :: too big");
      |                        ^~~~~~~
a.cc:71:20: error: 'value_type' does not name a type
   71 |   static constexpr value_type normalize(T value_) {
      |                    ^~~~~~~~~~
a.cc:82:3: error: 'value_type' does not name a type
   82 |   value_type value;
      |   ^~~~~~~~~~
a.cc:91:13: error: 'value_type' does not name a type
   91 |   constexpr value_type operator () () const { return value; }
      |             ^~~~~~~~~~
a.cc:95:13: error: 'value_type' does not name a type
   95 |   constexpr value_type &extract() { return value; }
      |             ^~~~~~~~~~
a.cc:97:27: error: 'max_type' has not been declared
   97 |   constexpr modular power(max_type exp) const {
      |                           ^~~~~~~~
a.cc:165:12: error: 'm32' was not declared in this scope
  165 | factorials<m32, 1000> fact;
      |            ^~~
a.cc:165:21: error: template argument 1 is invalid
  165 | factorials<m32, 1000> fact;
      |                     ^
a.cc: In function 'int main()':
a.cc:178:3: error: 'i32' was not declared in this scope
  178 |   i32 N, K;
      |   ^~~
a.cc:179:15: error: 'N' was not declared in this scope
  179 |   std::cin >> N >> K;
      |               ^
a.cc:179:20: error: 'K' was not declared in this scope
  179 |   std::cin >> N >> K;
      |                    ^
a.cc:181:5: error: 'm32' was not declared in this scope
  181 |     m32 ans;
      |     ^~~
a.cc:182:8: error: expected ';' before 'size'
  182 |     i32 size = 2 * (N - K);
      |        ^~~~~
      |        ;
a.cc:183:27: error: 'size' was not declared in this scope; did you mean 'std::size'?
  183 |     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:184:18: error: 'ans' was not declared in this scope; did you mean 'abs'?
  184 |       if (i & 1) ans -= fact(size, i) * fact.fact[N - i];
      |                  ^~~
      |                  abs
a.cc:184:29: error: 'fact' cannot be used as a function
  184 |       if (i & 1) ans -= fact(size, i) * fact.fact[N - i];
      |                         ~~~~^~~~~~~~~
a.cc:184:46: error: request for member 'fact' in 'fact', which is of non-class type 'int'
  184 |       if (i & 1) ans -= fact(size, i) * fact.fact[N - i];
      |                                              ^~~~
a.cc:185:12: error: 'ans' was not declared in this scope; did you mean 'abs'?
  185 |       else ans += fact(size, i) * fact.fact[N - i];
      |            ^~~
      |            abs
a.cc:185:23: error: 'fact' cannot be used as a function
  185 |       else ans += fact(size, i) * fact.fact[N - i];
      |                   ~~~~^~~~~~~~~
a.cc:185:40: error: request for member 'fact' in 'fact', which is of non-class type 'int'
  185 |       else ans += fact(size, i) * fact.fact[N - i];
      |                                        ^~~~
a.cc:187:18: error: 'ans' was not declared in this scope; did you mean 'abs'?
  187 |     std::cout << ans << '\n';
      |                  ^~~
      |                  abs
a.cc:190:23: error: 'm32' was not declared in this scope
  190 |     auto dp = gen_vec<m32>({ 2 * K + 1, N + 1 });
      |                       ^~~
a.cc:190:27: error: no matching function for call to 'gen_vec<<expression error> >(<brace-enclosed initializer list>)'
  190 |     auto dp = gen_vec<m32>({ 2 * K + 1, N + 1 });
      |               ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:168:16: note: candidate: 'template<class T, long unsigned int N, long unsigned int I> decltype(auto) gen_vec(const size_t (&)[N], typename std::enable_if<(I == N), const T&>::type)'
  168 | decltype(auto) gen_vec(const size_t (&list)[N], typename std::enable_if<(I == N), const T&>::type value = T()) {
      |                ^~~~~~~
a.cc:168:16: note:   template argument deduction/substitution failed:
a.cc:190:27: error: template argument 1 is invalid
  190 |     auto dp = gen_vec<m32>({ 2 * K + 1, N + 1 });
      |               ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:173:16: note: candidate: 'template<class T, long unsigned int N, long unsigned int I> decltype(auto) gen_vec(const size_t (&)[N], typename std::enable_if<(I != N), const T&>::type)'
  173 | decltype(auto) gen_vec(const size_t (&list)[N], typename std::enable_if<(I != N), const T&>::type value = T()) {
      |                ^~~~~~~
a.cc:173:16: note:   template argument deduction/substitution failed:
a.cc:190:27: error: template argument 1 is invalid
  190 |     auto dp = gen_vec<m32>({ 2 * K + 1, N + 1 })