#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector& vec, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.maxiesize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template vector sort_unique(vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template istream &operator>>(istream &is, tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template ostream &operator<<(ostream &os, const tuple &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } #endif template ostream &operator<<(ostream &os, const deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const pair &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template ostream &operator<<(ostream &os, const map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl #define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl : cerr) #else #define dbg(x) (x) #define dbgif(cond, x) 0 #endif // StarrySkyTree: segment tree for Range Minimum Query & Range Add Query // Complexity: (N)$ (construction), (\log N)$ (add / get / prod) // - RangeAddRangeMin(std::vector data_init) : Initialize array x by data_init. // - add(int begin, int end, Tp vadd) : Update x[i] <- x[i] + vadd for all begin <= i < end. // - get(int pos) : Get x[pos]. // - prod(int begin, int end) : Get min(x[begin], ..., x[end - 1]). template ::max() / 2> struct RangeAddRangeMin { int N, head; std::vector range_min, range_add; static inline Tp f(Tp x, Tp y) noexcept { return std::min(x, y); } inline void _merge(int pos) { range_min[pos] = f(range_min[pos * 2] + range_add[pos * 2], range_min[pos * 2 + 1] + range_add[pos * 2 + 1]); } void initialize(const std::vector &data_init) { N = data_init.size(), head = 1; while (head < N) head <<= 1; range_min.assign(head * 2, defaultT); range_add.assign(head * 2, 0); std::copy(data_init.begin(), data_init.end(), range_min.begin() + head); for (int pos = head; --pos;) _merge(pos); } RangeAddRangeMin() = default; RangeAddRangeMin(const std::vector &data_init) { initialize(data_init); } void _add(int begin, int end, int pos, int l, int r, Tp vadd) noexcept { if (r <= begin or end <= l) return; if (begin <= l and r <= end) { range_add[pos] += vadd; return; } _add(begin, end, pos * 2, l, (l + r) / 2, vadd); _add(begin, end, pos * 2 + 1, (l + r) / 2, r, vadd); _merge(pos); } // Add `vadd` to (x_begin, ..., x_{end - 1}) void add(int begin, int end, Tp vadd) noexcept { return _add(begin, end, 1, 0, head, vadd); } Tp _get(int begin, int end, int pos, int l, int r) const noexcept { if (r <= begin or end <= l) return defaultT; if (begin <= l and r <= end) return range_min[pos] + range_add[pos]; return f(_get(begin, end, pos * 2, l, (l + r) / 2), _get(begin, end, pos * 2 + 1, (l + r) / 2, r)) + range_add[pos]; } // Return f(x_begin, ..., x_{end - 1}) Tp get(int pos) const noexcept { return prod(pos, pos + 1); } Tp prod(int begin, int end) const noexcept { return _get(begin, end, 1, 0, head); } }; struct permutation_tree { enum NodeType { JoinAsc, JoinDesc, Cut, Leaf, None, }; struct node { NodeType tp; int L, R; // i in [L, R) int mini, maxi; // A[i] in [min, max] std::vector child; int sz() const { return maxi - mini + 1; } template friend OStream &operator<<(OStream &os, const node &n) { os << "[[" << n.L << ',' << n.R << ")(ch:"; for (auto i : n.child) os << i << ','; return os << ")(tp=" << n.tp << ")]"; } }; int root; std::vector A; std::vector nodes; bool is_adj(int lid, int rid) const { return nodes[lid].maxi + 1 == nodes[rid].mini; } void add_child(int parid, int chid) { nodes[parid].child.push_back(chid); nodes[parid].L = std::min(nodes[parid].L, nodes[chid].L); nodes[parid].R = std::max(nodes[parid].R, nodes[chid].R); nodes[parid].mini = std::min(nodes[parid].mini, nodes[chid].mini); nodes[parid].maxi = std::max(nodes[parid].maxi, nodes[chid].maxi); } NodeType findtype(int lid, int rid) const { const auto &l = nodes[lid].tp, &r = nodes[rid].tp; if (is_adj(lid, rid)) return NodeType::JoinAsc; if (is_adj(rid, lid)) return NodeType::JoinDesc; return NodeType::None; } permutation_tree(const std::vector &A_) : root(-1), A(A_) { // A: 0-origin RangeAddRangeMin segtree((std::vector(A.size()))); std::vector> hi{{A.size() + 1, -1}}, lo{{-1, -1}}; std::vector st; for (int i = 0; i < int(A.size()); ++i) { while (A[i] > hi.back().first) { segtree.add(hi[hi.size() - 2].second + 1, hi.back().second + 1, A[i] - hi.back().first); hi.pop_back(); } hi.emplace_back(A[i], i); while (A[i] < lo.back().first) { segtree.add(lo[lo.size() - 2].second + 1, lo.back().second + 1, lo.back().first - A[i]); lo.pop_back(); } lo.emplace_back(A[i], i); int h = nodes.size(); nodes.push_back({NodeType::Leaf, i, i + 1, A[i], A[i], std::vector{}}); while (true) { NodeType join_tp; if (st.size() and (join_tp = findtype(st.back(), h)) != NodeType::None) { const node &vtp = nodes[st.back()]; // Insert v as the child of the top node in the stack if (join_tp == vtp.tp) { // Append child to existing Join node add_child(st.back(), h); h = st.back(); st.pop_back(); } else { // Make new join node (with exactly two children) // TODO: Refactor here nodes.push_back({join_tp, std::min(vtp.L, nodes[h].L), std::max(vtp.R, nodes[h].R), std::min(vtp.mini, nodes[h].mini), std::max(vtp.maxi, nodes[h].maxi), {st.back(), h}}); h = nodes.size() - 1; st.pop_back(); } } else if (segtree.prod(0, i + 1 - nodes[h].sz()) == 0) { // Make Cut node // TODO: Refactor here int sz = nodes[h].sz(), L = nodes[h].L, R = nodes[h].R, maxi = nodes[h].maxi, mini = nodes[h].mini; int j; std::vector new_ch{h}; do { int j = st.back(); st.pop_back(); new_ch.push_back(j); node &n = nodes[j]; sz += n.sz(), L = n.L, maxi = std::max(maxi, n.maxi), mini = std::min(mini, n.mini); } while (maxi - mini + 1 != sz); std::reverse(new_ch.begin(), new_ch.end()); nodes.push_back({NodeType::Cut, L, R, mini, maxi, new_ch}); h = nodes.size() - 1; } else { break; } } st.push_back(h); segtree.add(0, i + 1, -1); } assert(st.size() == 1); root = st[0]; } void to_DOT(std::string filename = "") const { if (filename.empty()) filename = "permutation_tree_" + std::to_string(A.size()) + ".DOT"; std::ofstream ss(filename); ss << "digraph{\n"; int nleaf = 0; for (int i = 0; i < int(nodes.size()); i++) { ss << i << "[\n"; std::string lbl; if (nodes[i].tp == NodeType::Leaf) { lbl = "A[" + std::to_string(nleaf) + "] = " + std::to_string(A[nleaf]), nleaf++; } else { lbl += std::string(nodes[i].tp == NodeType::Cut ? "Cut" : "Join") + "\\n"; lbl += "[" + std::to_string(nodes[i].L) + ", " + std::to_string(nodes[i].R) + ")"; } ss << "label = \"" << lbl << "\",\n"; ss << "]\n"; for (const auto &ch : nodes[i].child) { ss << i << " -> " << ch << ";\n"; } } ss << "{rank = same;"; for (int i = 0; i < int(nodes.size()); i++) { if (nodes[i].tp == NodeType::Leaf) ss << ' ' << i << ';'; } ss << "}\n"; ss << "}\n"; ss.close(); return; } }; #include using mint = atcoder::modint998244353; // Utility functions for atcoder::static_modint template std::istream &operator>>(std::istream &is, atcoder::static_modint &x) { long long t; return is >> t, x = t, is; } template std::ostream &operator<<(std::ostream &os, const atcoder::static_modint &x) { return os << x.val(); } // atcoder::static_modint

, P: prime number template struct acl_fac { std::vector facs, facinvs; acl_fac(int N) { assert(-1 <= N and N < modint::mod()); facs.resize(N + 1, 1); for (int i = 1; i <= N; i++) facs[i] = facs[i - 1] * i; facinvs.assign(N + 1, facs.back().inv()); for (int i = N; i > 0; i--) facinvs[i - 1] = facinvs[i] * i; } modint ncr(int n, int r) const { if (n < 0 or r < 0 or n < r) return 0; return facs[n] * facinvs[r] * facinvs[n - r]; } modint operator[](int i) const { return facs[i]; } modint finv(int i) const { return facinvs[i]; } }; // acl_fac fac(1000000); int main() { int N, K; cin >> N >> K; vector P(N); cin >> P; for (auto &x : P) x--; permutation_tree tree(P); // tree.to_DOT(); vector dp(K + 1, vector(N + 1)); dp[0][0] = 1; auto rec = [&](auto &&self, int now) -> void { auto &v = tree.nodes[now]; if (v.tp == permutation_tree::NodeType::Cut or v.tp == permutation_tree::NodeType::Leaf) { REP(k, K) dp[k + 1][v.R] += dp[k][v.L]; } vector su(K); for (auto ch : v.child) { self(self, ch); if (v.tp == permutation_tree::NodeType::JoinAsc or v.tp == permutation_tree::JoinDesc) { IREP(k, K) { dp[k + 1][tree.nodes[ch].R] += su[k]; su[k] += dp[k][tree.nodes[ch].L]; } } } }; rec(rec, tree.root); dbg(dp); for (int i = 1; i <= K; i++) cout << dp[i][N].val() << '\n'; }