Getting closer and closer to a mathematician, Serval becomes a university student on math major in Japari University. On the Calculus class, his teacher taught him how to calculate the expected length of a random subsegment of a given segment. Then he left a bonus problem as homework, with the award of a garage kit from IOI. The bonus is to extend this problem to the general case as follows.
You are given a segment with length l. We randomly choose n segments by choosing two points (maybe with non-integer coordinates) from the given segment equiprobably and the interval between the two points forms a segment. You are given the number of random segments n, and another integer k. The 2n endpoints of the chosen segments split the segment into (2n+1) intervals. Your task is to calculate the expected total length of those intervals that are covered by at least k segments of the n random segments.
You should find the answer modulo 998244353.
Input
First line contains three space-separated positive integers n, k and l (1≤k≤n≤2000, 1≤l≤109).
Output
Output one integer — the expected total length of all the intervals covered by at least k segments of the n random segments modulo 998244353.
Formally, let M=998244353. It can be shown that the answer can be expressed as an irreducible fraction pq, where p and q are integers and q≢0(modM). Output the integer equal to p⋅q−1modM. In other words, output such an integer x that 0≤x<M and x⋅q≡p(modM).
很棒棒的一道题,又伤了我这个菜鸡的脑筋。题意很简洁,求一个长度为L线段上,随机放置n条子线段,求覆盖数>=k的部分的总长度的期望。
估计各种各样的解法会很多。我简单解释一下官方的题解。
首先,不妨令线段长度L为1(否则最后乘以L就行了)。然后随机问题可以转化成在线段上随机投掷一个测点P,投掷入覆盖数>=k的部分的概率。我们不妨想,随机往L上投
2
∗
n
+
1
2*n+1
2∗n+1个点,然后把其中的
2
∗
n
2*n
2∗n个点做一个配对,这个配对点之间就是子线段,剩下的一个点当做我们最后随机测点P。我们可以统计出所有符合要求的配对总数。符合要求的配对总数占所有配对的比,也就是测点投掷的概率。(这个想法也太tmd神仙了,瞬间把一个连续性问题转化为了离散型问题,仔细体会!)
合法配对总数可以用dp来求。首先,假设把所有
2
∗
n
+
1
2*n+1
2∗n+1个点从小到大排,设置dp状态
d
p
[
i
]
[
j
]
[
k
]
dp[i][j][k]
dp[i][j][k],i表示从小到大考虑到第i个点,j表示前i个点中,作为线段起点,而且尚未被匹配的个数,k=1表示测点P已经出现过时,符合条件的合法配对总数,k=0表示测点P尚未出现时的所有配对总数。最后我们的答案为
L
∗
(
d
p
[
2
∗
n
+
1
]
[
0
]
[
1
]
∗
n
!
∗
2
n
(
2
∗
n
+
1
)
!
)
L*(\frac{dp[2*n+1][0][1]*n!*2^n}{(2*n+1)!})
L∗((2∗n+1)!dp[2∗n+1][0][1]∗n!∗2n)。乘上的
n
!
∗
2
n
n!*2^n
n!∗2n可以这么解释,对所有子线段端点分别标号1,2,3,4……,其中1和2匹配,3和4匹配……,然后对上述
2
∗
n
+
1
2*n+1
2∗n+1个点中的
n
n
n个左端点从小到大的线段,分别给它们分配1(或者2),3(或者4),5(或者6),7(或者8)等端点号,那么有
n
!
∗
2
n
n!*2^n
n!∗2n种组合。
dp方程非常简单,仅仅给出代码。
#include<cstdio>
#define mo 998244353
using namespace std;
using LL=long long;
int dp[4005][2005][2],n,k,l;
int quick_power(int a, int b)
{
int res=1,base=a;
while(b)
{
if(b&1)
res=(LL)res*base%mo;
base=(LL)base*base%mo;
b>>=1;
}
return res;
}
int fac(int x)
{
int res=1;
for(int i=1;i<=x;i++)
res=(LL)res*i%mo;
return res;
}
int main()
{
scanf("%d%d%d",&n,&k,&l);
dp[1][1][0]=1;
for(int i=2;i<=2*n+1;i++)
for(int j=0;j<=i&&j<=n;j++)
{
dp[i][j][1]=(dp[i-1][j-1][1]+(j>=k?dp[i-1][j][0]:0)+(LL)dp[i-1][j+1][1]*(j+1))%mo;
dp[i][j][0]=(dp[i-1][j-1][0]+(LL)dp[i-1][j+1][0]*(j+1))%mo;
}
printf("%lld",(LL)l*dp[2*n+1][0][1]%mo*fac(n)%mo*quick_power(2,n)%mo*quick_power(fac(2*n+1),mo-2)%mo);
return 0;
}
这种思维方式一定要牢牢记住。
另外在评论区里看到了另一种解法,用FFT来求一个积分,暂时不太清楚是怎么一回事,先截图,以后慢慢研究。