题解:P12796 [NERC 2022] Game of Questions
观察:如果再把一个题出现多次,没有影响。然后我们设
先设
发现后效性了,把这个转移解成递推式:
如果我们求出了
然后按秩转移,每次枚举
#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]);
}