题解 P5405 【[CTS2019]氪金手游】
Enderwitherzjdl · · 题解
骗访问量啦QwQ My blog
题意
n种卡,卡
问:经过若干次抽卡并集齐所有卡后,第一次抽卡到每张卡的时间
考场上万万没想到容斥......QwQ
首先考虑一条链的情况
链上只有一条反向边时:
由容斥原理(update:感谢JSOI爆零珂学家yzhang指正):
将单向链推广到外向树,对于每个节点,由于是求第一次抽卡时间,各子树的抽卡时间只要满足子树内的先后关系,所以各子树之间概率是独立的,可以用dp转移。设
考虑加入反向边,用容斥计算:P(此边是反向边)=P(此边不存在)-P(此边是正向边)
并不想优化常数
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=1010;
const ll P=998244353;
int n,p[N][4],x,y,inv[N*3],f[N][N*3],g[N*3],ans,size[N];
int head[N],to[N<<1],nxt[N<<1],st[N<<1],q;
inline int read() {
int re=0; char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9') re=re*10+ch-48,ch=getchar();
return re;
}
inline void add(int x,int y) {
to[++q]=y; nxt[q]=head[x]; head[x]=q; st[q]=1;
to[++q]=x; nxt[q]=head[y]; head[y]=q; st[q]=P-1;
}
ll qpow(ll x,ll k) {ll r=1; for (; k; k>>=1,x=x*x%P) if (k&1) r=r*x%P; return r;}
void dfs(int x,int fa) {
f[x][0]=1; int y;
for (int i=head[x]; i; i=nxt[i]) if (to[i]!=fa) {
dfs(to[i],x); y=to[i];
memset(g,0,sizeof g);
for (int j=0; j<=size[x]; j++) for (int k=0; k<=size[y]; k++) {
g[j+k]=(g[j+k]+1ll*f[x][j]*f[y][k]%P*st[i]%P)%P;
if (st[i]!=1) g[j]=(g[j]+1ll*f[x][j]*f[y][k]%P)%P;
}
memcpy(f[x],g,sizeof g); size[x]+=size[y];
}
memset(g,0,sizeof g);
for (int j=0; j<=size[x]; j++) for (int k=1; k<=3; k++) {
g[j+k]=(g[j+k]+1ll*f[x][j]*k%P*inv[j+k]%P*p[x][k]%P)%P;
}
memcpy(f[x],g,sizeof g); size[x]+=3;
}
int main() {
n=read();
for (int i=1; i<=n; i++) {
x=0; for (int j=1; j<=3; j++) x+=(p[i][j]=read());
for (int j=1; j<=3; j++) p[i][j]=p[i][j]*qpow(x,P-2)%P;
}
for (int i=1; i<n; i++) x=read(),y=read(),add(x,y);
inv[1]=1; for (int i=2; i<=n*3; i++) inv[i]=1ll*(P-P/i)*inv[P%i]%P;
dfs(1,0);
for (int i=1; i<=3*n; i++) ans=(ans+f[1][i])%P;
printf("%d\n",ans);
return 0;
}