結果
問題 | No.2137 Stairs of Permutation |
ユーザー | 👑 hos.lyric |
提出日時 | 2022-11-25 21:30:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,063 ms / 2,000 ms |
コード長 | 8,359 bytes |
コンパイル時間 | 1,487 ms |
コンパイル使用メモリ | 117,872 KB |
実行使用メモリ | 42,496 KB |
最終ジャッジ日時 | 2024-10-02 04:04:25 |
合計ジャッジ時間 | 14,107 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 23 |
ソースコード
#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; }