結果

問題 No.2137 Stairs of Permutation
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-11-25 21:30:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,183 ms / 2,000 ms
コード長 8,359 bytes
コンパイル時間 1,463 ms
コンパイル使用メモリ 115,528 KB
実行使用メモリ 42,620 KB
最終ジャッジ日時 2024-04-10 02:43:07
合計ジャッジ時間 15,518 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 200 ms
9,972 KB
testcase_03 AC 828 ms
30,816 KB
testcase_04 AC 690 ms
26,112 KB
testcase_05 AC 528 ms
20,812 KB
testcase_06 AC 1,077 ms
39,908 KB
testcase_07 AC 1,138 ms
41,836 KB
testcase_08 AC 123 ms
7,620 KB
testcase_09 AC 58 ms
6,944 KB
testcase_10 AC 171 ms
9,104 KB
testcase_11 AC 1,041 ms
38,052 KB
testcase_12 AC 394 ms
16,512 KB
testcase_13 AC 572 ms
22,124 KB
testcase_14 AC 847 ms
31,056 KB
testcase_15 AC 1,155 ms
42,028 KB
testcase_16 AC 375 ms
16,176 KB
testcase_17 AC 335 ms
14,600 KB
testcase_18 AC 518 ms
20,732 KB
testcase_19 AC 422 ms
17,740 KB
testcase_20 AC 282 ms
12,812 KB
testcase_21 AC 1,142 ms
41,168 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 1,183 ms
42,620 KB
testcase_24 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////


// 0 = \sum_{j=0}^d (\sum_{k=0}^e css[j][k] i^k) as[i - j]  (d <= i < |as|)
template <unsigned M>
vector<vector<ModInt<M>>> findPRecurrence(const vector<ModInt<M>> &as, int e,
                                          bool verbose = false) {
  using Mint = ModInt<M>;
  const int asLen = as.size();
  // asLen - d >= (d + 1) (e + 1) - 1  (otherwise definitely  dim Ker >= 2)
  const int d0 = (asLen + 2) / (e + 2) - 1;
  if (d0 < 0) {
    if (verbose) {
      fprintf(stderr, "[findPRecurrence] |as| >= e  must hold.\n");
      fflush(stderr);
    }
    return {};
  }
  const int m = asLen - d0, n = (d0 + 1) * (e + 1);
  vector<vector<Mint>> bss(m, vector<Mint>(n, 0));
  for (int i = d0; i < asLen; ++i) for (int j = 0; j <= d0; ++j) {
    Mint pw = 1;
    for (int k = 0; k <= e; ++k) {
      bss[i - d0][j * (e + 1) + k] = pw * as[i - j];
      pw *= i;
    }
  }
  int r = 0;
  vector<int> hs;
  for (int h = 0; h < n; ++h) {
    for (int i = r; i < m; ++i) if (bss[i][h]) {
      bss[r].swap(bss[i]);
      break;
    }
    if (r < m && bss[r][h]) {
      const Mint s = bss[r][h].inv();
      for (int j = h; j < n; ++j) bss[r][j] *= s;
      for (int i = 0; i < m; ++i) if (i != r) {
        const Mint t = bss[i][h];
        for (int j = h; j < n; ++j) bss[i][j] -= t * bss[r][j];
      }
      ++r;
    } else {
      hs.push_back(h);
    }
  }
  if (hs.empty()) {
    if (verbose) {
      fprintf(stderr, "[findPRecurrence] Not found: d = %d, e = %d.\n", d0, e);
      fflush(stderr);
    }
    return {};
  }
  vector<vector<Mint>> css(d0 + 1, vector<Mint>(e + 1, 0));
  for (int j = 0; j <= d0; ++j) for (int k = 0; k <= e; ++k) {
    const int h = j * (e + 1) + k;
    css[j][k] = (h < hs[0]) ? -bss[h][hs[0]] : (h == hs[0]) ? 1 : 0;
  }
  int d = hs[0] / (e + 1);
  for (int i = d0; i < asLen; ++i) {
    Mint sum = 0;
    for (int j = 0; j <= d0; ++j) {
      Mint coef = 0, pw = 1;
      for (int k = 0; k <= e; ++k) {
        coef += css[j][k] * pw;
        pw *= i;
      }
      sum += coef * as[i - j];
    }
    for (; sum; ) {
      if (i - ++d < 0) break;
      assert(d <= d0);
      Mint coef = 0, pw = 1;
      for (int k = 0; k <= e; ++k) {
        coef += css[d][k] * pw;
        pw *= i;
      }
      sum += coef * as[i - d];
    }
  }
  css.resize(d + 1);
  if (verbose) {
    const int hsLen = hs.size();
    if (hsLen > d0 - d + 1) {
      fprintf(stderr, "[findPRecurrence] Degenerate? (dim Ker = %d)\n", hsLen);
    }
    fprintf(stderr, "[findPRecurrence] d = %d, e = %d, non-trivial: %d\n", d, e,
            asLen - d - (d + 1) * (e + 1) + 1);
    fprintf(stderr, "{\n");
    for (int j = 0; j <= d; ++j) {
      fprintf(stderr, "  {");
      for (int k = 0; k <= e; ++k) {
        const unsigned c = css[j][k].x;
        if (k > 0) fprintf(stderr, ", ");
        fprintf(stderr, "%d", static_cast<int>((c < M - c) ? c : (c - M)));
      }
      fprintf(stderr, "},\n");
    }
    fprintf(stderr, "}\n");
    fflush(stderr);
  }
  return css;
}

// 0 = \sum_{j=0}^d (\sum_{k=0}^e css[j][k] i^k) as[i - j]  (d <= i < |as|)
template <unsigned M>
vector<ModInt<M>> extendPRecurrence(vector<ModInt<M>> as,
                                    const vector<vector<ModInt<M>>> &css,
                                    int n) {
  using Mint = ModInt<M>;
  assert(!css.empty());
  const int d = css.size() - 1, e = css[0].size() - 1;
  for (int j = 0; j <= d; ++j) assert(static_cast<int>(css[j].size()) == e + 1);
  const int asLen = as.size();
  as.resize(n);
  for (int i = asLen; i < n; ++i) {
    Mint sum = 0;
    for (int j = 1; j <= d; ++j) {
      Mint coef = 0, pw = 1;
      for (int k = 0; k <= e; ++k) {
        coef += css[j][k] * pw;
        pw *= i;
      }
      sum += coef * as[i - j];
    }
    {
      Mint coef = 0, pw = 1;
      for (int k = 0; k <= e; ++k) {
        coef += css[0][k] * pw;
        pw *= i;
      }
      as[i] = -sum / coef;
    }
  }
  return as;
}

////////////////////////////////////////////////////////////////////////////////


constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;

constexpr int M = 100;
Mint c[M][M];

int main() {
  for (int n = 0; n < M; ++n) {
    c[n][0] = 0;
    c[n][n] = 1;
    for (int k = 1; k < n; ++k) {
      c[n][k] = c[n - 1][k - 1] + (n - 1) * c[n - 1][k];
    }
  }
  vector<Mint> as(M);
  for (int n = 0; n < M; ++n) {
    for (int k = 0; k <= n; ++k) {
      as[n] += c[n][k] * k * k * k;
    }
  }
cerr<<"as = "<<as<<endl;
  /*
  for (int e = 0; e <= 10; ++e) {
    findPRecurrence(as, e, true);
  }
  */
  const auto css = findPRecurrence(as, 4, true);
  
  int N;
  for (; ~scanf("%d", &N); ) {
    const auto res = extendPRecurrence(as, css, N + 1);
    printf("%u\n", res[N].x);
  }
  return 0;
}
0