CF1773G Game of Questions
随着游戏进行,留在场上的人会越来越少,这提供了自然的 DP 转移方向:枚举子集。
设
若
注意
设
易知
综上,时空复杂度均为
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1 << 17;
int n, m, c[N], p[17], lab[N], K[N];
double f[N], ans;
vector<int> s[N];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef ALEX_WEI
FILE* IN = freopen("1.in", "r", stdin);
FILE* OUT = freopen("1.out", "w", stdout);
#endif
cin >> n >> m;
for(int i = 1; i <= n; i++) {
int S = 0;
for(int j = 0; j < m; j++) {
char s;
cin >> s;
if(s == '1') S |= 1 << j;
}
c[S]++;
}
f[(1 << m) - 1] = 1;
for(int i = (1 << m) - 1; i; i--) {
if(i & 1 ^ 1) continue;
int ppc = 0;
for(int j = 0; j < m; j++) if(i >> j & 1) p[j] = ppc++;
s[i].resize(1 << ppc), lab[0] = 0;
for(int _ = (i - 1) & i; ; _ = (_ - 1) & i) {
int S = i ^ _, low = 31 - __builtin_clz(S & -S);
lab[S] = lab[S ^ (1 << low)] | (1 << p[low]);
K[lab[S]] = S;
if(!_) break;
}
int low = 31 - __builtin_clz((~i) & -(~i));
int _i = i ^ (1 << low);
for(int j = 0; j < 1 << ppc; j++) {
if(i + 1 == (1 << m)) {
s[i][j] = c[j];
continue;
}
int T = (j & ((1 << low) - 1)) | ((j >> low) << low + 1);
s[i][j] = s[_i][T] + s[_i][T | (1 << low)];
}
int sum = 0;
for(int j = 1; j + 1 < 1 << ppc; j++) sum += s[i][j];
if(!sum) ans += f[i];
else {
double pro = f[i] / sum;
for(int j = 1; j + 1 < 1 << ppc; j++) f[K[j]] += s[i][j] * pro;
}
}
printf("%.12lf\n", ans);
return 0;
}
/*
g++ G.cpp -o G -std=c++14 -O2 -DALEX_WEI
*/