General idea of the topic
There is a permutation with length N, \ (p=(p_1,p_2,p_3,p_4...,p_n) \), and then follow the following rules for the score \ (S(p) \) of permutation \ (P \):
There are \ (n \) individuals, the number of the \ (I \) individual is \ (I \), and then the \ (I \) individual has a ball, the number of the ball is \ (I \). Each operation, the \ (I \) individual will give the ball in his hand to the \ (p_i \) individual, and then ask you how many operations at least, everyone's number is the same as the number of the ball in his hand
Make \ (S_n \) a set of all possible \ (p \), and output \ (\ sum {p ∈ S_n} S(p)^{k}) \,\, mod \,\,998244353 \)
General idea
Set \ (dp_{i,j} \) to indicate that \ (I \) individuals are selected, and the minimum number of operands is \ (j \), and the number of schemes restored after the operation
Then, for the transfer, if we set the next ring, select \ (x \) individuals to transfer from \ (DP {I, J} \), then the next minimum operand is \ (lcm(j,x) \), the minimum common multiple, and the transfer state is \ (DP {I, lcm(j,x)} \)
But if you choose directly, there is repetition,
For example, we now have \ (4 \) numbers, and then \ (2 \) rings. Each ring is \ (2 \) individuals
If you directly select \ (C {4} ^ {2} * C {2} ^ {2} \)
But if you enumerate all the schemes, you will find that there are duplicates. For example, there will be
\(\ {1, 2 \} \), \ (\ {3, 4 \} \) and \ (\ {3, 4 \} \), \ (\ {1, 2 \} \) after the first one is selected, the rest is fixed
If it is difficult to avoid repetition, we can record the total number of rings, set it to \ (cnt \), and finally divide by \ (cnt! \)
But in this case, there is still repetition, that is, when we transfer
If \ (n \) individuals are selected now and \ (I \) individuals are selected next, it is \ (C {N-N} ^{I} * (i-1)! \)
So why \ ((i-1)! \) instead of $I$
For a ring, after sorting, the smallest point determines the size of its dictionary order, that is,
Only when the smallest point is selected first and confirmed, can the latter be selected casually, which corresponds to the above
Ring de duplication is actually sorted according to different essence and then de duplicated
Then look directly at the code
#pragma GCC optimize(2) #include <map> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 60, mod = 998244353; int f[N], fac[N], inv[N], n, k; map<int, int> dp[N][N]; int qpow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = 1ll * ans * a % mod; a = 1ll * a * a % mod; b >>= 1; } return ans; } int lcm(int a, int b) { if (!a || !b) return a + b; return a / __gcd(a, b) * b; } int C(int n, int m) { return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod; } // N how many are currently selected // cnt how many rings int qry(int N, int cnt, int Lcm) { //It's the last one if (N == n) return 1ll * qpow(Lcm, k) * inv[cnt] % mod; if (dp[N][cnt].find(Lcm) != dp[N][cnt].end()) return dp[N][cnt][Lcm]; int &t = dp[N][cnt][Lcm]; // for (int i = 1; N + i <= n; i++) // Choose i from the rest // Fix one first and determine the order t = (t + 1ll * C(n - N, i) * f[i] % mod * qry(N + i, cnt + 1, lcm(Lcm, i)) % mod) % mod; return t; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); cin >> n >> k; fac[0] = fac[1] = inv[0] = inv[1] = f[1] = 1; for (int i = 2; i <= n; i++) { f[i] = 1ll * f[i - 1] * (i - 1) % mod;//Factorial i-1 fac[i] = 1ll * fac[i - 1] * i % mod;//Factorial i inv[i] = 1ll * inv[i - 1] * qpow(i, mod - 2) % mod; } printf("%d\n", qry(0, 0, 0)); return 0; }