The world cup is in full swing! Dudu comes to a bar.
There are N couples meeting to watch the world cup. There are 2 x N rows in front of the screen.
Everyone will sit in a random position.
Of course, if a couple is sitting right next to each other, they will have a lot to say, which will affect the watching of the world cup.
Generally, for a seating plan, if there are K couples sitting next to each other, the noise value of DK will be generated.
Duduxiong wants to know the expected noise value of the random seating plan.
To avoid outputting real numbers, set the answer to ans, and output the value of ANS × (2N)! mod P. Where P=998244353
Idea: dp[i][j] refers to the number of solutions where I couples have been seated and j couples have been seated together.
for(int i=1;i<=N;i++) for(int j=0;j<=i;j++) { ///The first couple is not separated dp[i][j]+=dp[i-1][j]*j;///Sitting in the middle of the couple sitting next to each other dp[i][j]+=dp[i-1][j-1]*(2*(i-1)-(j-1)+1);///Think of the previous (j-1) couple as a group, and put them in the gap to sit ///Couple i separated dp[i][j]+=dp[i-1][j+2]*((j+2)*(j+1)/2);///Two separate a couple dp[i][j]+=dp[i-1][j+1]*(2*(i-1)-(j+1)+1)*(j+1);///One of them broke up a couple dp[i][j]+=dp[i-1][j]*((2*(i-1)-j+1)*(2*(i-1)-j)/2);///Neither of them did anything to the other couple }
Full code:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod=998244353; const int N=1000; ll dp[N+5][N+5],p[N+5]; void init() { p[0]=1ll; for(int i=1;i<=N;i++) p[i]=p[i-1]*2ll%mod;///There is also a seat sequence between lovers, preprocessing out 2^n dp[0][0]=1; for(ll i=1;i<=N;i++) for(ll j=0;j<=i;j++) { ///The first couple is not separated dp[i][j]+=dp[i-1][j]*j%mod; if(dp[i][j]>mod) dp[i][j]-=mod; if(j>0) { dp[i][j]+=dp[i-1][j-1]*(2*(i-1)-(j-1)+1)%mod; if(dp[i][j]>mod) dp[i][j]-=mod; } ///Couple i separated dp[i][j]+=dp[i-1][j+2]*((j+2)*(j+1)/2)%mod; if(dp[i][j]>mod) dp[i][j]-=mod; dp[i][j]+=dp[i-1][j+1]*(2*(i-1)-(j+1)+1)*(j+1)%mod; if(dp[i][j]>mod) dp[i][j]-=mod; dp[i][j]+=dp[i-1][j]*((2*(i-1)-j+1)*(2*(i-1)-j)/2)%mod; if(dp[i][j]>mod) dp[i][j]-=mod; } } int n; ll d; int main() { init(); while(~scanf("%d%lld",&n,&d)) { ll ans=0,tmp=1ll; for(int i=0;i<=n;i++) ///O(n) accumulation result { ans+=dp[n][i]*tmp%mod; if(ans>mod) ans-=mod; tmp=tmp*d%mod; } ans=ans*p[n]%mod; printf("%lld\n",ans); } return 0; }