跳到正文

题解

题解:P12796 [NERC 2022] Game of Questions

q1uple约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年9月10日
收录
2026年9月10日
更新
2026年11月11日
篇幅
约 4 分钟
获赞
3
题目
P12796

版本

  1. v2当前
  2. v1

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

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

fS=1n×(gS,∅⋅fS+gS,S⋅fS+∑T⊂S,T≠∅gS,T⋅fT)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)

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

fS=∑T⊂S,T≠∅gS,T⋅fTn−gS,∅−gS,Sf_S = \frac{\sum_{T \subset S, T \neq \emptyset} g_{S,T} \cdot f_T}{n - g_{S,\emptyset} - g_{S,S}}

如果我们求出了 gg,就能快速算出 ff

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

C++
#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]);
}
原文