其他分享
首页 > 其他分享> > 题解 客星璀璨之夜

题解 客星璀璨之夜

作者:互联网

传送门

并不知道小球进洞模型是什么,并且不建议在机房百度它

发现有很多重复的转移,打表找个规律可以优化掉一维,复杂度 \(O(n^2)\)

Code:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 3010
#define ll long long 
//#define int long long 

char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n;
ll x[N<<1], dp[N][N<<1], inv[N<<1];
const ll p=998244353;

signed main()
{
	n=read();
	inv[0]=inv[1]=1;
	for (int i=2; i<=n*2+1; ++i) inv[i]=(p-p/i)*inv[p%i]%p;
	for (int i=1; i<=n*2+1; ++i) x[i]=read();
	dp[1][1]=dp[1][2]=inv[2];
	for (int i=2; i<=n; ++i) {
		for (int j=1; j<=2*i; ++j) {
			dp[i][j] = (dp[i][j]+(j-1)*dp[i-1][j-2]%p*inv[i*2]%p)%p;
			//if (i==10 && j==2) printf("dp[%d][%d]+=%d*dp[%d][%d]*inv[i*2]\n", i, j, (j-1), i-1, j-2);
			dp[i][j] = (dp[i][j]+dp[i-1][j-1]*inv[i*2]+inv[i*2])%p;
			dp[i][j] = (dp[i][j]+(2*i-j)*dp[i-1][j]%p*inv[i*2]%p)%p;
			//if (i==10 && j==2) printf("dp[%d][%d]+=%d*dp[%d][%d]*inv[i*2]\n", i, j, (2*i-j), i-1, j);
		}
		#if 0
		for (int j=1; j<=i; ++j) {
			dp[i][j*2]=(dp[i][j*2]+inv[i*2])%p;
			dp[i][j*2-1]=(dp[i][j*2-1]+inv[i*2])%p;
			//if (i==10) printf("dp[%d][%d]+=inv[2]\n", i, j*2);
			//if (i==10) printf("dp[%d][%d]+=inv[2]\n", i, j*2-1);
			int pos1=0, pos2=0;
			for (int k=1; k<=2*i; ++k) {
				if (k<=2*j-2 || k>=2*j+1) ++pos1;
				dp[i][k] = (dp[i][k]+dp[i-1][pos1]*inv[i*2]%p)%p;
				if (i==10 && k==2) printf("dp[%d][%d]+=dp[%d][%d]*inv[i*2]\n", i, k, i-1, pos1);
				if (k<=2*j-1 || k>=2*j+2) ++pos2;
				dp[i][k] = (dp[i][k]+dp[i-1][pos2]*inv[i*2]%p)%p;
				if (i==10 && k==2) printf("dp[%d][%d]+=dp[%d][%d]*inv[i*2]\n", i, k, i-1, pos2);
			}
		}
		#endif
		//for (int j=1; j<=i*2; ++j) printf("cnt[%d][%d]=%lld\n", i-1, j, cnt[i][j]);
	}
	ll ans=0;
	for (int i=1; i<=2*n; ++i) ans=(ans+dp[n][i]*((x[i+1]-x[i])%p)%p)%p;
	printf("%lld\n", ans);
	
	return 0;
}

标签:题解,璀璨,long,pos1,客星,pos2,inv,dp,define
来源: https://www.cnblogs.com/narration/p/15110187.html