#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double D;
typedef vector<int> VI;
typedef set<int> SI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
#define A first
#define B second
#define SZ(x) int(x.size())
#define PB push_back
#define FR(i, a, b) for (int i = (a); i < (b); i++)
#define FOR(i, n) FR(i, 0, n)
#define RF(i, a, b) for (int i = (a); i >= (b); i--)
#define FRA(a, x) for (auto a: (x))
template <typename T> inline void set_min(T &a, T b) {if(b < a) a = b;}
template <typename T> inline void set_max(T &a, T b) {if(b > a) a = b;}
int n, q;
string se;
const int MXN = 20;
int dp[MXN][1<<MXN];
int dp2[MXN][1<<MXN];
int val[1<<MXN];
int ms[MXN];
int p;
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> q;
cin >> se;
FOR(i, 1<<n) {
val[i] = (se[i] - '0');
}
FOR(b, n) {
FOR(i, 1<<n) {
if (b == 0) {
dp[b][i] = ((i & (1 << b)) > 0 ? val[i] + val[i ^ (1<<b)] : val[i]);
dp2[b][i] = ((i & (1 << b)) == 0 ? val[i] + val[i ^ (1<<b)] : val[i]);
}
else {
dp[b][i] = ((i & (1 << b)) > 0 ? dp[b-1][i] + dp[b-1][i ^ (1<<b)] : dp[b-1][i]);
dp2[b][i] = ((i & (1 << b)) == 0 ? dp2[b-1][i] + dp2[b-1][i ^ (1<<b)] : dp2[b-1][i]);
}
}
}
string s;
FOR(_, q) {
cin >> s;
reverse(s.begin(), s.end());
int n0 = 0, n1 = 0, nq = 0;
FRA(x, s) {
if (x == '?') nq++;
else if (x == '0') n0++;
else n1++;
}
int ans = 0;
if (nq <= 6) {
// just add over the bitmasks
int main = 0;
FOR(i, n) {
if (s[i] == '?') ms[p++] = i;
else if (s[i] == '1') main += (1 << i);
}
FOR(i, 1<<nq) {
int p = main;
FOR(j, nq) {
if (i & (1 << j)) p += (1 << ms[j]);
}
ans += val[p];
}
}
else if (n0 <= 6) {
// 0011??
// super(001100)
// -super(011100)
// -super(101100)
// +super(111100)
int main = 0;
FOR(i, n) {
if (s[i] == '0') ms[p++] = i;
else if (s[i] == '1') main += (1 << i);
}
FOR(i, 1<<n0) {
int p = main;
FOR(j, n0) {
if (i & (1 << j)) {
p += (1 << ms[j]);
}
}
if (__builtin_popcount(i) % 2 == 0) ans += dp2[n-1][p];
else ans -= dp2[n-1][p];
}
}
else { // n1 <= 6
// 0011??
// sub(001111)
// -sub(001011)
// -sub(000111)
// +sub(000011)
int main = 0;
FOR(i, n) {
if (s[i] == '1') ms[p++] = i;
else if (s[i] == '?') main += (1 << i);
}
FOR(i, 1<<n1) {
int p = main;
FOR(j, n1) {
if (i & (1 << j)) p += (1 << ms[j]);
}
if ((n1 - __builtin_popcount(i)) % 2 == 0) ans += dp[n-1][p];
else ans -= dp[n-1][p];
}
}
cout << ans << '\n';
p = 0;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41308 KB |
Output is correct |
2 |
Correct |
5 ms |
41308 KB |
Output is correct |
3 |
Correct |
5 ms |
41308 KB |
Output is correct |
4 |
Correct |
4 ms |
41308 KB |
Output is correct |
5 |
Correct |
5 ms |
41460 KB |
Output is correct |
6 |
Correct |
5 ms |
41308 KB |
Output is correct |
7 |
Correct |
5 ms |
41308 KB |
Output is correct |
8 |
Correct |
5 ms |
41308 KB |
Output is correct |
9 |
Correct |
4 ms |
41432 KB |
Output is correct |
10 |
Correct |
5 ms |
41308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41308 KB |
Output is correct |
2 |
Correct |
5 ms |
41308 KB |
Output is correct |
3 |
Correct |
5 ms |
41308 KB |
Output is correct |
4 |
Correct |
4 ms |
41308 KB |
Output is correct |
5 |
Correct |
5 ms |
41460 KB |
Output is correct |
6 |
Correct |
5 ms |
41308 KB |
Output is correct |
7 |
Correct |
5 ms |
41308 KB |
Output is correct |
8 |
Correct |
5 ms |
41308 KB |
Output is correct |
9 |
Correct |
4 ms |
41432 KB |
Output is correct |
10 |
Correct |
5 ms |
41308 KB |
Output is correct |
11 |
Correct |
353 ms |
45396 KB |
Output is correct |
12 |
Correct |
401 ms |
45172 KB |
Output is correct |
13 |
Correct |
251 ms |
44704 KB |
Output is correct |
14 |
Correct |
234 ms |
44412 KB |
Output is correct |
15 |
Correct |
694 ms |
45376 KB |
Output is correct |
16 |
Correct |
337 ms |
44624 KB |
Output is correct |
17 |
Correct |
328 ms |
44556 KB |
Output is correct |
18 |
Correct |
145 ms |
46716 KB |
Output is correct |
19 |
Correct |
173 ms |
43356 KB |
Output is correct |
20 |
Correct |
381 ms |
45200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41308 KB |
Output is correct |
2 |
Correct |
5 ms |
41308 KB |
Output is correct |
3 |
Correct |
5 ms |
41308 KB |
Output is correct |
4 |
Correct |
4 ms |
41308 KB |
Output is correct |
5 |
Correct |
5 ms |
41460 KB |
Output is correct |
6 |
Correct |
5 ms |
41308 KB |
Output is correct |
7 |
Correct |
5 ms |
41308 KB |
Output is correct |
8 |
Correct |
5 ms |
41308 KB |
Output is correct |
9 |
Correct |
4 ms |
41432 KB |
Output is correct |
10 |
Correct |
5 ms |
41308 KB |
Output is correct |
11 |
Correct |
353 ms |
45396 KB |
Output is correct |
12 |
Correct |
401 ms |
45172 KB |
Output is correct |
13 |
Correct |
251 ms |
44704 KB |
Output is correct |
14 |
Correct |
234 ms |
44412 KB |
Output is correct |
15 |
Correct |
694 ms |
45376 KB |
Output is correct |
16 |
Correct |
337 ms |
44624 KB |
Output is correct |
17 |
Correct |
328 ms |
44556 KB |
Output is correct |
18 |
Correct |
145 ms |
46716 KB |
Output is correct |
19 |
Correct |
173 ms |
43356 KB |
Output is correct |
20 |
Correct |
381 ms |
45200 KB |
Output is correct |
21 |
Correct |
189 ms |
57864 KB |
Output is correct |
22 |
Correct |
620 ms |
58196 KB |
Output is correct |
23 |
Correct |
368 ms |
57016 KB |
Output is correct |
24 |
Correct |
328 ms |
56656 KB |
Output is correct |
25 |
Correct |
297 ms |
58964 KB |
Output is correct |
26 |
Correct |
454 ms |
57232 KB |
Output is correct |
27 |
Correct |
427 ms |
57200 KB |
Output is correct |
28 |
Correct |
174 ms |
59684 KB |
Output is correct |
29 |
Correct |
199 ms |
55632 KB |
Output is correct |
30 |
Correct |
387 ms |
57960 KB |
Output is correct |
31 |
Correct |
760 ms |
57888 KB |
Output is correct |
32 |
Correct |
440 ms |
57644 KB |
Output is correct |
33 |
Correct |
270 ms |
56748 KB |
Output is correct |
34 |
Correct |
340 ms |
56916 KB |
Output is correct |
35 |
Correct |
426 ms |
57164 KB |
Output is correct |
36 |
Correct |
155 ms |
55676 KB |
Output is correct |
37 |
Correct |
720 ms |
57940 KB |
Output is correct |
38 |
Correct |
208 ms |
55792 KB |
Output is correct |
39 |
Correct |
362 ms |
57168 KB |
Output is correct |
40 |
Correct |
330 ms |
56748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41308 KB |
Output is correct |
2 |
Correct |
5 ms |
41308 KB |
Output is correct |
3 |
Correct |
5 ms |
41308 KB |
Output is correct |
4 |
Correct |
4 ms |
41308 KB |
Output is correct |
5 |
Correct |
5 ms |
41460 KB |
Output is correct |
6 |
Correct |
5 ms |
41308 KB |
Output is correct |
7 |
Correct |
5 ms |
41308 KB |
Output is correct |
8 |
Correct |
5 ms |
41308 KB |
Output is correct |
9 |
Correct |
4 ms |
41432 KB |
Output is correct |
10 |
Correct |
5 ms |
41308 KB |
Output is correct |
11 |
Runtime error |
24 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41308 KB |
Output is correct |
2 |
Correct |
5 ms |
41308 KB |
Output is correct |
3 |
Correct |
5 ms |
41308 KB |
Output is correct |
4 |
Correct |
4 ms |
41308 KB |
Output is correct |
5 |
Correct |
5 ms |
41460 KB |
Output is correct |
6 |
Correct |
5 ms |
41308 KB |
Output is correct |
7 |
Correct |
5 ms |
41308 KB |
Output is correct |
8 |
Correct |
5 ms |
41308 KB |
Output is correct |
9 |
Correct |
4 ms |
41432 KB |
Output is correct |
10 |
Correct |
5 ms |
41308 KB |
Output is correct |
11 |
Correct |
353 ms |
45396 KB |
Output is correct |
12 |
Correct |
401 ms |
45172 KB |
Output is correct |
13 |
Correct |
251 ms |
44704 KB |
Output is correct |
14 |
Correct |
234 ms |
44412 KB |
Output is correct |
15 |
Correct |
694 ms |
45376 KB |
Output is correct |
16 |
Correct |
337 ms |
44624 KB |
Output is correct |
17 |
Correct |
328 ms |
44556 KB |
Output is correct |
18 |
Correct |
145 ms |
46716 KB |
Output is correct |
19 |
Correct |
173 ms |
43356 KB |
Output is correct |
20 |
Correct |
381 ms |
45200 KB |
Output is correct |
21 |
Correct |
189 ms |
57864 KB |
Output is correct |
22 |
Correct |
620 ms |
58196 KB |
Output is correct |
23 |
Correct |
368 ms |
57016 KB |
Output is correct |
24 |
Correct |
328 ms |
56656 KB |
Output is correct |
25 |
Correct |
297 ms |
58964 KB |
Output is correct |
26 |
Correct |
454 ms |
57232 KB |
Output is correct |
27 |
Correct |
427 ms |
57200 KB |
Output is correct |
28 |
Correct |
174 ms |
59684 KB |
Output is correct |
29 |
Correct |
199 ms |
55632 KB |
Output is correct |
30 |
Correct |
387 ms |
57960 KB |
Output is correct |
31 |
Correct |
760 ms |
57888 KB |
Output is correct |
32 |
Correct |
440 ms |
57644 KB |
Output is correct |
33 |
Correct |
270 ms |
56748 KB |
Output is correct |
34 |
Correct |
340 ms |
56916 KB |
Output is correct |
35 |
Correct |
426 ms |
57164 KB |
Output is correct |
36 |
Correct |
155 ms |
55676 KB |
Output is correct |
37 |
Correct |
720 ms |
57940 KB |
Output is correct |
38 |
Correct |
208 ms |
55792 KB |
Output is correct |
39 |
Correct |
362 ms |
57168 KB |
Output is correct |
40 |
Correct |
330 ms |
56748 KB |
Output is correct |
41 |
Runtime error |
24 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |