#include<iostream>
using namespace std;
const int MAXN = (1<<20) + 10;
int n, q, a, b, c, sol, mask, N, rev, count, i, j, tc;
char l[30], cc;
int val[MAXN], sum0[MAXN], sum1[MAXN], pc[MAXN];
void precompute () {
for (i=0; i<n; i++) {
for (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)];
}
}
}
#define pcc(x) putchar(x);
int main () {
cin >> n >> q;
do {
cc = getchar();
} while (!(cc>='0' && cc<='9'));
for (i=0; i<(1<<n); i++) {
if (i>0) {
cc = getchar();
}
val[i] = cc - '0';
pc[i] = pc[i - (i&-i)]+1;
sum0[i] = val[i];
}
for (int i=0; i<(1<<n); i++) {
sum1[i] = val[i ^ ((1<<n) - 1)];
}
precompute();
for (tc=0; tc<q; tc++) {
a = b = c = 0;
do {
cc = getchar();
} while (!(cc=='0' || cc=='1' || cc=='?'));
for (i=0; i<n; i++) {
if (i>0) cc = getchar();
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] <= pc[b] && pc[a] <= pc[c]) {
for (mask = a; ; mask = (mask - 1) & a) {
sol += val[mask | c];
if (!mask) break;
}
} else if (pc[b] <= pc[a] && pc[b] <= pc[c]) {
for (mask = b; ; mask = (mask-1) & b) {
rev = mask | a;
if (pc[rev] & 1) {
sol -= sum1[rev];
} else {
sol += sum1[rev];
}
if (!mask) break;
}
if (sol < 0) sol = -sol;
} else {
for (mask = c; ; mask = (mask - 1) & c) {
rev = mask | a;
if (pc[rev] & 1) {
sol -= sum0[rev];
} else {
sol += sum0[rev];
}
if (!mask) break;
}
if (sol < 0) sol = -sol;
}
N = sol, rev, count = 0;
rev = N;
if (N == 0) { pcc('0'); putchar('\n'); continue ;}
while ((rev % 10) == 0) { count++; rev /= 10;} //obtain the count of the number of 0s
rev = 0;
while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;} //store reverse of N in rev
while (rev != 0) { pcc(rev % 10 + '0'); rev /= 10;}
while (count--) pcc('0');
putchar('\n');
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:84:31: warning: right operand of comma operator has no effect [-Wunused-value]
N = sol, rev, count = 0;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
428 KB |
Output is correct |
4 |
Correct |
3 ms |
436 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
736 KB |
Output is correct |
8 |
Correct |
3 ms |
736 KB |
Output is correct |
9 |
Correct |
3 ms |
736 KB |
Output is correct |
10 |
Correct |
3 ms |
736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
428 KB |
Output is correct |
4 |
Correct |
3 ms |
436 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
736 KB |
Output is correct |
8 |
Correct |
3 ms |
736 KB |
Output is correct |
9 |
Correct |
3 ms |
736 KB |
Output is correct |
10 |
Correct |
3 ms |
736 KB |
Output is correct |
11 |
Correct |
260 ms |
4980 KB |
Output is correct |
12 |
Correct |
305 ms |
4980 KB |
Output is correct |
13 |
Correct |
258 ms |
4980 KB |
Output is correct |
14 |
Correct |
356 ms |
4980 KB |
Output is correct |
15 |
Correct |
269 ms |
5512 KB |
Output is correct |
16 |
Correct |
261 ms |
5512 KB |
Output is correct |
17 |
Correct |
322 ms |
5512 KB |
Output is correct |
18 |
Correct |
335 ms |
6968 KB |
Output is correct |
19 |
Correct |
217 ms |
6968 KB |
Output is correct |
20 |
Correct |
280 ms |
6968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
428 KB |
Output is correct |
4 |
Correct |
3 ms |
436 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
736 KB |
Output is correct |
8 |
Correct |
3 ms |
736 KB |
Output is correct |
9 |
Correct |
3 ms |
736 KB |
Output is correct |
10 |
Correct |
3 ms |
736 KB |
Output is correct |
11 |
Correct |
260 ms |
4980 KB |
Output is correct |
12 |
Correct |
305 ms |
4980 KB |
Output is correct |
13 |
Correct |
258 ms |
4980 KB |
Output is correct |
14 |
Correct |
356 ms |
4980 KB |
Output is correct |
15 |
Correct |
269 ms |
5512 KB |
Output is correct |
16 |
Correct |
261 ms |
5512 KB |
Output is correct |
17 |
Correct |
322 ms |
5512 KB |
Output is correct |
18 |
Correct |
335 ms |
6968 KB |
Output is correct |
19 |
Correct |
217 ms |
6968 KB |
Output is correct |
20 |
Correct |
280 ms |
6968 KB |
Output is correct |
21 |
Correct |
372 ms |
6968 KB |
Output is correct |
22 |
Correct |
396 ms |
6968 KB |
Output is correct |
23 |
Correct |
348 ms |
6968 KB |
Output is correct |
24 |
Correct |
375 ms |
6968 KB |
Output is correct |
25 |
Correct |
427 ms |
7312 KB |
Output is correct |
26 |
Correct |
411 ms |
7312 KB |
Output is correct |
27 |
Correct |
359 ms |
7312 KB |
Output is correct |
28 |
Correct |
336 ms |
8528 KB |
Output is correct |
29 |
Correct |
309 ms |
8528 KB |
Output is correct |
30 |
Correct |
376 ms |
8528 KB |
Output is correct |
31 |
Correct |
356 ms |
8528 KB |
Output is correct |
32 |
Correct |
437 ms |
8528 KB |
Output is correct |
33 |
Correct |
350 ms |
8528 KB |
Output is correct |
34 |
Correct |
348 ms |
8528 KB |
Output is correct |
35 |
Correct |
359 ms |
8528 KB |
Output is correct |
36 |
Correct |
215 ms |
8528 KB |
Output is correct |
37 |
Correct |
298 ms |
8528 KB |
Output is correct |
38 |
Correct |
265 ms |
8528 KB |
Output is correct |
39 |
Correct |
357 ms |
8528 KB |
Output is correct |
40 |
Correct |
379 ms |
8528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
428 KB |
Output is correct |
4 |
Correct |
3 ms |
436 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
736 KB |
Output is correct |
8 |
Correct |
3 ms |
736 KB |
Output is correct |
9 |
Correct |
3 ms |
736 KB |
Output is correct |
10 |
Correct |
3 ms |
736 KB |
Output is correct |
11 |
Correct |
102 ms |
19828 KB |
Output is correct |
12 |
Correct |
114 ms |
19828 KB |
Output is correct |
13 |
Correct |
114 ms |
19828 KB |
Output is correct |
14 |
Correct |
165 ms |
19828 KB |
Output is correct |
15 |
Correct |
113 ms |
19828 KB |
Output is correct |
16 |
Correct |
178 ms |
19828 KB |
Output is correct |
17 |
Correct |
135 ms |
19828 KB |
Output is correct |
18 |
Correct |
93 ms |
20032 KB |
Output is correct |
19 |
Correct |
144 ms |
20032 KB |
Output is correct |
20 |
Correct |
99 ms |
20032 KB |
Output is correct |
21 |
Correct |
114 ms |
20032 KB |
Output is correct |
22 |
Correct |
158 ms |
20032 KB |
Output is correct |
23 |
Correct |
100 ms |
20032 KB |
Output is correct |
24 |
Correct |
137 ms |
20032 KB |
Output is correct |
25 |
Correct |
150 ms |
20032 KB |
Output is correct |
26 |
Correct |
91 ms |
20032 KB |
Output is correct |
27 |
Correct |
114 ms |
20032 KB |
Output is correct |
28 |
Correct |
100 ms |
20032 KB |
Output is correct |
29 |
Correct |
121 ms |
20032 KB |
Output is correct |
30 |
Correct |
111 ms |
20032 KB |
Output is correct |
31 |
Correct |
99 ms |
20032 KB |
Output is correct |
32 |
Correct |
123 ms |
20032 KB |
Output is correct |
33 |
Correct |
117 ms |
20032 KB |
Output is correct |
34 |
Correct |
79 ms |
20032 KB |
Output is correct |
35 |
Correct |
121 ms |
20032 KB |
Output is correct |
36 |
Correct |
134 ms |
20032 KB |
Output is correct |
37 |
Correct |
110 ms |
20032 KB |
Output is correct |
38 |
Correct |
126 ms |
20032 KB |
Output is correct |
39 |
Correct |
99 ms |
20032 KB |
Output is correct |
40 |
Correct |
111 ms |
20032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
428 KB |
Output is correct |
4 |
Correct |
3 ms |
436 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
736 KB |
Output is correct |
8 |
Correct |
3 ms |
736 KB |
Output is correct |
9 |
Correct |
3 ms |
736 KB |
Output is correct |
10 |
Correct |
3 ms |
736 KB |
Output is correct |
11 |
Correct |
260 ms |
4980 KB |
Output is correct |
12 |
Correct |
305 ms |
4980 KB |
Output is correct |
13 |
Correct |
258 ms |
4980 KB |
Output is correct |
14 |
Correct |
356 ms |
4980 KB |
Output is correct |
15 |
Correct |
269 ms |
5512 KB |
Output is correct |
16 |
Correct |
261 ms |
5512 KB |
Output is correct |
17 |
Correct |
322 ms |
5512 KB |
Output is correct |
18 |
Correct |
335 ms |
6968 KB |
Output is correct |
19 |
Correct |
217 ms |
6968 KB |
Output is correct |
20 |
Correct |
280 ms |
6968 KB |
Output is correct |
21 |
Correct |
372 ms |
6968 KB |
Output is correct |
22 |
Correct |
396 ms |
6968 KB |
Output is correct |
23 |
Correct |
348 ms |
6968 KB |
Output is correct |
24 |
Correct |
375 ms |
6968 KB |
Output is correct |
25 |
Correct |
427 ms |
7312 KB |
Output is correct |
26 |
Correct |
411 ms |
7312 KB |
Output is correct |
27 |
Correct |
359 ms |
7312 KB |
Output is correct |
28 |
Correct |
336 ms |
8528 KB |
Output is correct |
29 |
Correct |
309 ms |
8528 KB |
Output is correct |
30 |
Correct |
376 ms |
8528 KB |
Output is correct |
31 |
Correct |
356 ms |
8528 KB |
Output is correct |
32 |
Correct |
437 ms |
8528 KB |
Output is correct |
33 |
Correct |
350 ms |
8528 KB |
Output is correct |
34 |
Correct |
348 ms |
8528 KB |
Output is correct |
35 |
Correct |
359 ms |
8528 KB |
Output is correct |
36 |
Correct |
215 ms |
8528 KB |
Output is correct |
37 |
Correct |
298 ms |
8528 KB |
Output is correct |
38 |
Correct |
265 ms |
8528 KB |
Output is correct |
39 |
Correct |
357 ms |
8528 KB |
Output is correct |
40 |
Correct |
379 ms |
8528 KB |
Output is correct |
41 |
Correct |
102 ms |
19828 KB |
Output is correct |
42 |
Correct |
114 ms |
19828 KB |
Output is correct |
43 |
Correct |
114 ms |
19828 KB |
Output is correct |
44 |
Correct |
165 ms |
19828 KB |
Output is correct |
45 |
Correct |
113 ms |
19828 KB |
Output is correct |
46 |
Correct |
178 ms |
19828 KB |
Output is correct |
47 |
Correct |
135 ms |
19828 KB |
Output is correct |
48 |
Correct |
93 ms |
20032 KB |
Output is correct |
49 |
Correct |
144 ms |
20032 KB |
Output is correct |
50 |
Correct |
99 ms |
20032 KB |
Output is correct |
51 |
Correct |
114 ms |
20032 KB |
Output is correct |
52 |
Correct |
158 ms |
20032 KB |
Output is correct |
53 |
Correct |
100 ms |
20032 KB |
Output is correct |
54 |
Correct |
137 ms |
20032 KB |
Output is correct |
55 |
Correct |
150 ms |
20032 KB |
Output is correct |
56 |
Correct |
91 ms |
20032 KB |
Output is correct |
57 |
Correct |
114 ms |
20032 KB |
Output is correct |
58 |
Correct |
100 ms |
20032 KB |
Output is correct |
59 |
Correct |
121 ms |
20032 KB |
Output is correct |
60 |
Correct |
111 ms |
20032 KB |
Output is correct |
61 |
Correct |
99 ms |
20032 KB |
Output is correct |
62 |
Correct |
123 ms |
20032 KB |
Output is correct |
63 |
Correct |
117 ms |
20032 KB |
Output is correct |
64 |
Correct |
79 ms |
20032 KB |
Output is correct |
65 |
Correct |
121 ms |
20032 KB |
Output is correct |
66 |
Correct |
134 ms |
20032 KB |
Output is correct |
67 |
Correct |
110 ms |
20032 KB |
Output is correct |
68 |
Correct |
126 ms |
20032 KB |
Output is correct |
69 |
Correct |
99 ms |
20032 KB |
Output is correct |
70 |
Correct |
111 ms |
20032 KB |
Output is correct |
71 |
Correct |
676 ms |
24560 KB |
Output is correct |
72 |
Correct |
746 ms |
24608 KB |
Output is correct |
73 |
Correct |
1016 ms |
24608 KB |
Output is correct |
74 |
Correct |
1541 ms |
24608 KB |
Output is correct |
75 |
Correct |
783 ms |
26148 KB |
Output is correct |
76 |
Correct |
1688 ms |
26148 KB |
Output is correct |
77 |
Correct |
1399 ms |
26148 KB |
Output is correct |
78 |
Correct |
519 ms |
28276 KB |
Output is correct |
79 |
Correct |
665 ms |
28276 KB |
Output is correct |
80 |
Correct |
771 ms |
28276 KB |
Output is correct |
81 |
Correct |
1108 ms |
28276 KB |
Output is correct |
82 |
Correct |
1681 ms |
44040 KB |
Output is correct |
83 |
Correct |
722 ms |
44040 KB |
Output is correct |
84 |
Correct |
1424 ms |
44040 KB |
Output is correct |
85 |
Correct |
1397 ms |
65484 KB |
Output is correct |
86 |
Correct |
388 ms |
65484 KB |
Output is correct |
87 |
Runtime error |
805 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. |
88 |
Halted |
0 ms |
0 KB |
- |