#pragma GCC optimize "O3"
#pragma GCC optimize "unroll-loops"
#pragma GCC target "sse,sse2,sse3,sse4,abm,avx,mmx,popcnt,tune=native"
#include <bits/stdc++.h>
using namespace std;
int N, L, Q;
short arr[1<<20];
int pre[2][1<<20];
short cnt[3];
int msk, t;
int ret;
vector<short> v;
vector<short> k;
/*
void rec(int n){
if(n == -1){
if(2 == t){
ret += arr[msk];
}
else{
const short c = ((__builtin_popcount(msk)&1)<<1)-1;
ret += pre[1-t][msk]*c;
}
return;
}
msk <<= 1;
if(v[n] == 2){
rec(n-1);
msk |= 1;
rec(n-1);
}
else{
msk |= v[n];
rec(n-1);
}
msk >>= 1;
}
*/
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> L >> Q;
N = 1<<L;
for(int m = 0; m<N; m++){
char c;
cin >> c;
arr[m] = c-'0';
pre[0][m] = arr[m];
}
for(int m = 0; m<N; m++){
pre[1][m] = arr[m^(N-1)];
}
for(int d = 0; d<L; d++){
for(int m = N-1; m>=0; m--){
if((1<<d)&m){
pre[0][m] += pre[0][m^(1<<d)];
pre[1][m] += pre[1][m^(1<<d)];
}
}
}
while(Q--){
cnt[0] = cnt[1] = cnt[2] = 0;
for(int k = 0; k<L; k++){
char c;
cin >> c;
if(c == '?'){
v.emplace_back(2);
}
else{
v.emplace_back(c-'0');
}
cnt[v.back()]++;
}
ret = 0;
if(cnt[2] <= cnt[0] && cnt[2] <= cnt[1]){
t = 2;
}
else if(cnt[1] < cnt[0]){
for(int k = 0; k<L; k++){
if(v[k] == 2){
v[k] = 1;
}
else if(v[k] == 1){
v[k] = 2;
}
else{
v[k] = 0;
}
}
t = 1;
}
else{
for(int k = 0; k<L; k++){
if(v[k] == 2){
v[k] = 1;
}
else if(v[k] == 1){
v[k] = 0;
}
else{
v[k] = 2;
}
}
t = 0;
}
for(int b = 0; b<L; b++){
//cout << v[b];
if(v[b] == 2){
k.emplace_back(L-b-1);
}
else if(v[b] == 1){
msk |= (1<<(L-b-1));
}
}
/*
cout << " ";
for(int n : k){
cout << n << " ";
}
*/
const int S = 1<<k.size();
for(int m = 0; m<S; m++){
int temp = 0;
for(int n = 0; n<k.size(); n++){
if(m>>n&1){
temp |= (1<<k[n]);
}
}
if(t == 2){
ret += arr[temp|msk];
}
else{
ret += pre[1-t][msk|temp]*(((__builtin_popcount(msk|temp)&1)<<1)-1);
}
}
msk = 0;
//cout << t << " ";
cout << abs(ret) << "\n";
k.clear();
v.clear();
}
}
/*
#include <bits/stdc++.h>
using namespace std;
int N, L, Q, T;
int arr[1<<20];
int dp[60000];
vector<char> ternary[60000];
int pows[20];
int pre[1000005];
vector<char> qu[1000005];
int ans[1000005];
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> L >> Q;
N = 1<<L;
for(int m = 0; m<N; m++){
char c;
cin >> c;
arr[m] = c-'0';
}
int H = L/2;
T = 1;
pows[0] = 1;
for(int k = 0; k<H; k++){
T *= 3;
pows[k+1] = pows[k] * 3;
}
for(int m = 0; m<T; m++){
int temp = m;
for(int k = 0; k<H; k++){
ternary[m].emplace_back(temp%3);
temp /= 3;
}
}
for(int q = 1; q<=Q; q++){
int t = 0;
for(int i = 0; i<L; i++){
char c;
cin >> c;
if(c == '?'){
qu[q].emplace_back(2);
}
else{
qu[q].emplace_back(c-'0');
}
}
for(int k = 0; k<H; k++){
t *= 3;
t += qu[q][k];
}
reverse(qu[q].begin(), qu[q].end());
pre[q] = t;
}
for(int m = 0; m<1<<(L-H); m++){
fill(dp, dp+T, 0);
for(int t = 0; t<T; t++){
bool has2 = 0;
int temp = 0;
for(int h = 0; h<H; h++){
if(ternary[t][h] == 2){
has2 = 1;
dp[t] = dp[t-pows[h]] + dp[t-pows[h]*2];
break;
}
temp += (1<<h)*ternary[t][h];
}
if(!has2){
dp[t] = arr[(temp<<(L-H)) + m];
}
}
for(int q = 1; q<=Q; q++){
bool good = 1;
for(int b = 0; b<L-H; b++){
if(qu[q][b] == 2 || qu[q][b] == ((m>>b)&1)){
}
else{
good = 0;
break;
}
}
if(good){
ans[q] += dp[pre[q]];
}
}
}
for(int q = 1; q<=Q; q++){
cout << ans[q] << "\n";
}
}
*/
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:130:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int n = 0; n<k.size(); n++){
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
423 ms |
4472 KB |
Output is correct |
12 |
Correct |
456 ms |
4088 KB |
Output is correct |
13 |
Correct |
461 ms |
3320 KB |
Output is correct |
14 |
Correct |
508 ms |
3448 KB |
Output is correct |
15 |
Correct |
454 ms |
4472 KB |
Output is correct |
16 |
Correct |
512 ms |
3576 KB |
Output is correct |
17 |
Correct |
509 ms |
3576 KB |
Output is correct |
18 |
Correct |
310 ms |
5368 KB |
Output is correct |
19 |
Correct |
366 ms |
2424 KB |
Output is correct |
20 |
Correct |
452 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
423 ms |
4472 KB |
Output is correct |
12 |
Correct |
456 ms |
4088 KB |
Output is correct |
13 |
Correct |
461 ms |
3320 KB |
Output is correct |
14 |
Correct |
508 ms |
3448 KB |
Output is correct |
15 |
Correct |
454 ms |
4472 KB |
Output is correct |
16 |
Correct |
512 ms |
3576 KB |
Output is correct |
17 |
Correct |
509 ms |
3576 KB |
Output is correct |
18 |
Correct |
310 ms |
5368 KB |
Output is correct |
19 |
Correct |
366 ms |
2424 KB |
Output is correct |
20 |
Correct |
452 ms |
4088 KB |
Output is correct |
21 |
Correct |
561 ms |
4472 KB |
Output is correct |
22 |
Correct |
602 ms |
4600 KB |
Output is correct |
23 |
Correct |
607 ms |
3704 KB |
Output is correct |
24 |
Correct |
619 ms |
3448 KB |
Output is correct |
25 |
Correct |
588 ms |
5496 KB |
Output is correct |
26 |
Correct |
670 ms |
3960 KB |
Output is correct |
27 |
Correct |
665 ms |
3960 KB |
Output is correct |
28 |
Correct |
375 ms |
6520 KB |
Output is correct |
29 |
Correct |
452 ms |
2424 KB |
Output is correct |
30 |
Correct |
605 ms |
4600 KB |
Output is correct |
31 |
Correct |
674 ms |
4476 KB |
Output is correct |
32 |
Correct |
725 ms |
4484 KB |
Output is correct |
33 |
Correct |
550 ms |
3320 KB |
Output is correct |
34 |
Correct |
654 ms |
3448 KB |
Output is correct |
35 |
Correct |
676 ms |
3960 KB |
Output is correct |
36 |
Correct |
388 ms |
2424 KB |
Output is correct |
37 |
Correct |
583 ms |
4472 KB |
Output is correct |
38 |
Correct |
470 ms |
2552 KB |
Output is correct |
39 |
Correct |
613 ms |
3708 KB |
Output is correct |
40 |
Correct |
628 ms |
3448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
93 ms |
10872 KB |
Output is correct |
12 |
Correct |
94 ms |
10908 KB |
Output is correct |
13 |
Correct |
105 ms |
10744 KB |
Output is correct |
14 |
Correct |
124 ms |
10872 KB |
Output is correct |
15 |
Correct |
100 ms |
11000 KB |
Output is correct |
16 |
Correct |
142 ms |
10872 KB |
Output is correct |
17 |
Correct |
124 ms |
10872 KB |
Output is correct |
18 |
Correct |
69 ms |
11000 KB |
Output is correct |
19 |
Correct |
82 ms |
10744 KB |
Output is correct |
20 |
Correct |
96 ms |
10872 KB |
Output is correct |
21 |
Correct |
104 ms |
10872 KB |
Output is correct |
22 |
Correct |
123 ms |
10872 KB |
Output is correct |
23 |
Correct |
87 ms |
10744 KB |
Output is correct |
24 |
Correct |
119 ms |
11000 KB |
Output is correct |
25 |
Correct |
120 ms |
10872 KB |
Output is correct |
26 |
Correct |
73 ms |
10744 KB |
Output is correct |
27 |
Correct |
95 ms |
10876 KB |
Output is correct |
28 |
Correct |
80 ms |
10744 KB |
Output is correct |
29 |
Correct |
104 ms |
10744 KB |
Output is correct |
30 |
Correct |
111 ms |
10872 KB |
Output is correct |
31 |
Correct |
86 ms |
10744 KB |
Output is correct |
32 |
Correct |
140 ms |
10872 KB |
Output is correct |
33 |
Correct |
127 ms |
10872 KB |
Output is correct |
34 |
Correct |
75 ms |
10744 KB |
Output is correct |
35 |
Correct |
109 ms |
10872 KB |
Output is correct |
36 |
Correct |
108 ms |
10872 KB |
Output is correct |
37 |
Correct |
113 ms |
10872 KB |
Output is correct |
38 |
Correct |
117 ms |
10876 KB |
Output is correct |
39 |
Correct |
124 ms |
10872 KB |
Output is correct |
40 |
Correct |
113 ms |
10872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
423 ms |
4472 KB |
Output is correct |
12 |
Correct |
456 ms |
4088 KB |
Output is correct |
13 |
Correct |
461 ms |
3320 KB |
Output is correct |
14 |
Correct |
508 ms |
3448 KB |
Output is correct |
15 |
Correct |
454 ms |
4472 KB |
Output is correct |
16 |
Correct |
512 ms |
3576 KB |
Output is correct |
17 |
Correct |
509 ms |
3576 KB |
Output is correct |
18 |
Correct |
310 ms |
5368 KB |
Output is correct |
19 |
Correct |
366 ms |
2424 KB |
Output is correct |
20 |
Correct |
452 ms |
4088 KB |
Output is correct |
21 |
Correct |
561 ms |
4472 KB |
Output is correct |
22 |
Correct |
602 ms |
4600 KB |
Output is correct |
23 |
Correct |
607 ms |
3704 KB |
Output is correct |
24 |
Correct |
619 ms |
3448 KB |
Output is correct |
25 |
Correct |
588 ms |
5496 KB |
Output is correct |
26 |
Correct |
670 ms |
3960 KB |
Output is correct |
27 |
Correct |
665 ms |
3960 KB |
Output is correct |
28 |
Correct |
375 ms |
6520 KB |
Output is correct |
29 |
Correct |
452 ms |
2424 KB |
Output is correct |
30 |
Correct |
605 ms |
4600 KB |
Output is correct |
31 |
Correct |
674 ms |
4476 KB |
Output is correct |
32 |
Correct |
725 ms |
4484 KB |
Output is correct |
33 |
Correct |
550 ms |
3320 KB |
Output is correct |
34 |
Correct |
654 ms |
3448 KB |
Output is correct |
35 |
Correct |
676 ms |
3960 KB |
Output is correct |
36 |
Correct |
388 ms |
2424 KB |
Output is correct |
37 |
Correct |
583 ms |
4472 KB |
Output is correct |
38 |
Correct |
470 ms |
2552 KB |
Output is correct |
39 |
Correct |
613 ms |
3708 KB |
Output is correct |
40 |
Correct |
628 ms |
3448 KB |
Output is correct |
41 |
Correct |
93 ms |
10872 KB |
Output is correct |
42 |
Correct |
94 ms |
10908 KB |
Output is correct |
43 |
Correct |
105 ms |
10744 KB |
Output is correct |
44 |
Correct |
124 ms |
10872 KB |
Output is correct |
45 |
Correct |
100 ms |
11000 KB |
Output is correct |
46 |
Correct |
142 ms |
10872 KB |
Output is correct |
47 |
Correct |
124 ms |
10872 KB |
Output is correct |
48 |
Correct |
69 ms |
11000 KB |
Output is correct |
49 |
Correct |
82 ms |
10744 KB |
Output is correct |
50 |
Correct |
96 ms |
10872 KB |
Output is correct |
51 |
Correct |
104 ms |
10872 KB |
Output is correct |
52 |
Correct |
123 ms |
10872 KB |
Output is correct |
53 |
Correct |
87 ms |
10744 KB |
Output is correct |
54 |
Correct |
119 ms |
11000 KB |
Output is correct |
55 |
Correct |
120 ms |
10872 KB |
Output is correct |
56 |
Correct |
73 ms |
10744 KB |
Output is correct |
57 |
Correct |
95 ms |
10876 KB |
Output is correct |
58 |
Correct |
80 ms |
10744 KB |
Output is correct |
59 |
Correct |
104 ms |
10744 KB |
Output is correct |
60 |
Correct |
111 ms |
10872 KB |
Output is correct |
61 |
Correct |
86 ms |
10744 KB |
Output is correct |
62 |
Correct |
140 ms |
10872 KB |
Output is correct |
63 |
Correct |
127 ms |
10872 KB |
Output is correct |
64 |
Correct |
75 ms |
10744 KB |
Output is correct |
65 |
Correct |
109 ms |
10872 KB |
Output is correct |
66 |
Correct |
108 ms |
10872 KB |
Output is correct |
67 |
Correct |
113 ms |
10872 KB |
Output is correct |
68 |
Correct |
117 ms |
10876 KB |
Output is correct |
69 |
Correct |
124 ms |
10872 KB |
Output is correct |
70 |
Correct |
113 ms |
10872 KB |
Output is correct |
71 |
Correct |
973 ms |
15608 KB |
Output is correct |
72 |
Correct |
1010 ms |
15736 KB |
Output is correct |
73 |
Correct |
1075 ms |
14328 KB |
Output is correct |
74 |
Correct |
1553 ms |
22520 KB |
Output is correct |
75 |
Correct |
986 ms |
38264 KB |
Output is correct |
76 |
Correct |
1593 ms |
36472 KB |
Output is correct |
77 |
Correct |
1493 ms |
36404 KB |
Output is correct |
78 |
Correct |
543 ms |
40188 KB |
Output is correct |
79 |
Correct |
708 ms |
34168 KB |
Output is correct |
80 |
Correct |
974 ms |
37372 KB |
Output is correct |
81 |
Correct |
1287 ms |
37240 KB |
Output is correct |
82 |
Correct |
1568 ms |
36216 KB |
Output is correct |
83 |
Correct |
888 ms |
35448 KB |
Output is correct |
84 |
Correct |
1552 ms |
36216 KB |
Output is correct |
85 |
Correct |
1495 ms |
36376 KB |
Output is correct |
86 |
Correct |
594 ms |
34168 KB |
Output is correct |
87 |
Correct |
986 ms |
37252 KB |
Output is correct |
88 |
Correct |
761 ms |
34232 KB |
Output is correct |
89 |
Correct |
1100 ms |
35832 KB |
Output is correct |
90 |
Correct |
1373 ms |
36180 KB |
Output is correct |
91 |
Correct |
873 ms |
35448 KB |
Output is correct |
92 |
Correct |
1884 ms |
36472 KB |
Output is correct |
93 |
Correct |
1551 ms |
36472 KB |
Output is correct |
94 |
Correct |
602 ms |
34168 KB |
Output is correct |
95 |
Correct |
1299 ms |
36344 KB |
Output is correct |
96 |
Correct |
1328 ms |
36344 KB |
Output is correct |
97 |
Correct |
1322 ms |
36344 KB |
Output is correct |
98 |
Correct |
1283 ms |
36344 KB |
Output is correct |
99 |
Correct |
1371 ms |
36336 KB |
Output is correct |
100 |
Correct |
1350 ms |
36344 KB |
Output is correct |