题解:P12796 [NERC 2022] Game of Questions

· · 题解

观察:如果再把一个题出现多次,没有影响。然后我们设 f_S 表示剩余人集合为 S 时获胜概率,把所有会影响集合大小的 S_i,拿出来直接转移,容易做到 O(4^m)。因为转移的限制只有交集非空。

先设 f_S 表示剩余人集合为 S 时获胜概率。转移考虑枚举子集 T,表示经过一个问题后集合 S \to T,记达成这个问题方案数为 g_{S,T},得到转移:

f_S = \frac{1}{n} \times \left( g_{S,\emptyset} \cdot f_S + g_{S,S} \cdot f_S + \sum_{T \subset S, T \neq \emptyset} g_{S,T} \cdot f_T \right)

发现后效性了,把这个转移解成递推式:

f_S = \frac{\sum_{T \subset S, T \neq \emptyset} g_{S,T} \cdot f_T}{n - g_{S,\emptyset} - g_{S,S}}

如果我们求出了 g,就能快速算出 f

然后按秩转移,每次枚举 S 补集的 \operatorname{lowbit},g_{S,T} = g_{S \cup x,T}+g_{S \cup x,T \cup x} 做到了 O(3^m)。

#include<bits/stdc++.h>
using namespace std;
const int N=19,mod=998244353,INF=2e9;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
#define qmi(a,b) a=min(a,b)
#define qma(a,b) a=max(a,b)
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define atrep(i,l,r) for(int i=(r);i>=(l);i--)
#define vec vector<int>
#define pb push_back
typedef double db;
db f[1<<N];
int tmp[N];
int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<pii> >g((1<<m),vector<pii>());
    atrep(S,0,(1<<m)-1){
        int len=1<<__builtin_popcount(S);
        g[S].resize(len);
        for(int T=S;;T=(T-1)&S){
            g[S][--len].first=T; 
            if(T==0)
                break;
        }
    }
    rep(i,1,n){
        string str;
        cin>>str;
        int S=0;
        rep(j,0,m-1){
            if(str[j]=='1'){
                S|=(1<<j);
            }
        }
        g[(1<<m)-1][S].second++;
    }
    #define lowbit(x)   ((x)&(-x))
    atrep(S,0,(1<<m)-2){
        int k=lowbit((((1<<m)-1)^S));
        int T=S|k;
        for(auto x:g[S])
            tmp[x.first]=0;
        for(auto x:g[T])
            tmp[S&x.first]+=x.second;
        for(auto &x:g[S])
            x.second=tmp[x.first];
    }
    rep(S,0,(1<<m)-1){
        if(S%2==0)
            continue;
        int cnt=n-g[S][0].second-g[S].back().second;
        if(cnt==0){
            f[S]=1.0;
        }else{
           for(auto it:g[S]){
                int T=it.first;
                if(T!=0&&T!=S)
                    f[S]+=f[T]*it.second;
            }
            f[S]/=cnt;
        }
    }
    printf("%.10lf\n",f[(1<<m)-1]);
}