#include<iostream>
using namespace std;
const int MAXN = 21;
int n, q;
char l[MAXN];
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)];
}
}
}
void ispis (int x) {
for (int i=n-1; i>=0; i--) {
cout << (bool) (x & (1<<i));
}
cout << endl;
}
int main () {
std::ios::sync_with_stdio(false);
cin >> n >> q;
for (int i=0; i<(1<<n); i++) {
char c;
cin >> c;
val[i] = c - '0';
}
precompute();
for (int tc=0; tc<q; tc++) {
int a = 0, b = 0, c = 0;
for (int i=0; i<n; i++) {
cin >> l[i];
if (l[i] == '?') {
a ^= (1<<(n-1-i));
} else if (l[i] == '0') {
b ^= (1<<(n-1-i));
} else {
c ^= (1<<(n-1-i));
}
}
int sol = 0, mask = 0, cnt = 0;
if (pc[a] <= n/3) {
do {
cnt++;
sol += val[mask | c];
} while (mask = (mask-a) & a);
} else if (pc[b] <= n/3) {
do {
cnt++;
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 {
cnt++;
if (pc[mask | a] & 1) {
sol -= sum0[mask | a];
} else {
sol += sum0[mask | a];
}
} while (mask = (mask-c) & c);
if (sol < 0) sol = -sol;
}
cout << sol << "\n";
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:58:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
} while (mask = (mask-a) & a);
~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp:67:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
} while (mask = (mask-b) & b);
~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp:77:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
} while (mask = (mask-c) & c);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
520 KB |
Output is correct |
4 |
Correct |
4 ms |
520 KB |
Output is correct |
5 |
Correct |
4 ms |
520 KB |
Output is correct |
6 |
Correct |
5 ms |
612 KB |
Output is correct |
7 |
Correct |
5 ms |
612 KB |
Output is correct |
8 |
Correct |
4 ms |
628 KB |
Output is correct |
9 |
Correct |
4 ms |
628 KB |
Output is correct |
10 |
Correct |
4 ms |
628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
520 KB |
Output is correct |
4 |
Correct |
4 ms |
520 KB |
Output is correct |
5 |
Correct |
4 ms |
520 KB |
Output is correct |
6 |
Correct |
5 ms |
612 KB |
Output is correct |
7 |
Correct |
5 ms |
612 KB |
Output is correct |
8 |
Correct |
4 ms |
628 KB |
Output is correct |
9 |
Correct |
4 ms |
628 KB |
Output is correct |
10 |
Correct |
4 ms |
628 KB |
Output is correct |
11 |
Correct |
1795 ms |
4492 KB |
Output is correct |
12 |
Correct |
1909 ms |
4492 KB |
Output is correct |
13 |
Correct |
1930 ms |
4492 KB |
Output is correct |
14 |
Execution timed out |
2049 ms |
4492 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
520 KB |
Output is correct |
4 |
Correct |
4 ms |
520 KB |
Output is correct |
5 |
Correct |
4 ms |
520 KB |
Output is correct |
6 |
Correct |
5 ms |
612 KB |
Output is correct |
7 |
Correct |
5 ms |
612 KB |
Output is correct |
8 |
Correct |
4 ms |
628 KB |
Output is correct |
9 |
Correct |
4 ms |
628 KB |
Output is correct |
10 |
Correct |
4 ms |
628 KB |
Output is correct |
11 |
Correct |
1795 ms |
4492 KB |
Output is correct |
12 |
Correct |
1909 ms |
4492 KB |
Output is correct |
13 |
Correct |
1930 ms |
4492 KB |
Output is correct |
14 |
Execution timed out |
2049 ms |
4492 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
520 KB |
Output is correct |
4 |
Correct |
4 ms |
520 KB |
Output is correct |
5 |
Correct |
4 ms |
520 KB |
Output is correct |
6 |
Correct |
5 ms |
612 KB |
Output is correct |
7 |
Correct |
5 ms |
612 KB |
Output is correct |
8 |
Correct |
4 ms |
628 KB |
Output is correct |
9 |
Correct |
4 ms |
628 KB |
Output is correct |
10 |
Correct |
4 ms |
628 KB |
Output is correct |
11 |
Correct |
217 ms |
17228 KB |
Output is correct |
12 |
Correct |
200 ms |
17312 KB |
Output is correct |
13 |
Correct |
243 ms |
17312 KB |
Output is correct |
14 |
Correct |
293 ms |
17324 KB |
Output is correct |
15 |
Correct |
215 ms |
17452 KB |
Output is correct |
16 |
Correct |
226 ms |
17452 KB |
Output is correct |
17 |
Correct |
210 ms |
17452 KB |
Output is correct |
18 |
Correct |
168 ms |
17580 KB |
Output is correct |
19 |
Correct |
198 ms |
17580 KB |
Output is correct |
20 |
Correct |
177 ms |
17580 KB |
Output is correct |
21 |
Correct |
243 ms |
17580 KB |
Output is correct |
22 |
Correct |
229 ms |
17580 KB |
Output is correct |
23 |
Correct |
175 ms |
17580 KB |
Output is correct |
24 |
Correct |
193 ms |
17580 KB |
Output is correct |
25 |
Correct |
212 ms |
17580 KB |
Output is correct |
26 |
Correct |
227 ms |
17580 KB |
Output is correct |
27 |
Correct |
208 ms |
17580 KB |
Output is correct |
28 |
Correct |
181 ms |
17580 KB |
Output is correct |
29 |
Correct |
183 ms |
17580 KB |
Output is correct |
30 |
Correct |
196 ms |
17580 KB |
Output is correct |
31 |
Correct |
183 ms |
17580 KB |
Output is correct |
32 |
Correct |
217 ms |
17580 KB |
Output is correct |
33 |
Correct |
245 ms |
17580 KB |
Output is correct |
34 |
Correct |
159 ms |
17580 KB |
Output is correct |
35 |
Correct |
220 ms |
17580 KB |
Output is correct |
36 |
Correct |
229 ms |
17580 KB |
Output is correct |
37 |
Correct |
230 ms |
17580 KB |
Output is correct |
38 |
Correct |
236 ms |
17580 KB |
Output is correct |
39 |
Correct |
238 ms |
17580 KB |
Output is correct |
40 |
Correct |
246 ms |
17580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
520 KB |
Output is correct |
4 |
Correct |
4 ms |
520 KB |
Output is correct |
5 |
Correct |
4 ms |
520 KB |
Output is correct |
6 |
Correct |
5 ms |
612 KB |
Output is correct |
7 |
Correct |
5 ms |
612 KB |
Output is correct |
8 |
Correct |
4 ms |
628 KB |
Output is correct |
9 |
Correct |
4 ms |
628 KB |
Output is correct |
10 |
Correct |
4 ms |
628 KB |
Output is correct |
11 |
Correct |
1795 ms |
4492 KB |
Output is correct |
12 |
Correct |
1909 ms |
4492 KB |
Output is correct |
13 |
Correct |
1930 ms |
4492 KB |
Output is correct |
14 |
Execution timed out |
2049 ms |
4492 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |