#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 &1)) sos[mask][0][0] += s[mask - 1] - '0';
sos[mask][0][1] = s[mask] - '0';
if((mask &1) == 0) sos[mask][0][1] += s[mask + 1] - '0';
continue;
}
sos[mask][i &1][0] = sos[mask][(i - 1)&1][0];
sos[mask][i &1][1] = sos[mask][(i - 1)&1][1];
if(mask >> i & 1) sos[mask][(i)&1][0] += sos[mask - (1 << i)][(i - 1)&1][0];
else sos[mask][i &1][1] += sos[mask + (1 << i)][(i - 1)&1][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 &1) == (__builtin_popcount(mask) &1)) ans += sos[num][(n - 1)&1][0];
else ans -= sos[num][(n - 1)&1][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) & 1)) ans += sos[num][(n - 1)&1][1];
else ans -= sos[num][(n - 1)&1][1];
}
}
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
380 KB |
Output is correct |
5 |
Correct |
1 ms |
504 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 |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
380 KB |
Output is correct |
5 |
Correct |
1 ms |
504 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 |
492 KB |
Output is correct |
11 |
Correct |
160 ms |
4424 KB |
Output is correct |
12 |
Correct |
155 ms |
4160 KB |
Output is correct |
13 |
Correct |
209 ms |
3400 KB |
Output is correct |
14 |
Correct |
272 ms |
3444 KB |
Output is correct |
15 |
Correct |
192 ms |
4480 KB |
Output is correct |
16 |
Correct |
327 ms |
3912 KB |
Output is correct |
17 |
Correct |
291 ms |
3400 KB |
Output is correct |
18 |
Correct |
114 ms |
5448 KB |
Output is correct |
19 |
Correct |
154 ms |
2376 KB |
Output is correct |
20 |
Correct |
182 ms |
4040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
380 KB |
Output is correct |
5 |
Correct |
1 ms |
504 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 |
492 KB |
Output is correct |
11 |
Correct |
160 ms |
4424 KB |
Output is correct |
12 |
Correct |
155 ms |
4160 KB |
Output is correct |
13 |
Correct |
209 ms |
3400 KB |
Output is correct |
14 |
Correct |
272 ms |
3444 KB |
Output is correct |
15 |
Correct |
192 ms |
4480 KB |
Output is correct |
16 |
Correct |
327 ms |
3912 KB |
Output is correct |
17 |
Correct |
291 ms |
3400 KB |
Output is correct |
18 |
Correct |
114 ms |
5448 KB |
Output is correct |
19 |
Correct |
154 ms |
2376 KB |
Output is correct |
20 |
Correct |
182 ms |
4040 KB |
Output is correct |
21 |
Correct |
178 ms |
4628 KB |
Output is correct |
22 |
Correct |
193 ms |
4680 KB |
Output is correct |
23 |
Correct |
453 ms |
3772 KB |
Output is correct |
24 |
Correct |
983 ms |
3400 KB |
Output is correct |
25 |
Correct |
269 ms |
8368 KB |
Output is correct |
26 |
Correct |
783 ms |
7196 KB |
Output is correct |
27 |
Correct |
580 ms |
4060 KB |
Output is correct |
28 |
Correct |
134 ms |
6472 KB |
Output is correct |
29 |
Correct |
174 ms |
2376 KB |
Output is correct |
30 |
Correct |
197 ms |
4708 KB |
Output is correct |
31 |
Correct |
465 ms |
4424 KB |
Output is correct |
32 |
Correct |
1016 ms |
4424 KB |
Output is correct |
33 |
Correct |
242 ms |
3364 KB |
Output is correct |
34 |
Correct |
738 ms |
3396 KB |
Output is correct |
35 |
Correct |
575 ms |
3912 KB |
Output is correct |
36 |
Correct |
140 ms |
5960 KB |
Output is correct |
37 |
Correct |
172 ms |
4492 KB |
Output is correct |
38 |
Correct |
178 ms |
2388 KB |
Output is correct |
39 |
Correct |
436 ms |
3660 KB |
Output is correct |
40 |
Correct |
1000 ms |
3400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
380 KB |
Output is correct |
5 |
Correct |
1 ms |
504 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 |
492 KB |
Output is correct |
11 |
Correct |
61 ms |
18028 KB |
Output is correct |
12 |
Correct |
55 ms |
18156 KB |
Output is correct |
13 |
Correct |
108 ms |
18148 KB |
Output is correct |
14 |
Correct |
252 ms |
18164 KB |
Output is correct |
15 |
Correct |
75 ms |
18152 KB |
Output is correct |
16 |
Correct |
233 ms |
18032 KB |
Output is correct |
17 |
Correct |
171 ms |
18272 KB |
Output is correct |
18 |
Correct |
51 ms |
18408 KB |
Output is correct |
19 |
Correct |
59 ms |
18076 KB |
Output is correct |
20 |
Correct |
60 ms |
18152 KB |
Output is correct |
21 |
Correct |
122 ms |
18020 KB |
Output is correct |
22 |
Correct |
285 ms |
18016 KB |
Output is correct |
23 |
Correct |
75 ms |
18096 KB |
Output is correct |
24 |
Correct |
220 ms |
18156 KB |
Output is correct |
25 |
Correct |
141 ms |
18052 KB |
Output is correct |
26 |
Correct |
55 ms |
17908 KB |
Output is correct |
27 |
Correct |
52 ms |
18668 KB |
Output is correct |
28 |
Correct |
53 ms |
19316 KB |
Output is correct |
29 |
Correct |
100 ms |
18160 KB |
Output is correct |
30 |
Correct |
221 ms |
18920 KB |
Output is correct |
31 |
Correct |
65 ms |
18152 KB |
Output is correct |
32 |
Correct |
182 ms |
18152 KB |
Output is correct |
33 |
Correct |
137 ms |
18152 KB |
Output is correct |
34 |
Correct |
46 ms |
17908 KB |
Output is correct |
35 |
Correct |
107 ms |
18156 KB |
Output is correct |
36 |
Correct |
127 ms |
18152 KB |
Output is correct |
37 |
Correct |
112 ms |
18152 KB |
Output is correct |
38 |
Correct |
112 ms |
18152 KB |
Output is correct |
39 |
Correct |
116 ms |
18152 KB |
Output is correct |
40 |
Correct |
138 ms |
18152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
380 KB |
Output is correct |
5 |
Correct |
1 ms |
504 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 |
492 KB |
Output is correct |
11 |
Correct |
160 ms |
4424 KB |
Output is correct |
12 |
Correct |
155 ms |
4160 KB |
Output is correct |
13 |
Correct |
209 ms |
3400 KB |
Output is correct |
14 |
Correct |
272 ms |
3444 KB |
Output is correct |
15 |
Correct |
192 ms |
4480 KB |
Output is correct |
16 |
Correct |
327 ms |
3912 KB |
Output is correct |
17 |
Correct |
291 ms |
3400 KB |
Output is correct |
18 |
Correct |
114 ms |
5448 KB |
Output is correct |
19 |
Correct |
154 ms |
2376 KB |
Output is correct |
20 |
Correct |
182 ms |
4040 KB |
Output is correct |
21 |
Correct |
178 ms |
4628 KB |
Output is correct |
22 |
Correct |
193 ms |
4680 KB |
Output is correct |
23 |
Correct |
453 ms |
3772 KB |
Output is correct |
24 |
Correct |
983 ms |
3400 KB |
Output is correct |
25 |
Correct |
269 ms |
8368 KB |
Output is correct |
26 |
Correct |
783 ms |
7196 KB |
Output is correct |
27 |
Correct |
580 ms |
4060 KB |
Output is correct |
28 |
Correct |
134 ms |
6472 KB |
Output is correct |
29 |
Correct |
174 ms |
2376 KB |
Output is correct |
30 |
Correct |
197 ms |
4708 KB |
Output is correct |
31 |
Correct |
465 ms |
4424 KB |
Output is correct |
32 |
Correct |
1016 ms |
4424 KB |
Output is correct |
33 |
Correct |
242 ms |
3364 KB |
Output is correct |
34 |
Correct |
738 ms |
3396 KB |
Output is correct |
35 |
Correct |
575 ms |
3912 KB |
Output is correct |
36 |
Correct |
140 ms |
5960 KB |
Output is correct |
37 |
Correct |
172 ms |
4492 KB |
Output is correct |
38 |
Correct |
178 ms |
2388 KB |
Output is correct |
39 |
Correct |
436 ms |
3660 KB |
Output is correct |
40 |
Correct |
1000 ms |
3400 KB |
Output is correct |
41 |
Correct |
61 ms |
18028 KB |
Output is correct |
42 |
Correct |
55 ms |
18156 KB |
Output is correct |
43 |
Correct |
108 ms |
18148 KB |
Output is correct |
44 |
Correct |
252 ms |
18164 KB |
Output is correct |
45 |
Correct |
75 ms |
18152 KB |
Output is correct |
46 |
Correct |
233 ms |
18032 KB |
Output is correct |
47 |
Correct |
171 ms |
18272 KB |
Output is correct |
48 |
Correct |
51 ms |
18408 KB |
Output is correct |
49 |
Correct |
59 ms |
18076 KB |
Output is correct |
50 |
Correct |
60 ms |
18152 KB |
Output is correct |
51 |
Correct |
122 ms |
18020 KB |
Output is correct |
52 |
Correct |
285 ms |
18016 KB |
Output is correct |
53 |
Correct |
75 ms |
18096 KB |
Output is correct |
54 |
Correct |
220 ms |
18156 KB |
Output is correct |
55 |
Correct |
141 ms |
18052 KB |
Output is correct |
56 |
Correct |
55 ms |
17908 KB |
Output is correct |
57 |
Correct |
52 ms |
18668 KB |
Output is correct |
58 |
Correct |
53 ms |
19316 KB |
Output is correct |
59 |
Correct |
100 ms |
18160 KB |
Output is correct |
60 |
Correct |
221 ms |
18920 KB |
Output is correct |
61 |
Correct |
65 ms |
18152 KB |
Output is correct |
62 |
Correct |
182 ms |
18152 KB |
Output is correct |
63 |
Correct |
137 ms |
18152 KB |
Output is correct |
64 |
Correct |
46 ms |
17908 KB |
Output is correct |
65 |
Correct |
107 ms |
18156 KB |
Output is correct |
66 |
Correct |
127 ms |
18152 KB |
Output is correct |
67 |
Correct |
112 ms |
18152 KB |
Output is correct |
68 |
Correct |
112 ms |
18152 KB |
Output is correct |
69 |
Correct |
116 ms |
18152 KB |
Output is correct |
70 |
Correct |
138 ms |
18152 KB |
Output is correct |
71 |
Correct |
373 ms |
23140 KB |
Output is correct |
72 |
Correct |
383 ms |
23020 KB |
Output is correct |
73 |
Correct |
1560 ms |
21656 KB |
Output is correct |
74 |
Execution timed out |
2071 ms |
19944 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |