#include <bits/stdc++.h>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int L = (1 << 20) + 5;
int l, q, cost[L], defMask = 0, sumSubset[L], sumSubsetOf[L];
int getMask(vector<int> &pos, int replaceMask) {
int ansMask = defMask;
fw (i, 0, pos.size()) {
int bitID = pos[i];
int bit = ((replaceMask >> i) & 1);
ansMask |= (bit << bitID);
}
return ansMask;
}
signed main() {
#ifdef BLU
in("blu.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> l >> q;
fw (i, 0, (1 << l)) {
char c;
cin >> c;
cost[i] = c - '0';
}
//sumSubset[i] stores sum of costs of j that j & i = j.
fw (i, 0, (1 << l)) sumSubset[i] = cost[i];
fw (i, 0, l) fw (mask, 0, (1 << l)) {
if (mask & (1 << i)) sumSubset[mask] += sumSubset[mask ^ (1 << i)];
}
//sumSubsetOf[i] stores sum of costs of j that j & i = i
fw (i, 0, (1 << l)) sumSubsetOf[i] = cost[i];
fw (i, 0, l) bw (mask, (1 << l), 0) {
if (!(mask & (1 << i))) sumSubsetOf[mask] += sumSubsetOf[mask | (1 << i)];
}
while (q--) {
string t;
cin >> t;
reverse(t.begin(), t.end());
int cnt0 = 0, cnt1 = 1, cntQ = 0;
fw (i, 0, t.length()) {
if (t[i] == '0') cnt0++;
else if (t[i] == '1') cnt1++;
else cntQ++;
}
//At least one of these is <= 6. Iterate over them.
int ans = 0;
defMask = 0;
vector<int> pos; pos.clear();
if (cntQ <= 6) {
fw (i, 0, t.length()) {
if (t[i] == '?') pos.pb(i);
else defMask |= ((t[i] - '0') << i);
}
fw (replacementMask, 0, (1 << cntQ)) {
int mask = getMask(pos, replacementMask);
ans += cost[mask];
}
} else if (cnt0 <= 6) {
fw (i, 0, t.length()) {
if (t[i] == '0') pos.pb(i);
else {
if (t[i] == '1') defMask |= (1 << i);
}
}
fw (replacementMask, 0, (1 << cnt0)) {
int mask = getMask(pos, replacementMask);
if (bp(replacementMask) % 2 == 0) {
ans += sumSubsetOf[mask];
} else ans -= sumSubsetOf[mask];
}
} else if (cnt1 <= 6) {
fw (i, 0, t.length()) {
if (t[i] == '1') pos.pb(i);
else {
if (t[i] == '?') defMask |= (1 << i);
}
}
fw (replacementMask, 0, (1 << cnt1)) {
int mask = getMask(pos, replacementMask);
if ((cnt1 - bp(replacementMask)) % 2 == 0) {
ans += sumSubset[mask];
} else ans -= sumSubset[mask];
}
}
cout << ans << "\n";
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int getMask(std::vector<int>&, int)':
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:20:6:
fw (i, 0, pos.size()) {
~~~~~~~~~~~~~~~~
snake_escaping.cpp:20:2: note: in expansion of macro 'fw'
fw (i, 0, pos.size()) {
^~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:53:7:
fw (i, 0, t.length()) {
~~~~~~~~~~~~~~~~
snake_escaping.cpp:53:3: note: in expansion of macro 'fw'
fw (i, 0, t.length()) {
^~
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:63:8:
fw (i, 0, t.length()) {
~~~~~~~~~~~~~~~~
snake_escaping.cpp:63:4: note: in expansion of macro 'fw'
fw (i, 0, t.length()) {
^~
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:72:8:
fw (i, 0, t.length()) {
~~~~~~~~~~~~~~~~
snake_escaping.cpp:72:4: note: in expansion of macro 'fw'
fw (i, 0, t.length()) {
^~
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:85:8:
fw (i, 0, t.length()) {
~~~~~~~~~~~~~~~~
snake_escaping.cpp:85:4: note: in expansion of macro 'fw'
fw (i, 0, t.length()) {
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
863 ms |
15164 KB |
Output is correct |
12 |
Correct |
808 ms |
14840 KB |
Output is correct |
13 |
Correct |
526 ms |
14248 KB |
Output is correct |
14 |
Correct |
563 ms |
14232 KB |
Output is correct |
15 |
Correct |
1302 ms |
15172 KB |
Output is correct |
16 |
Correct |
672 ms |
14460 KB |
Output is correct |
17 |
Correct |
653 ms |
14432 KB |
Output is correct |
18 |
Correct |
212 ms |
16120 KB |
Output is correct |
19 |
Correct |
256 ms |
13048 KB |
Output is correct |
20 |
Correct |
781 ms |
14968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
863 ms |
15164 KB |
Output is correct |
12 |
Correct |
808 ms |
14840 KB |
Output is correct |
13 |
Correct |
526 ms |
14248 KB |
Output is correct |
14 |
Correct |
563 ms |
14232 KB |
Output is correct |
15 |
Correct |
1302 ms |
15172 KB |
Output is correct |
16 |
Correct |
672 ms |
14460 KB |
Output is correct |
17 |
Correct |
653 ms |
14432 KB |
Output is correct |
18 |
Correct |
212 ms |
16120 KB |
Output is correct |
19 |
Correct |
256 ms |
13048 KB |
Output is correct |
20 |
Correct |
781 ms |
14968 KB |
Output is correct |
21 |
Correct |
391 ms |
18168 KB |
Output is correct |
22 |
Correct |
1132 ms |
18496 KB |
Output is correct |
23 |
Correct |
728 ms |
17528 KB |
Output is correct |
24 |
Correct |
697 ms |
17412 KB |
Output is correct |
25 |
Correct |
634 ms |
19160 KB |
Output is correct |
26 |
Correct |
870 ms |
18020 KB |
Output is correct |
27 |
Correct |
830 ms |
17652 KB |
Output is correct |
28 |
Correct |
232 ms |
20208 KB |
Output is correct |
29 |
Correct |
301 ms |
16124 KB |
Output is correct |
30 |
Correct |
740 ms |
18280 KB |
Output is correct |
31 |
Correct |
1391 ms |
18196 KB |
Output is correct |
32 |
Correct |
966 ms |
18160 KB |
Output is correct |
33 |
Correct |
539 ms |
17144 KB |
Output is correct |
34 |
Correct |
702 ms |
17352 KB |
Output is correct |
35 |
Correct |
826 ms |
17580 KB |
Output is correct |
36 |
Correct |
227 ms |
16104 KB |
Output is correct |
37 |
Correct |
1347 ms |
18276 KB |
Output is correct |
38 |
Correct |
312 ms |
16236 KB |
Output is correct |
39 |
Correct |
703 ms |
17456 KB |
Output is correct |
40 |
Correct |
702 ms |
17192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
111 ms |
15020 KB |
Output is correct |
12 |
Incorrect |
119 ms |
14808 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
863 ms |
15164 KB |
Output is correct |
12 |
Correct |
808 ms |
14840 KB |
Output is correct |
13 |
Correct |
526 ms |
14248 KB |
Output is correct |
14 |
Correct |
563 ms |
14232 KB |
Output is correct |
15 |
Correct |
1302 ms |
15172 KB |
Output is correct |
16 |
Correct |
672 ms |
14460 KB |
Output is correct |
17 |
Correct |
653 ms |
14432 KB |
Output is correct |
18 |
Correct |
212 ms |
16120 KB |
Output is correct |
19 |
Correct |
256 ms |
13048 KB |
Output is correct |
20 |
Correct |
781 ms |
14968 KB |
Output is correct |
21 |
Correct |
391 ms |
18168 KB |
Output is correct |
22 |
Correct |
1132 ms |
18496 KB |
Output is correct |
23 |
Correct |
728 ms |
17528 KB |
Output is correct |
24 |
Correct |
697 ms |
17412 KB |
Output is correct |
25 |
Correct |
634 ms |
19160 KB |
Output is correct |
26 |
Correct |
870 ms |
18020 KB |
Output is correct |
27 |
Correct |
830 ms |
17652 KB |
Output is correct |
28 |
Correct |
232 ms |
20208 KB |
Output is correct |
29 |
Correct |
301 ms |
16124 KB |
Output is correct |
30 |
Correct |
740 ms |
18280 KB |
Output is correct |
31 |
Correct |
1391 ms |
18196 KB |
Output is correct |
32 |
Correct |
966 ms |
18160 KB |
Output is correct |
33 |
Correct |
539 ms |
17144 KB |
Output is correct |
34 |
Correct |
702 ms |
17352 KB |
Output is correct |
35 |
Correct |
826 ms |
17580 KB |
Output is correct |
36 |
Correct |
227 ms |
16104 KB |
Output is correct |
37 |
Correct |
1347 ms |
18276 KB |
Output is correct |
38 |
Correct |
312 ms |
16236 KB |
Output is correct |
39 |
Correct |
703 ms |
17456 KB |
Output is correct |
40 |
Correct |
702 ms |
17192 KB |
Output is correct |
41 |
Correct |
111 ms |
15020 KB |
Output is correct |
42 |
Incorrect |
119 ms |
14808 KB |
Output isn't correct |
43 |
Halted |
0 ms |
0 KB |
- |