# 【LOJ2538】Slay the Spire（PKUWC2018）-DP+组合数

$ans=\sum _{i=0}^{k-1}G\left(i,i\right)\cdot F\left(m-i,k-i\right)+\sum _{i=k}^{m-1}G\left(i,k-1\right)\cdot F\left(m-i,1\right)$

$g\left(i,j\right)={w}_{i}\cdot \sum _{p=1}^{i-1}g\left(p,j-1\right)$

$G\left(x,y\right)=\sum _{i=1}^{n}g\left(i,y\right)\cdot {C}_{n-i}^{x-y}$

$f\left(i,j\right)={w}_{i}\cdot {C}_{i-1}^{j-1}+\sum _{p=1}^{i-1}f\left(p,j-1\right)$

$F\left(x,y\right)=\sum _{i=1}^{n}f\left(i,y\right)\cdot {C}_{n-i}^{x-y}$

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
int T,n,m,k,last=0;
ll wg[1510],wf[1510],C[1510][1510]={0};
ll g[1510][1510],f[1510][1510],sum[1510][1510],sumf[1510][1510];

bool cmp(ll a,ll b)
{
return a>b;
}

void calc_C(int n)
{
if (n<=last) return;
for(int i=last+1;i<=n;i++)
{
C[i][0]=1;
for(int j=1;j<=i;j++)
C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
}
last=n;
}

ll G(int x,int y)
{
if (x>n||x<y) return 0;
if (y==0) return C[n][x];
ll ans=0;
for(int i=1;i<=n;i++)
ans=(ans+g[i][y]*C[n-i][x-y])%mod;
return ans;
}

ll F(int x,int y)
{
if (x>n||x<y) return 0;
ll ans=0;
for(int i=1;i<=n;i++)
ans=(ans+f[i][y]*C[n-i][x-y])%mod;
return ans;
}

int main()
{
scanf("%d",&T);
C[0][0]=1;
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
calc_C(n);
for(int i=1;i<=n;i++)
scanf("%lld",&wg[i]);
for(int i=1;i<=n;i++)
scanf("%lld",&wf[i]);

sort(wg+1,wg+n+1,cmp);
sort(wf+1,wf+n+1,cmp);
for(int i=0;i<=n+1;i++)
g[0][i]=sum[0][i]=0;
g[0][0]=sum[0][0]=1;
for(int i=1;i<=n;i++)
{
g[i][0]=sum[i][0]=1;
for(int j=1;j<=i;j++)
{
if (j>k) break;
g[i][j]=wg[i]*sum[i-1][j-1]%mod;
sum[i][j]=(sum[i-1][j]+g[i][j])%mod;
}
sum[i][i+1]=0;
}

for(int i=0;i<=n+1;i++)
f[0][i]=sumf[0][i]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
if (j>k) break;
f[i][j]=(sumf[i-1][j-1]+wf[i]*C[i-1][j-1])%mod;
sumf[i][j]=(sumf[i-1][j]+f[i][j])%mod;
}
sumf[i][i+1]=0;
}

ll ans=0;
for(int i=0;i<=k-1;i++)
ans=(ans+G(i,i)*F(m-i,k-i))%mod;
for(int i=k;i<=m-1;i++)
ans=(ans+G(i,k-1)*F(m-i,1))%mod;
printf("%lld\n",ans);
}

return 0;
}
posted @ 2018-06-24 10:39  Maxwei_wzj  阅读(101)  评论(0编辑  收藏  举报