MONOORBEZ - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: nikhil_010309
Tester: raysh07
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

DP, combinatorics

PROBLEM:

For an array X with elements in [1, M] and a permutation P of [1, M], define P(X) to be the array [P_{X_1}, P_{X_2}, \ldots, P_{X_N}].
Define P^k(X) to be this iterated k times.

Given the array A = [M, M, \ldots, M], and values K, D; count pairs (X, P) such that:

  • X is an array of length N with values in [1, M]
  • X \le A lexicographically
  • P’s graph consists of K connected components.
  • X \lt P(X) \lt P^2(X) \lt\ldots\lt P^D(X) lexicographically.

EXPLANATION:

There are many conditions here, so let’s break them down one at a time.

X is an array of length N with values in [1, M].
Then we want X \le A lexicographically. But, since A = [M, \ldots, M] here, that follows automatically. Thus we can ignore that condition in this version of the problem.

Next, the graph of (i, P_i) edges should consist of K connected components.
This graph looks like a collection of disjoint cycles, so really we want K cycles to form the permutation.

Finally, the X \lt P(X) \lt P^2(X) \lt\ldots\lt P^D(X) condition.
To understand this, let’s look at the first index where X and P(X) differ.
This is the smallest index r such that P_{X_r} \ne X_r.
For X \lt P(X) to hold, we need X_r \lt P_{X_r}.

Next, we look at P(X) and P^2(X).
Here, it can be observed that the very same index r will be the first one where P(X) and P^(X) differ; because everything before r is fixed by P anyway while this index cannot be fixed by P (since P is a permutation and not an arbitrary function.)

Extending this logic, the smallest index r such that P_{X_r} \ne X_r determines whether the entire chain X \lt P(X) \lt P^2(X) \lt\ldots\lt P^D(X) is possible or not.

Note, in particular, that this chain simply represents X_r moving along its cycle in P, so we really just need the cycle containing X_r to have D steps in ascending order.


Let’s now use this observation to count things.

Suppose we fix the index r to be the first one not fixed by P.
We’ll count pairs (P, D) with this fixed index.

First, let’s deal with the prefix before r.
Every element there must be fixed by P.
Suppose there are k distinct elements among them.
Then,

  • There are \binom{M}{k} ways to choose this subset of k elements.
  • There are S_2(r-1, k) ways to distribute these k distinct elements among the first r-1 positions, ensuring that every element appears at least once; after fixing an order of elements.
  • Different orderings of the k elements correspond to different labelings; and hence there’s an extra multiplier of k! to fix the order first.

The Stirling numbers can be precomputed fairly easily in quadratic time using DP.

Once that’s done, we have the elements at indices \ge r of X, as well as the other elements of the permutation P to deal with (it has only k fixed points so far.)
For that, one easy step is that elements of X at indices beyond r don’t matter at all as we saw, and so there are M options for each one giving a total multiplier of M^{N-r} for that part.

Now, let’s consider the element X_r itself.

It must increase D times along its cycle.
So, let us choose D+1 distinct elements to be the initial value plus the increments.
This can be done in \binom{M-k}{D+1} ways.
X_r is fixed to be the smallest of them.

Note now that the entire array X is determined; we just need to complete the permutation P.

That can be done as follows.
We currently have k singletons, as well as a chain consisting of X_r and the elements it must pass through.

Let’s treat this chain as a single vertex.
Then, we have M - k - D “free” elements, and we can form any permutation with them; as long as the total number of cycles is equal to exactly K - k.

The number of ways of doing this is given by S_1(M-k-D, K-k) where S_1 denotes the Stirling numbers of the first kind.
Just like the second kind, these can be precomputed and stored using a fairly simple recurrence.

Thus, after some precomputation, it’s possible to compute the answer for a fixed (r, k) pair in constant time.
There are \mathcal{O}(NM) choices of this pair, try all of them and add up the counts.

TIME COMPLEXITY:

\mathcal{O}(NM) per testcase.

CODE:

Tester's code (C++)
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int mod = 998244353;

struct mint{
    int x;

    mint (){ x = 0;}
    mint (int32_t xx){ x = xx % mod; if (x < 0) x += mod;}
    mint (long long xx){ x = xx % mod; if (x < 0) x += mod;}

    int val(){
        return x;
    }
    mint &operator++(){
        x++;
        if (x == mod) x = 0;
        return *this;
    }
    mint &operator--(){
        if (x == 0) x = mod;
        x--;
        return *this;
    }
    mint operator++(int32_t){
        mint result = *this;
        ++*this;
        return result;
    }
    
    mint operator--(int32_t){
        mint result = *this;
        --*this;
        return result;
    }
    mint& operator+=(const mint &b){
        x += b.x;
        if (x >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint &b){
        x -= b.x;
        if (x < 0) x += mod;
        return *this;
    }
    mint& operator*=(const mint &b){
        long long z = x;
        z *= b.x;
        z %= mod;
        x = (int)z;
        return *this;
    }
    mint operator+() const {
        return *this;
    }
    mint operator-() const {
        return mint() - *this;
    }
    mint operator/=(const mint &b){
        return *this = *this * b.inv();
    }
    mint power(long long n) const {
        mint ok = *this, r = 1;
        while (n){
            if (n & 1){
                r *= ok;
            }
            ok *= ok;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        return power(mod - 2);
    }
    friend mint operator+(const mint& a, const mint& b){ return mint(a) += b;}
    friend mint operator-(const mint& a, const mint& b){ return mint(a) -= b;}
    friend mint operator*(const mint& a, const mint& b){ return mint(a) *= b;}
    friend mint operator/(const mint& a, const mint& b){ return mint(a) /= b;}
    friend bool operator==(const mint& a, const mint& b){ return a.x == b.x;}
    friend bool operator!=(const mint& a, const mint& b){ return a.x != b.x;}
    mint power(mint a, long long n){
        return a.power(n);
    }
    friend ostream &operator<<(ostream &os, const mint &m) {
        os << m.x;
        return os;
    }
    explicit operator bool() const {
        return x != 0;
    }
};

struct factorials{
    int n;
    vector <mint> ff, iff;
    
    factorials(int nn){
        n = nn;
        ff.resize(n + 1);
        iff.resize(n + 1);
        
        ff[0] = 1;
        for (int i = 1; i <= n; i++){
            ff[i] = ff[i - 1] * i;
        }
        
        iff[n] = ff[n].inv();
        for (int i = n - 1; i >= 0; i--){
            iff[i] = iff[i + 1] * (i + 1);
        }
    }
    
    mint C(int n, int r){
        if (n == r) return mint(1);
        if (n < 0 || r < 0 || r > n) return mint(0);
        return ff[n] * iff[r] * iff[n - r];
    }
    
    mint P(int n, int r){
        if (n < 0 || r < 0 || r > n) return mint(0);
        return ff[n] * iff[n - r];
    }
    
    mint solutions(int n, int r){
        // Solutions to x1 + x2 + ... + xn = r, xi >= 0 
        return C(n + r - 1, n - 1);
    }
    
    mint catalan(int n){
        return ff[2 * n] * iff[n] * iff[n + 1];
    }
};

const int PRECOMP = 3e6 + 69;
factorials F(PRECOMP);

// REMEMBER To check MOD and PRECOMP

void Solve() 
{
    int n, m, k, d; cin >> n >> m >> k >> d;
    
    vector <int> a(n);
    for (auto &x : a) cin >> x;
    
    vector<vector<mint>> dp(m + 1, vector<mint>(m + 1, 0));
    dp[0][0] = 1;
    for (int i = 2; i <= m; i++){
        for (int j = 1; j <= i / 2; j++){
            dp[i][j] = (i - 1) * (dp[i - 1][j] + dp[i - 2][j - 1]);
        }
    }
    
    mint ans = 0;
    for (int fixed = 0; fixed <= k; fixed++){
        int left = m - fixed;
        // d + 1 converge to 1 
        if (left < d + 1) continue;
        left -= d;
        
        mint here = dp[left][k - fixed];
        if (k - fixed - 1 >= 0) here += dp[left - 1][k - fixed - 1];
        
        here *= F.C(m, fixed) * F.C(m - fixed, d + 1);
        mint add = 0;
        
        for (int i = 1; i <= n; i++){
            mint c1 = fixed;
            mint c2 = m;
            add += (c1.power(i - 1)) * (c2.power(n - i));
        }
        here *= add;
        
        ans += here;
    }
    
    cout << ans << "\n";
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // freopen("in",  "r", stdin);
    // freopen("out", "w", stdout);
    
    cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}