#include<iostream>
using namespace std;
const int MAXN = 21;
int n, q, a, b, c, sol, mask;
char l[MAXN], cc;
int val[1<<MAXN], sum0[1<<MAXN], sum1[1<<MAXN], pc[1<<MAXN];
void precompute () {
for (int i=0; i<(1<<n); i++) {
pc[i] = __builtin_popcount(i);
sum0[i] = val[i];
sum1[i] = val[i ^ ((1<<n) - 1)];
}
for (int i=0; i<n; i++) {
for (int j=0; j<(1<<n); j++) {
if (j & (1<<i)) sum0[j] += sum0[j ^ (1<<i)];
if (j & (1<<i)) sum1[j] += sum1[j ^ (1<<i)];
}
}
}
int main () {
cin >> n >> q;
for (int i=0; i<(1<<n); i++) {
do {
cc = getchar_unlocked();
} while (!((cc>='0' && cc<='9') || cc=='?'));
val[i] = cc - '0';
}
precompute();
for (int tc=0; tc<q; tc++) {
a = b = c = 0;
for (int i=0; i<n; i++) {
do {
cc = getchar_unlocked();
} while (!((cc>='0' && cc<='9') || cc=='?'));
l[i] = cc;
if (l[i] == '?') {
a ^= (1<<(n-1-i));
} else if (l[i] == '0') {
b ^= (1<<(n-1-i));
} else {
c ^= (1<<(n-1-i));
}
}
sol = 0, mask = 0;
if (pc[a] <= n/3) {
do {
sol += val[mask | c];
} while (mask = (mask-a) & a);
} else if (pc[b] <= n/3) {
do {
if (pc[mask | a] & 1) {
sol -= sum1[mask | a];
} else {
sol += sum1[mask | a];
}
} while (mask = (mask-b) & b);
if (sol < 0) sol = -sol;
} else {
do {
if (pc[mask | a] & 1) {
sol -= sum0[mask | a];
} else {
sol += sum0[mask | a];
}
} while (mask = (mask-c) & c);
if (sol < 0) sol = -sol;
}
printf("%d\n", sol);
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:53:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
} while (mask = (mask-a) & a);
~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp:61:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
} while (mask = (mask-b) & b);
~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp:70:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
} while (mask = (mask-c) & c);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
568 KB |
Output is correct |
6 |
Correct |
3 ms |
580 KB |
Output is correct |
7 |
Correct |
3 ms |
588 KB |
Output is correct |
8 |
Correct |
3 ms |
616 KB |
Output is correct |
9 |
Correct |
3 ms |
664 KB |
Output is correct |
10 |
Correct |
5 ms |
664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
568 KB |
Output is correct |
6 |
Correct |
3 ms |
580 KB |
Output is correct |
7 |
Correct |
3 ms |
588 KB |
Output is correct |
8 |
Correct |
3 ms |
616 KB |
Output is correct |
9 |
Correct |
3 ms |
664 KB |
Output is correct |
10 |
Correct |
5 ms |
664 KB |
Output is correct |
11 |
Correct |
252 ms |
8036 KB |
Output is correct |
12 |
Correct |
311 ms |
18520 KB |
Output is correct |
13 |
Correct |
277 ms |
19240 KB |
Output is correct |
14 |
Correct |
253 ms |
19440 KB |
Output is correct |
15 |
Correct |
242 ms |
20332 KB |
Output is correct |
16 |
Correct |
304 ms |
30288 KB |
Output is correct |
17 |
Correct |
307 ms |
34936 KB |
Output is correct |
18 |
Correct |
188 ms |
36832 KB |
Output is correct |
19 |
Correct |
241 ms |
36832 KB |
Output is correct |
20 |
Correct |
264 ms |
36832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
568 KB |
Output is correct |
6 |
Correct |
3 ms |
580 KB |
Output is correct |
7 |
Correct |
3 ms |
588 KB |
Output is correct |
8 |
Correct |
3 ms |
616 KB |
Output is correct |
9 |
Correct |
3 ms |
664 KB |
Output is correct |
10 |
Correct |
5 ms |
664 KB |
Output is correct |
11 |
Correct |
252 ms |
8036 KB |
Output is correct |
12 |
Correct |
311 ms |
18520 KB |
Output is correct |
13 |
Correct |
277 ms |
19240 KB |
Output is correct |
14 |
Correct |
253 ms |
19440 KB |
Output is correct |
15 |
Correct |
242 ms |
20332 KB |
Output is correct |
16 |
Correct |
304 ms |
30288 KB |
Output is correct |
17 |
Correct |
307 ms |
34936 KB |
Output is correct |
18 |
Correct |
188 ms |
36832 KB |
Output is correct |
19 |
Correct |
241 ms |
36832 KB |
Output is correct |
20 |
Correct |
264 ms |
36832 KB |
Output is correct |
21 |
Correct |
296 ms |
36832 KB |
Output is correct |
22 |
Correct |
312 ms |
36832 KB |
Output is correct |
23 |
Correct |
311 ms |
36832 KB |
Output is correct |
24 |
Correct |
327 ms |
36832 KB |
Output is correct |
25 |
Correct |
317 ms |
50736 KB |
Output is correct |
26 |
Correct |
362 ms |
57256 KB |
Output is correct |
27 |
Correct |
320 ms |
58076 KB |
Output is correct |
28 |
Correct |
186 ms |
58748 KB |
Output is correct |
29 |
Correct |
270 ms |
58748 KB |
Output is correct |
30 |
Correct |
324 ms |
58748 KB |
Output is correct |
31 |
Correct |
309 ms |
58748 KB |
Output is correct |
32 |
Correct |
313 ms |
58748 KB |
Output is correct |
33 |
Runtime error |
340 ms |
66560 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
568 KB |
Output is correct |
6 |
Correct |
3 ms |
580 KB |
Output is correct |
7 |
Correct |
3 ms |
588 KB |
Output is correct |
8 |
Correct |
3 ms |
616 KB |
Output is correct |
9 |
Correct |
3 ms |
664 KB |
Output is correct |
10 |
Correct |
5 ms |
664 KB |
Output is correct |
11 |
Runtime error |
103 ms |
66560 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
568 KB |
Output is correct |
6 |
Correct |
3 ms |
580 KB |
Output is correct |
7 |
Correct |
3 ms |
588 KB |
Output is correct |
8 |
Correct |
3 ms |
616 KB |
Output is correct |
9 |
Correct |
3 ms |
664 KB |
Output is correct |
10 |
Correct |
5 ms |
664 KB |
Output is correct |
11 |
Correct |
252 ms |
8036 KB |
Output is correct |
12 |
Correct |
311 ms |
18520 KB |
Output is correct |
13 |
Correct |
277 ms |
19240 KB |
Output is correct |
14 |
Correct |
253 ms |
19440 KB |
Output is correct |
15 |
Correct |
242 ms |
20332 KB |
Output is correct |
16 |
Correct |
304 ms |
30288 KB |
Output is correct |
17 |
Correct |
307 ms |
34936 KB |
Output is correct |
18 |
Correct |
188 ms |
36832 KB |
Output is correct |
19 |
Correct |
241 ms |
36832 KB |
Output is correct |
20 |
Correct |
264 ms |
36832 KB |
Output is correct |
21 |
Correct |
296 ms |
36832 KB |
Output is correct |
22 |
Correct |
312 ms |
36832 KB |
Output is correct |
23 |
Correct |
311 ms |
36832 KB |
Output is correct |
24 |
Correct |
327 ms |
36832 KB |
Output is correct |
25 |
Correct |
317 ms |
50736 KB |
Output is correct |
26 |
Correct |
362 ms |
57256 KB |
Output is correct |
27 |
Correct |
320 ms |
58076 KB |
Output is correct |
28 |
Correct |
186 ms |
58748 KB |
Output is correct |
29 |
Correct |
270 ms |
58748 KB |
Output is correct |
30 |
Correct |
324 ms |
58748 KB |
Output is correct |
31 |
Correct |
309 ms |
58748 KB |
Output is correct |
32 |
Correct |
313 ms |
58748 KB |
Output is correct |
33 |
Runtime error |
340 ms |
66560 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
34 |
Halted |
0 ms |
0 KB |
- |