大意: $n$对情侣, $2n$个座位, 对于一个方案, 若$k$对情侣相邻, 则喧闹值增加$D^k$, 求喧闹值期望.
跟CF 840C一样, 设$dp[i][j]$为$i$个人, 有$j$对情侣相邻, 枚举每个人转移即可.
#include <iostream>
#include <cstdio>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
typedef long long ll;
const int N = 2e3+10, P = 998244353;
int n, d, dp[N][N], fac[N][N];
int main() {
dp[0][0] = 1;
REP(i,0,2000) REP(j,0,i/2) if (dp[i][j]) {
int &r = dp[i][j];
if (i&1) {
dp[i+1][j] = (dp[i+1][j]+(ll)(i+1-j-2)*r)%P;
dp[i+1][j+1] = (dp[i+1][j+1]+2ll*r)%P;
if (j) dp[i+1][j-1] = (dp[i+1][j-1]+(ll)j*r)%P;
}
else {
dp[i+1][j] = (dp[i+1][j]+(ll)(i+1-j)*r)%P;
if (j) dp[i+1][j-1] = (dp[i+1][j-1]+(ll)j*r)%P;
}
}
REP(i,1,1000) {
fac[i][0] = 1;
REP(j,1,1000) fac[i][j] = (ll)fac[i][j-1]*i%P;
}
while (~scanf("%d%d", &n, &d)) {
int ans = 0;
REP(i,0,n) {
ans = (ans+(ll)dp[2*n][i]*fac[d][i])%P;
}
printf("%d\n", ans);
}
}