#include "bits/stdc++.h"
#define F first
#define S second
#define ll long long
#define pii pair<ll,int>
const int mxN = (1 << 20) + 5;
const int mod = 1e9 + 7;
using namespace std;
int sos[mxN][2][2];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,q;
cin >>n>>q;
string s;
cin>>s;
for(int i = 0;i < n;i++){
for(int mask = 0;mask < (1 << n);mask++){
if(!i){
sos[mask][0][0] = s[mask] - '0';
if(mask % 2) sos[mask][0][0] += s[mask - 1] - '0';
sos[mask][0][1] = s[mask] - '0';
if(mask % 2 == 0) sos[mask][0][1] += s[mask + 1] - '0';
continue;
}
sos[mask][i % 2][0] = sos[mask][(i - 1)%2][0];
sos[mask][i % 2][1] = sos[mask][(i - 1)%2][1];
if(mask >> i & 1) sos[mask][(i)%2][0] += sos[mask - (1 << i)][(i - 1)%2][0];
else sos[mask][i % 2][1] += sos[mask + (1 << i)][(i - 1)%2][1];
}
}
while(q--){
string a;
cin >>a;
reverse(a.begin(),a.end());
int Qcnt = 0, Ocnt = 0, Icnt = 0;
for(auto x : a){
if(x == '?') Qcnt++;
else if(x == '1') Icnt++;
else Ocnt++;
}
int ans = 0;
if(min({Qcnt,Ocnt,Icnt}) == Qcnt){
// printf("? small\n");
for(int mask = 0;mask < (1 << Qcnt);mask++){
int cnt = 0;
int num = 0;
int i = 0;
for(auto x : a){
if(x == '?') {
if(mask >> cnt & 1) num += (1 << i);
cnt++;
}
if(x == '1') num += (1 << i);
i++;
}
ans += s[num] - '0';
}
}else if(min({Qcnt,Ocnt,Icnt}) == Icnt){
// printf("1 small\n");
for(int mask = 0; mask < (1 << Icnt);mask++){
int cnt = 0;
int num = 0;
int i = 0;
for(auto x : a){
if(x == '1') {
if(mask >> cnt & 1) num += (1 << i);
cnt++;
}
if(x == '?') num += (1 << i);
i++;
}
if(Icnt % 2 == __builtin_popcount(mask) % 2) ans += sos[num][(n - 1)% 2][0];
else ans -= sos[num][(n - 1)% 2][0];
}
}else{
for(int mask = 0; mask < (1 << Ocnt);mask++){
// printf("0 small\n");
int cnt = 0;
int num = 0;
int i = 0;
for(auto x : a){
if(x == '0') {
if(mask >> cnt & 1) num += (1 << i);
cnt++;
}
if(x == '1') num += (1 << i);
// if(x == '?') num += (1 << i);
i++;
}
if(0 == __builtin_popcount(mask) % 2) ans += sos[num][(n - 1)%2][1];
else ans -= sos[num][(n - 1)%2][1];
}
}
cout<<ans<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
145 ms |
4820 KB |
Output is correct |
12 |
Correct |
161 ms |
4004 KB |
Output is correct |
13 |
Correct |
196 ms |
3400 KB |
Output is correct |
14 |
Correct |
267 ms |
3260 KB |
Output is correct |
15 |
Correct |
234 ms |
7248 KB |
Output is correct |
16 |
Correct |
331 ms |
6536 KB |
Output is correct |
17 |
Correct |
296 ms |
3400 KB |
Output is correct |
18 |
Correct |
133 ms |
7472 KB |
Output is correct |
19 |
Correct |
145 ms |
2376 KB |
Output is correct |
20 |
Correct |
175 ms |
6728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
145 ms |
4820 KB |
Output is correct |
12 |
Correct |
161 ms |
4004 KB |
Output is correct |
13 |
Correct |
196 ms |
3400 KB |
Output is correct |
14 |
Correct |
267 ms |
3260 KB |
Output is correct |
15 |
Correct |
234 ms |
7248 KB |
Output is correct |
16 |
Correct |
331 ms |
6536 KB |
Output is correct |
17 |
Correct |
296 ms |
3400 KB |
Output is correct |
18 |
Correct |
133 ms |
7472 KB |
Output is correct |
19 |
Correct |
145 ms |
2376 KB |
Output is correct |
20 |
Correct |
175 ms |
6728 KB |
Output is correct |
21 |
Correct |
180 ms |
4428 KB |
Output is correct |
22 |
Correct |
198 ms |
4680 KB |
Output is correct |
23 |
Correct |
450 ms |
4300 KB |
Output is correct |
24 |
Correct |
970 ms |
8864 KB |
Output is correct |
25 |
Correct |
262 ms |
9692 KB |
Output is correct |
26 |
Correct |
764 ms |
9804 KB |
Output is correct |
27 |
Correct |
556 ms |
3912 KB |
Output is correct |
28 |
Correct |
134 ms |
7492 KB |
Output is correct |
29 |
Correct |
157 ms |
2376 KB |
Output is correct |
30 |
Correct |
181 ms |
6688 KB |
Output is correct |
31 |
Correct |
452 ms |
4412 KB |
Output is correct |
32 |
Correct |
1018 ms |
4584 KB |
Output is correct |
33 |
Correct |
239 ms |
3404 KB |
Output is correct |
34 |
Correct |
739 ms |
4580 KB |
Output is correct |
35 |
Correct |
578 ms |
4504 KB |
Output is correct |
36 |
Correct |
155 ms |
6728 KB |
Output is correct |
37 |
Correct |
181 ms |
9288 KB |
Output is correct |
38 |
Correct |
178 ms |
2376 KB |
Output is correct |
39 |
Correct |
421 ms |
3720 KB |
Output is correct |
40 |
Correct |
946 ms |
8476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
63 ms |
18156 KB |
Output is correct |
12 |
Correct |
56 ms |
18028 KB |
Output is correct |
13 |
Correct |
103 ms |
18152 KB |
Output is correct |
14 |
Correct |
267 ms |
18176 KB |
Output is correct |
15 |
Correct |
88 ms |
18076 KB |
Output is correct |
16 |
Correct |
198 ms |
19436 KB |
Output is correct |
17 |
Correct |
149 ms |
18172 KB |
Output is correct |
18 |
Correct |
49 ms |
18280 KB |
Output is correct |
19 |
Correct |
57 ms |
17908 KB |
Output is correct |
20 |
Correct |
55 ms |
18232 KB |
Output is correct |
21 |
Correct |
125 ms |
18152 KB |
Output is correct |
22 |
Correct |
272 ms |
18152 KB |
Output is correct |
23 |
Correct |
77 ms |
18152 KB |
Output is correct |
24 |
Correct |
193 ms |
18156 KB |
Output is correct |
25 |
Correct |
152 ms |
18152 KB |
Output is correct |
26 |
Correct |
52 ms |
17908 KB |
Output is correct |
27 |
Correct |
57 ms |
18152 KB |
Output is correct |
28 |
Correct |
60 ms |
19196 KB |
Output is correct |
29 |
Correct |
110 ms |
19448 KB |
Output is correct |
30 |
Correct |
227 ms |
19872 KB |
Output is correct |
31 |
Correct |
75 ms |
19432 KB |
Output is correct |
32 |
Correct |
237 ms |
19432 KB |
Output is correct |
33 |
Correct |
172 ms |
19440 KB |
Output is correct |
34 |
Correct |
52 ms |
18024 KB |
Output is correct |
35 |
Correct |
116 ms |
18152 KB |
Output is correct |
36 |
Correct |
123 ms |
18152 KB |
Output is correct |
37 |
Correct |
119 ms |
19432 KB |
Output is correct |
38 |
Correct |
129 ms |
18152 KB |
Output is correct |
39 |
Correct |
120 ms |
18028 KB |
Output is correct |
40 |
Correct |
128 ms |
18020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
145 ms |
4820 KB |
Output is correct |
12 |
Correct |
161 ms |
4004 KB |
Output is correct |
13 |
Correct |
196 ms |
3400 KB |
Output is correct |
14 |
Correct |
267 ms |
3260 KB |
Output is correct |
15 |
Correct |
234 ms |
7248 KB |
Output is correct |
16 |
Correct |
331 ms |
6536 KB |
Output is correct |
17 |
Correct |
296 ms |
3400 KB |
Output is correct |
18 |
Correct |
133 ms |
7472 KB |
Output is correct |
19 |
Correct |
145 ms |
2376 KB |
Output is correct |
20 |
Correct |
175 ms |
6728 KB |
Output is correct |
21 |
Correct |
180 ms |
4428 KB |
Output is correct |
22 |
Correct |
198 ms |
4680 KB |
Output is correct |
23 |
Correct |
450 ms |
4300 KB |
Output is correct |
24 |
Correct |
970 ms |
8864 KB |
Output is correct |
25 |
Correct |
262 ms |
9692 KB |
Output is correct |
26 |
Correct |
764 ms |
9804 KB |
Output is correct |
27 |
Correct |
556 ms |
3912 KB |
Output is correct |
28 |
Correct |
134 ms |
7492 KB |
Output is correct |
29 |
Correct |
157 ms |
2376 KB |
Output is correct |
30 |
Correct |
181 ms |
6688 KB |
Output is correct |
31 |
Correct |
452 ms |
4412 KB |
Output is correct |
32 |
Correct |
1018 ms |
4584 KB |
Output is correct |
33 |
Correct |
239 ms |
3404 KB |
Output is correct |
34 |
Correct |
739 ms |
4580 KB |
Output is correct |
35 |
Correct |
578 ms |
4504 KB |
Output is correct |
36 |
Correct |
155 ms |
6728 KB |
Output is correct |
37 |
Correct |
181 ms |
9288 KB |
Output is correct |
38 |
Correct |
178 ms |
2376 KB |
Output is correct |
39 |
Correct |
421 ms |
3720 KB |
Output is correct |
40 |
Correct |
946 ms |
8476 KB |
Output is correct |
41 |
Correct |
63 ms |
18156 KB |
Output is correct |
42 |
Correct |
56 ms |
18028 KB |
Output is correct |
43 |
Correct |
103 ms |
18152 KB |
Output is correct |
44 |
Correct |
267 ms |
18176 KB |
Output is correct |
45 |
Correct |
88 ms |
18076 KB |
Output is correct |
46 |
Correct |
198 ms |
19436 KB |
Output is correct |
47 |
Correct |
149 ms |
18172 KB |
Output is correct |
48 |
Correct |
49 ms |
18280 KB |
Output is correct |
49 |
Correct |
57 ms |
17908 KB |
Output is correct |
50 |
Correct |
55 ms |
18232 KB |
Output is correct |
51 |
Correct |
125 ms |
18152 KB |
Output is correct |
52 |
Correct |
272 ms |
18152 KB |
Output is correct |
53 |
Correct |
77 ms |
18152 KB |
Output is correct |
54 |
Correct |
193 ms |
18156 KB |
Output is correct |
55 |
Correct |
152 ms |
18152 KB |
Output is correct |
56 |
Correct |
52 ms |
17908 KB |
Output is correct |
57 |
Correct |
57 ms |
18152 KB |
Output is correct |
58 |
Correct |
60 ms |
19196 KB |
Output is correct |
59 |
Correct |
110 ms |
19448 KB |
Output is correct |
60 |
Correct |
227 ms |
19872 KB |
Output is correct |
61 |
Correct |
75 ms |
19432 KB |
Output is correct |
62 |
Correct |
237 ms |
19432 KB |
Output is correct |
63 |
Correct |
172 ms |
19440 KB |
Output is correct |
64 |
Correct |
52 ms |
18024 KB |
Output is correct |
65 |
Correct |
116 ms |
18152 KB |
Output is correct |
66 |
Correct |
123 ms |
18152 KB |
Output is correct |
67 |
Correct |
119 ms |
19432 KB |
Output is correct |
68 |
Correct |
129 ms |
18152 KB |
Output is correct |
69 |
Correct |
120 ms |
18028 KB |
Output is correct |
70 |
Correct |
128 ms |
18020 KB |
Output is correct |
71 |
Correct |
319 ms |
23016 KB |
Output is correct |
72 |
Correct |
367 ms |
23136 KB |
Output is correct |
73 |
Correct |
1377 ms |
21668 KB |
Output is correct |
74 |
Execution timed out |
2077 ms |
21512 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |