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

    [CTS2019]氪金手游 

    各种情况加在一起

    先考虑弱化版:外向树,wi确定

    i合法的概率就是wi/sw sw表示子树的w的和,和子树外情况无关

    这些概率乘起来就是最终合法的概率

    如果都是外向树,

    f[i][j]i为根子树,sw=j的所有wi出现方案下的合法概率和

    背包

    有反向边?

    直接处理满足很难,子树内外有先后顺序

    容斥!不满足+随意

    不满足只要转移的时候乘上-1

    随意就是断开这条边不考虑.

    所以f[i][j]定义是:i为根子树的连通块sw=j,所有情况的合法概率乘上(-1)^|S|的和

    注意统计答案,由于j是相连的size,从1~3*n都有意义

    // luogu-judger-enable-o2
    #include<bits/stdc++.h>
    #define reg register int
    #define il inline
    #define fi first
    #define se second
    #define mk(a,b) make_pair(a,b)
    #define numb (ch^'0')
    #define pb push_back
    #define solid const auto &
    #define enter cout<<endl
    #define pii pair<int,int>
    using namespace std;
    typedef long long ll;
    template<class T>il void rd(T &x){
        char ch;x=0;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
        for(x=numb;isdigit(ch=getchar());x=x*10+numb);(fl==true)&&(x=-x);}
    template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');}
    template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');}
    template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('
    ');}
    namespace Modulo{
    const int mod=998244353;
    il int ad(int x,int y){return x+y>=mod?x+y-mod:x+y;}
    il int sub(int x,int y){return ad(x,mod-y);}
    il int mul(int x,int y){return (ll)x*y%mod;}
    il void inc(int &x,int y){x=ad(x,y);}
    il void inc2(int &x,int y){x=mul(x,y);}
    il int qm(int x,int y=mod-2){int ret=1;while(y){if(y&1) ret=mul(x,ret);x=mul(x,x);y>>=1;}return ret;}
    template<class ...Args>il int ad(const int a,const int b,const Args &...args) {return ad(ad(a,b),args...);}
    template<class ...Args>il int mul(const int a,const int b,const Args &...args) {return mul(mul(a,b),args...);}
    }
    using namespace Modulo;
    namespace Miracle{
    const int N=1003;
    int n;
    int p[N][4];
    struct node{
        int nxt,to;
        int val;
    }e[2*N];
    int hd[N],cnt;
    void add(int x,int y,int c){
        e[++cnt].nxt=hd[x];
        e[cnt].to=y;e[cnt].val=c;
        hd[x]=cnt;
    }
    int f[N][3003];
    int g[3003];
    int sz[N];
    int ni[3*N];
    void dfs(int x,int fa){
        f[x][0]=1;
        sz[x]=0;
        for(reg i=hd[x];i;i=e[i].nxt){
            int y=e[i].to;
            if(y==fa) continue;
            dfs(y,x);
            if(e[i].val==1){
                for(reg j=3*sz[x];j>=0;--j){
                    for(reg k=3*sz[y];k>=1;--k){
                        inc(f[x][j+k],mul(f[x][j],f[y][k]));
                    }
                    f[x][j]=0;
                }
            }else{
                for(reg j=3*sz[x];j>=0;--j){
                    int tot=0;
                    int v=f[x][j];
                    for(reg k=3*sz[y];k>=1;--k){
                        inc(f[x][j+k],mul(mod-1,f[x][j],f[y][k]));
                        inc(tot,f[y][k]);
                    }
                    f[x][j]=0;
                    inc(f[x][j],mul(v,tot));
                }
            }
            sz[x]+=sz[y];
        }
        ++sz[x];
        memset(g,0,sizeof g);
        for(reg i=3*sz[x];i>=1;--i){
            for(reg j=1;j<=3&&i-j>=0;++j){
                inc(g[i],mul(p[x][j],j,ni[i],f[x][i-j]));
            }
        }
        memcpy(f[x],g,sizeof g);
    }
    int main(){
        rd(n);
        int a1,a2,a3;
        ni[1]=1;
        for(reg i=2;i<=3*n;++i) {
            ni[i]=mul(mod-mod/i,ni[mod%i]);
        }
        
        for(reg i=1;i<=n;++i){
            rd(a1);rd(a2);rd(a3);int tot=qm(ad(a1,a2,a3));
            p[i][1]=mul(a1,tot),p[i][2]=mul(a2,tot);
            p[i][3]=mul(a3,tot);
        }    
        int x,y;
        for(reg i=1;i<n;++i){
            rd(x);rd(y);
            add(x,y,1);add(y,x,-1);
        }
        dfs(1,0);
        int ans=0;
        for(reg j=1;j<=3*sz[1];++j){
            inc(ans,f[1][j]);
        }
        cout<<ans;
        return 0;
    }
    
    }
    signed main(){
        Miracle::main();
        return 0;
    }
    
    /*
       Author: *Miracle*
    */

    一般情况:$Pi wi/sw$

    反向边?容斥

    然后带着所有系数什么的一起DP

  • 相关阅读:
    js参数自定义
    分页插件--记录
    .net mvc接收参数为null的解决方案
    c#枚举转字典或表格
    openlayers添加弹出框
    openlayers按坐标点播放
    openlayers轨迹匀速播放
    MyEclipse配置进行Hibernate逆映射
    BIO,NIO,AIO
    Git遇到的一点错误
  • 原文地址:https://www.cnblogs.com/Miracevin/p/11149254.html
Copyright © 2011-2022 走看看