zoukankan      html  css  js  c++  java
  • [CTS 2019] 氪金手游

    题目

    传送门

    解法

    完了又学不走了,我就是全机房最弱的 QAQ

    容易发现题目给了一棵树。

    先考虑外向树的情况。设第 (i) 个点子树的 (W_i) 和为 (s_i)(sum_{i=1}^n W_i=S)

    由于除了 (i) 子树的点要比 (i) 晚抽到,其余的点都可以比 (i) 早抽到。所以 (i) 比子树中的所有卡牌都要早抽到的概率为:

    [frac{W_i}{S}sum_{j=0}^{infty}left(frac{S-s_i}{S} ight )^j ]

    相当于枚举 (i) 是第几个被抽到的(注意可能会有不满足条件的情况,比如 (i) 是第一张被抽到的牌,但它有入度,这种情况会被其它点的限制考虑)。

    发现后面的柿子正好是个无穷递降等比数列求和公式,故概率为

    [frac{W_i}{S} imes 1 imes frac{S}{s_i}=frac{W_i}{s_i} ]

    这个只与子树信息有关。我们考虑设计状态 (f_{i,j}) 表示处理完 (i) 这个子树,(s_i=j) 的满足条件的概率。转移直接背包就好了。

    然后考虑反向边:考虑容斥。朴素容斥原理就是求满足任一条件的元素个数,已知满足一种条件的元素个数。

    不过我们要求的是满足所有反向边条件的元素个数。有什么联系?只要我们将上文的 “满足” 换成 “不满足”(不满足也是一种条件),那么容斥求得的就是不满足任意条件的元素个数,再在全集上求一个补集就是满足所有条件的元素个数。

    所以答案就是至少 (0) 个条件不满足(即全集) (-) 至少一个条件不满足 (+) 至少两个条件不满足 (...)

    容易发现此时的容斥系数是 (-1) 的条件个数次方。那么每次就乘上个 (-1) 即可。

    我们知道不满足 (i) 个反向边是很好算的:枚举 (i) 条反向边反向,其余反向边删掉。这就转化成了上面外向树的问题。

    代码

    #include <cstdio>
    
    #define rep(i,_l,_r) for(register signed i=(_l),_end=(_r);i<=_end;++i)
    #define fep(i,_l,_r) for(register signed i=(_l),_end=(_r);i>=_end;--i)
    #define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
    #define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
    #define print(x,y) write(x),putchar(y)
    
    template <class T> inline T read(const T sample) {
        T x=0; int f=1; char s;
        while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
        while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
        return x*f;
    }
    template <class T> inline void write(const T x) {
        if(x<0) return (void) (putchar('-'),write(-x));
        if(x>9) write(x/10);
        putchar(x%10^48);
    }
    template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
    template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
    template <class T> inline T fab(const T x) {return x>0?x:-x;}
    template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
    template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
    template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}
    
    const int mod=998244353,maxn=1005;
    
    int t[maxn*3],ans,a[maxn],b[maxn],c[maxn],n,cnt,head[maxn],nxt[maxn<<1],to[maxn<<1],op[maxn<<1],inv[maxn*3],siz[maxn],deno[maxn],f[maxn][maxn*3];
    
    void inc(int &x,const int y) {
    	x=x+y;
    	if(x>=mod) x=x-mod;
    }
    
    void dec(int &x,const int y) {
    	x=x-y;
    	if(x<0) x=x+mod;
    }
    
    void addEdge(int u,int v) {
    	nxt[++cnt]=head[u],to[cnt]=v,op[cnt]=0,head[u]=cnt;
    	nxt[++cnt]=head[v],to[cnt]=u,op[cnt]=1,head[v]=cnt;
    }
    
    int Inv(int x,int y=mod-2) {
    	int r=1;
    	while(y) {
    		if(y&1) r=1ll*r*x%mod;
    		x=1ll*x*x%mod; y>>=1;
    	}
    	return r;
    }
    
    void init() {
    	inv[0]=inv[1]=1;
    	rep(i,2,n*3) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
    }
    
    void dfs(int u,int fa) {
    	siz[u]=1;
    	f[u][1]=1ll*a[u]*deno[u]%mod;
    	f[u][2]=1ll*b[u]*deno[u]%mod*2%mod;
    	f[u][3]=1ll*c[u]*deno[u]%mod*3%mod; // 乘上 W_i
    	erep(i,u) {
    		if(v==fa) continue;
    		dfs(v,u);
    		fep(j,siz[u]*3,1) fep(k,siz[v]*3,1) {
    			int tmp=1ll*f[u][j]*f[v][k]%mod;
    			if(op[i]) dec(t[j+k],tmp),inc(t[j],tmp);
    			// 反向和删掉
    			else inc(t[j+k],tmp);
    		}
    		siz[u]+=siz[v];
    		rep(j,1,siz[u]*3) f[u][j]=t[j],t[j]=0;
    	}
    	rep(i,1,siz[u]*3) f[u][i]=1ll*f[u][i]*inv[i]%mod; // 除掉 s_i
    }
    
    // 计算大奖概率:sum 某种 w 方案的概率 	imes 这种 w 方案得到大奖概率
    
    int main() {
    	int u,v;
    	n=read(9); init();
    	rep(i,1,n) a[i]=read(9),b[i]=read(9),c[i]=read(9),deno[i]=Inv(a[i]+b[i]+c[i]);
    	rep(i,1,n-1) 
    		u=read(9),v=read(9),
    		addEdge(u,v);
    	dfs(1,1);
    	rep(i,1,n*3) inc(ans,f[1][i]);
    	print(ans,'
    ');
    	return 0;
    }
    

    参考题解

    感觉我是直接嫖的。

    ( ext{ViXbob})

  • 相关阅读:
    【尺取】HDU Problem Killer
    【尺取或dp】codeforces C. An impassioned circulation of affection
    【搜索】codeforces C. The Tag Game
    【数学】codeforces B. The Golden Age
    【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring
    【贪心】codeforces D. Minimum number of steps
    【数学】codeforces A. Success Rate
    【二分+交互】codeforces B. Glad to see you!
    【组合 数学】codeforces C. Do you want a date?
    JavaScript实现快速排序
  • 原文地址:https://www.cnblogs.com/AWhiteWall/p/14433320.html
Copyright © 2011-2022 走看看