#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] == '1') 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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
168 ms |
15020 KB |
Output is correct |
12 |
Correct |
191 ms |
14700 KB |
Output is correct |
13 |
Correct |
249 ms |
14104 KB |
Output is correct |
14 |
Correct |
277 ms |
14096 KB |
Output is correct |
15 |
Correct |
245 ms |
15056 KB |
Output is correct |
16 |
Correct |
284 ms |
14268 KB |
Output is correct |
17 |
Correct |
282 ms |
14244 KB |
Output is correct |
18 |
Correct |
136 ms |
16108 KB |
Output is correct |
19 |
Correct |
166 ms |
13012 KB |
Output is correct |
20 |
Correct |
205 ms |
14764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
168 ms |
15020 KB |
Output is correct |
12 |
Correct |
191 ms |
14700 KB |
Output is correct |
13 |
Correct |
249 ms |
14104 KB |
Output is correct |
14 |
Correct |
277 ms |
14096 KB |
Output is correct |
15 |
Correct |
245 ms |
15056 KB |
Output is correct |
16 |
Correct |
284 ms |
14268 KB |
Output is correct |
17 |
Correct |
282 ms |
14244 KB |
Output is correct |
18 |
Correct |
136 ms |
16108 KB |
Output is correct |
19 |
Correct |
166 ms |
13012 KB |
Output is correct |
20 |
Correct |
205 ms |
14764 KB |
Output is correct |
21 |
Correct |
196 ms |
18120 KB |
Output is correct |
22 |
Correct |
237 ms |
18240 KB |
Output is correct |
23 |
Correct |
346 ms |
17284 KB |
Output is correct |
24 |
Correct |
439 ms |
17044 KB |
Output is correct |
25 |
Correct |
324 ms |
19112 KB |
Output is correct |
26 |
Correct |
430 ms |
17552 KB |
Output is correct |
27 |
Correct |
391 ms |
17544 KB |
Output is correct |
28 |
Correct |
151 ms |
20088 KB |
Output is correct |
29 |
Correct |
178 ms |
16072 KB |
Output is correct |
30 |
Correct |
274 ms |
18236 KB |
Output is correct |
31 |
Correct |
373 ms |
18092 KB |
Output is correct |
32 |
Correct |
516 ms |
18040 KB |
Output is correct |
33 |
Correct |
293 ms |
16984 KB |
Output is correct |
34 |
Correct |
444 ms |
17164 KB |
Output is correct |
35 |
Correct |
395 ms |
17660 KB |
Output is correct |
36 |
Correct |
150 ms |
16076 KB |
Output is correct |
37 |
Correct |
229 ms |
18056 KB |
Output is correct |
38 |
Correct |
203 ms |
16172 KB |
Output is correct |
39 |
Correct |
345 ms |
17504 KB |
Output is correct |
40 |
Correct |
438 ms |
17044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
40 ms |
16056 KB |
Output is correct |
12 |
Correct |
44 ms |
16076 KB |
Output is correct |
13 |
Correct |
71 ms |
15932 KB |
Output is correct |
14 |
Correct |
91 ms |
15956 KB |
Output is correct |
15 |
Correct |
54 ms |
16040 KB |
Output is correct |
16 |
Correct |
82 ms |
16044 KB |
Output is correct |
17 |
Correct |
91 ms |
15944 KB |
Output is correct |
18 |
Correct |
33 ms |
16212 KB |
Output is correct |
19 |
Correct |
42 ms |
15924 KB |
Output is correct |
20 |
Correct |
45 ms |
16080 KB |
Output is correct |
21 |
Correct |
65 ms |
16044 KB |
Output is correct |
22 |
Correct |
86 ms |
15924 KB |
Output is correct |
23 |
Correct |
58 ms |
15884 KB |
Output is correct |
24 |
Correct |
96 ms |
15992 KB |
Output is correct |
25 |
Correct |
78 ms |
16024 KB |
Output is correct |
26 |
Correct |
33 ms |
15924 KB |
Output is correct |
27 |
Correct |
41 ms |
16060 KB |
Output is correct |
28 |
Correct |
38 ms |
15804 KB |
Output is correct |
29 |
Correct |
60 ms |
16000 KB |
Output is correct |
30 |
Correct |
82 ms |
15980 KB |
Output is correct |
31 |
Correct |
53 ms |
15980 KB |
Output is correct |
32 |
Correct |
100 ms |
16036 KB |
Output is correct |
33 |
Correct |
79 ms |
15940 KB |
Output is correct |
34 |
Correct |
44 ms |
15856 KB |
Output is correct |
35 |
Correct |
66 ms |
16008 KB |
Output is correct |
36 |
Correct |
67 ms |
15980 KB |
Output is correct |
37 |
Correct |
70 ms |
15924 KB |
Output is correct |
38 |
Correct |
66 ms |
15984 KB |
Output is correct |
39 |
Correct |
94 ms |
16020 KB |
Output is correct |
40 |
Correct |
69 ms |
16004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
168 ms |
15020 KB |
Output is correct |
12 |
Correct |
191 ms |
14700 KB |
Output is correct |
13 |
Correct |
249 ms |
14104 KB |
Output is correct |
14 |
Correct |
277 ms |
14096 KB |
Output is correct |
15 |
Correct |
245 ms |
15056 KB |
Output is correct |
16 |
Correct |
284 ms |
14268 KB |
Output is correct |
17 |
Correct |
282 ms |
14244 KB |
Output is correct |
18 |
Correct |
136 ms |
16108 KB |
Output is correct |
19 |
Correct |
166 ms |
13012 KB |
Output is correct |
20 |
Correct |
205 ms |
14764 KB |
Output is correct |
21 |
Correct |
196 ms |
18120 KB |
Output is correct |
22 |
Correct |
237 ms |
18240 KB |
Output is correct |
23 |
Correct |
346 ms |
17284 KB |
Output is correct |
24 |
Correct |
439 ms |
17044 KB |
Output is correct |
25 |
Correct |
324 ms |
19112 KB |
Output is correct |
26 |
Correct |
430 ms |
17552 KB |
Output is correct |
27 |
Correct |
391 ms |
17544 KB |
Output is correct |
28 |
Correct |
151 ms |
20088 KB |
Output is correct |
29 |
Correct |
178 ms |
16072 KB |
Output is correct |
30 |
Correct |
274 ms |
18236 KB |
Output is correct |
31 |
Correct |
373 ms |
18092 KB |
Output is correct |
32 |
Correct |
516 ms |
18040 KB |
Output is correct |
33 |
Correct |
293 ms |
16984 KB |
Output is correct |
34 |
Correct |
444 ms |
17164 KB |
Output is correct |
35 |
Correct |
395 ms |
17660 KB |
Output is correct |
36 |
Correct |
150 ms |
16076 KB |
Output is correct |
37 |
Correct |
229 ms |
18056 KB |
Output is correct |
38 |
Correct |
203 ms |
16172 KB |
Output is correct |
39 |
Correct |
345 ms |
17504 KB |
Output is correct |
40 |
Correct |
438 ms |
17044 KB |
Output is correct |
41 |
Correct |
40 ms |
16056 KB |
Output is correct |
42 |
Correct |
44 ms |
16076 KB |
Output is correct |
43 |
Correct |
71 ms |
15932 KB |
Output is correct |
44 |
Correct |
91 ms |
15956 KB |
Output is correct |
45 |
Correct |
54 ms |
16040 KB |
Output is correct |
46 |
Correct |
82 ms |
16044 KB |
Output is correct |
47 |
Correct |
91 ms |
15944 KB |
Output is correct |
48 |
Correct |
33 ms |
16212 KB |
Output is correct |
49 |
Correct |
42 ms |
15924 KB |
Output is correct |
50 |
Correct |
45 ms |
16080 KB |
Output is correct |
51 |
Correct |
65 ms |
16044 KB |
Output is correct |
52 |
Correct |
86 ms |
15924 KB |
Output is correct |
53 |
Correct |
58 ms |
15884 KB |
Output is correct |
54 |
Correct |
96 ms |
15992 KB |
Output is correct |
55 |
Correct |
78 ms |
16024 KB |
Output is correct |
56 |
Correct |
33 ms |
15924 KB |
Output is correct |
57 |
Correct |
41 ms |
16060 KB |
Output is correct |
58 |
Correct |
38 ms |
15804 KB |
Output is correct |
59 |
Correct |
60 ms |
16000 KB |
Output is correct |
60 |
Correct |
82 ms |
15980 KB |
Output is correct |
61 |
Correct |
53 ms |
15980 KB |
Output is correct |
62 |
Correct |
100 ms |
16036 KB |
Output is correct |
63 |
Correct |
79 ms |
15940 KB |
Output is correct |
64 |
Correct |
44 ms |
15856 KB |
Output is correct |
65 |
Correct |
66 ms |
16008 KB |
Output is correct |
66 |
Correct |
67 ms |
15980 KB |
Output is correct |
67 |
Correct |
70 ms |
15924 KB |
Output is correct |
68 |
Correct |
66 ms |
15984 KB |
Output is correct |
69 |
Correct |
94 ms |
16020 KB |
Output is correct |
70 |
Correct |
69 ms |
16004 KB |
Output is correct |
71 |
Correct |
308 ms |
40264 KB |
Output is correct |
72 |
Correct |
414 ms |
40464 KB |
Output is correct |
73 |
Correct |
843 ms |
38944 KB |
Output is correct |
74 |
Correct |
1410 ms |
39352 KB |
Output is correct |
75 |
Correct |
621 ms |
41352 KB |
Output is correct |
76 |
Correct |
1405 ms |
39620 KB |
Output is correct |
77 |
Correct |
1208 ms |
39524 KB |
Output is correct |
78 |
Correct |
207 ms |
43348 KB |
Output is correct |
79 |
Correct |
302 ms |
37340 KB |
Output is correct |
80 |
Correct |
413 ms |
40456 KB |
Output is correct |
81 |
Correct |
852 ms |
40360 KB |
Output is correct |
82 |
Correct |
1275 ms |
39340 KB |
Output is correct |
83 |
Correct |
553 ms |
38528 KB |
Output is correct |
84 |
Correct |
1238 ms |
39264 KB |
Output is correct |
85 |
Correct |
1213 ms |
39568 KB |
Output is correct |
86 |
Correct |
212 ms |
37296 KB |
Output is correct |
87 |
Correct |
300 ms |
40376 KB |
Output is correct |
88 |
Correct |
307 ms |
37324 KB |
Output is correct |
89 |
Correct |
837 ms |
38956 KB |
Output is correct |
90 |
Correct |
1187 ms |
39304 KB |
Output is correct |
91 |
Correct |
607 ms |
38512 KB |
Output is correct |
92 |
Correct |
1277 ms |
39596 KB |
Output is correct |
93 |
Correct |
1091 ms |
39464 KB |
Output is correct |
94 |
Correct |
211 ms |
37320 KB |
Output is correct |
95 |
Correct |
942 ms |
39392 KB |
Output is correct |
96 |
Correct |
968 ms |
39360 KB |
Output is correct |
97 |
Correct |
1016 ms |
39368 KB |
Output is correct |
98 |
Correct |
915 ms |
39420 KB |
Output is correct |
99 |
Correct |
849 ms |
39452 KB |
Output is correct |
100 |
Correct |
884 ms |
39484 KB |
Output is correct |