#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
int n, q, a[1 << 20], S[1 << 20], T[1 << 20], cnt[2], ans, p;
string s;
void f(int i, int op, int st){
if(i == n){
if(!op) ans += a[st];
else if(op == 1){
if(p & 1) ans -= S[st];
else ans += S[st];
}
else{
if(p & 1) ans -= T[st];
else ans += T[st];
//cout << i << ' ' << st << ' ' << T[st] << ' ' << ans << '\n';
}
return;
}
if(!op){
if(s[i] == '?') {
f(i + 1, op, st);
f(i + 1, op, st | 1 << i);
}
else {
if(s[i] == '1') st |= 1 << i;
f(i + 1, op, st);
}
}
else if(op == 1){
if(s[i] == '1'){
f(i + 1, op, st | 1 << i);
p++;
f(i + 1, op, st);
p--;
}
else{
if(s[i] == '?') st |= 1 << i;
f(i + 1, op, st);
}
}
else{
if(s[i] == '0'){
f(i + 1, op, st);
p++;
f(i + 1, op, st | 1 << i);
p--;
}
else{
if(s[i] == '1') st |= 1 << i;
f(i + 1, op, st);
}
}
return;
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q >> s;
for(int i = 0; i < 1 << n; i++) {
a[i] = s[i] - '0';
S[i] = T[i] = a[i];
}
for(int i = 0; i < n; i++)
for(int mask = 0; mask < 1 << n; mask++) {
if(mask & (1 << i)) S[mask] += S[mask ^ (1 << i)];
if(!(mask & (1 << i))) T[mask] += T[mask ^ (1 << i)];
}
while(q--){
cin >> s;
reverse(all(s));
cnt[0] = cnt[1] = 0;
for(int i = 0; i < n; i++){
if(s[i] == '?') cnt[0]++;
else if(s[i] == '0') cnt[1]++;
}
ans = 0;
if(cnt[0] <= n / 3) f(0, 0, 0);
else if(cnt[1] <= n / 3) f(0, 1, 0);
else f(0, 2, 0);
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
514 ms |
15064 KB |
Output is correct |
12 |
Correct |
468 ms |
14756 KB |
Output is correct |
13 |
Correct |
309 ms |
14008 KB |
Output is correct |
14 |
Correct |
275 ms |
14004 KB |
Output is correct |
15 |
Correct |
276 ms |
15088 KB |
Output is correct |
16 |
Correct |
284 ms |
14208 KB |
Output is correct |
17 |
Correct |
343 ms |
14196 KB |
Output is correct |
18 |
Correct |
137 ms |
16088 KB |
Output is correct |
19 |
Correct |
185 ms |
13040 KB |
Output is correct |
20 |
Correct |
500 ms |
14700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
514 ms |
15064 KB |
Output is correct |
12 |
Correct |
468 ms |
14756 KB |
Output is correct |
13 |
Correct |
309 ms |
14008 KB |
Output is correct |
14 |
Correct |
275 ms |
14004 KB |
Output is correct |
15 |
Correct |
276 ms |
15088 KB |
Output is correct |
16 |
Correct |
284 ms |
14208 KB |
Output is correct |
17 |
Correct |
343 ms |
14196 KB |
Output is correct |
18 |
Correct |
137 ms |
16088 KB |
Output is correct |
19 |
Correct |
185 ms |
13040 KB |
Output is correct |
20 |
Correct |
500 ms |
14700 KB |
Output is correct |
21 |
Correct |
978 ms |
18112 KB |
Output is correct |
22 |
Correct |
1089 ms |
18336 KB |
Output is correct |
23 |
Correct |
509 ms |
17324 KB |
Output is correct |
24 |
Correct |
490 ms |
17168 KB |
Output is correct |
25 |
Correct |
355 ms |
19060 KB |
Output is correct |
26 |
Correct |
463 ms |
17648 KB |
Output is correct |
27 |
Correct |
526 ms |
17600 KB |
Output is correct |
28 |
Correct |
198 ms |
20180 KB |
Output is correct |
29 |
Correct |
217 ms |
16076 KB |
Output is correct |
30 |
Correct |
1166 ms |
18312 KB |
Output is correct |
31 |
Correct |
632 ms |
18052 KB |
Output is correct |
32 |
Correct |
521 ms |
18172 KB |
Output is correct |
33 |
Correct |
388 ms |
17112 KB |
Output is correct |
34 |
Correct |
452 ms |
17172 KB |
Output is correct |
35 |
Correct |
545 ms |
17540 KB |
Output is correct |
36 |
Correct |
174 ms |
16140 KB |
Output is correct |
37 |
Correct |
1411 ms |
18100 KB |
Output is correct |
38 |
Correct |
193 ms |
16060 KB |
Output is correct |
39 |
Correct |
540 ms |
17320 KB |
Output is correct |
40 |
Correct |
452 ms |
17076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
587 ms |
16068 KB |
Output is correct |
12 |
Correct |
782 ms |
16020 KB |
Output is correct |
13 |
Correct |
140 ms |
15920 KB |
Output is correct |
14 |
Correct |
116 ms |
15920 KB |
Output is correct |
15 |
Correct |
55 ms |
16104 KB |
Output is correct |
16 |
Correct |
90 ms |
16040 KB |
Output is correct |
17 |
Correct |
143 ms |
15920 KB |
Output is correct |
18 |
Correct |
33 ms |
16112 KB |
Output is correct |
19 |
Correct |
38 ms |
15812 KB |
Output is correct |
20 |
Correct |
831 ms |
15980 KB |
Output is correct |
21 |
Correct |
116 ms |
16016 KB |
Output is correct |
22 |
Correct |
121 ms |
15936 KB |
Output is correct |
23 |
Correct |
84 ms |
15948 KB |
Output is correct |
24 |
Correct |
80 ms |
15924 KB |
Output is correct |
25 |
Correct |
156 ms |
16032 KB |
Output is correct |
26 |
Correct |
35 ms |
15812 KB |
Output is correct |
27 |
Correct |
487 ms |
16020 KB |
Output is correct |
28 |
Correct |
41 ms |
15816 KB |
Output is correct |
29 |
Correct |
144 ms |
15936 KB |
Output is correct |
30 |
Correct |
86 ms |
15940 KB |
Output is correct |
31 |
Correct |
74 ms |
15952 KB |
Output is correct |
32 |
Correct |
100 ms |
15940 KB |
Output is correct |
33 |
Correct |
171 ms |
15920 KB |
Output is correct |
34 |
Correct |
35 ms |
15856 KB |
Output is correct |
35 |
Correct |
211 ms |
15916 KB |
Output is correct |
36 |
Correct |
225 ms |
15980 KB |
Output is correct |
37 |
Correct |
241 ms |
15952 KB |
Output is correct |
38 |
Correct |
207 ms |
15916 KB |
Output is correct |
39 |
Correct |
205 ms |
15976 KB |
Output is correct |
40 |
Correct |
206 ms |
15964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
514 ms |
15064 KB |
Output is correct |
12 |
Correct |
468 ms |
14756 KB |
Output is correct |
13 |
Correct |
309 ms |
14008 KB |
Output is correct |
14 |
Correct |
275 ms |
14004 KB |
Output is correct |
15 |
Correct |
276 ms |
15088 KB |
Output is correct |
16 |
Correct |
284 ms |
14208 KB |
Output is correct |
17 |
Correct |
343 ms |
14196 KB |
Output is correct |
18 |
Correct |
137 ms |
16088 KB |
Output is correct |
19 |
Correct |
185 ms |
13040 KB |
Output is correct |
20 |
Correct |
500 ms |
14700 KB |
Output is correct |
21 |
Correct |
978 ms |
18112 KB |
Output is correct |
22 |
Correct |
1089 ms |
18336 KB |
Output is correct |
23 |
Correct |
509 ms |
17324 KB |
Output is correct |
24 |
Correct |
490 ms |
17168 KB |
Output is correct |
25 |
Correct |
355 ms |
19060 KB |
Output is correct |
26 |
Correct |
463 ms |
17648 KB |
Output is correct |
27 |
Correct |
526 ms |
17600 KB |
Output is correct |
28 |
Correct |
198 ms |
20180 KB |
Output is correct |
29 |
Correct |
217 ms |
16076 KB |
Output is correct |
30 |
Correct |
1166 ms |
18312 KB |
Output is correct |
31 |
Correct |
632 ms |
18052 KB |
Output is correct |
32 |
Correct |
521 ms |
18172 KB |
Output is correct |
33 |
Correct |
388 ms |
17112 KB |
Output is correct |
34 |
Correct |
452 ms |
17172 KB |
Output is correct |
35 |
Correct |
545 ms |
17540 KB |
Output is correct |
36 |
Correct |
174 ms |
16140 KB |
Output is correct |
37 |
Correct |
1411 ms |
18100 KB |
Output is correct |
38 |
Correct |
193 ms |
16060 KB |
Output is correct |
39 |
Correct |
540 ms |
17320 KB |
Output is correct |
40 |
Correct |
452 ms |
17076 KB |
Output is correct |
41 |
Correct |
587 ms |
16068 KB |
Output is correct |
42 |
Correct |
782 ms |
16020 KB |
Output is correct |
43 |
Correct |
140 ms |
15920 KB |
Output is correct |
44 |
Correct |
116 ms |
15920 KB |
Output is correct |
45 |
Correct |
55 ms |
16104 KB |
Output is correct |
46 |
Correct |
90 ms |
16040 KB |
Output is correct |
47 |
Correct |
143 ms |
15920 KB |
Output is correct |
48 |
Correct |
33 ms |
16112 KB |
Output is correct |
49 |
Correct |
38 ms |
15812 KB |
Output is correct |
50 |
Correct |
831 ms |
15980 KB |
Output is correct |
51 |
Correct |
116 ms |
16016 KB |
Output is correct |
52 |
Correct |
121 ms |
15936 KB |
Output is correct |
53 |
Correct |
84 ms |
15948 KB |
Output is correct |
54 |
Correct |
80 ms |
15924 KB |
Output is correct |
55 |
Correct |
156 ms |
16032 KB |
Output is correct |
56 |
Correct |
35 ms |
15812 KB |
Output is correct |
57 |
Correct |
487 ms |
16020 KB |
Output is correct |
58 |
Correct |
41 ms |
15816 KB |
Output is correct |
59 |
Correct |
144 ms |
15936 KB |
Output is correct |
60 |
Correct |
86 ms |
15940 KB |
Output is correct |
61 |
Correct |
74 ms |
15952 KB |
Output is correct |
62 |
Correct |
100 ms |
15940 KB |
Output is correct |
63 |
Correct |
171 ms |
15920 KB |
Output is correct |
64 |
Correct |
35 ms |
15856 KB |
Output is correct |
65 |
Correct |
211 ms |
15916 KB |
Output is correct |
66 |
Correct |
225 ms |
15980 KB |
Output is correct |
67 |
Correct |
241 ms |
15952 KB |
Output is correct |
68 |
Correct |
207 ms |
15916 KB |
Output is correct |
69 |
Correct |
205 ms |
15976 KB |
Output is correct |
70 |
Correct |
206 ms |
15964 KB |
Output is correct |
71 |
Execution timed out |
2041 ms |
20436 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |