#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';
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
2 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 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 |
148 ms |
4540 KB |
Output is correct |
12 |
Correct |
165 ms |
3932 KB |
Output is correct |
13 |
Correct |
213 ms |
3400 KB |
Output is correct |
14 |
Correct |
284 ms |
3340 KB |
Output is correct |
15 |
Correct |
208 ms |
4424 KB |
Output is correct |
16 |
Correct |
318 ms |
3656 KB |
Output is correct |
17 |
Correct |
271 ms |
3412 KB |
Output is correct |
18 |
Correct |
116 ms |
5448 KB |
Output is correct |
19 |
Correct |
158 ms |
2376 KB |
Output is correct |
20 |
Correct |
166 ms |
3912 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 |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
2 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 |
148 ms |
4540 KB |
Output is correct |
12 |
Correct |
165 ms |
3932 KB |
Output is correct |
13 |
Correct |
213 ms |
3400 KB |
Output is correct |
14 |
Correct |
284 ms |
3340 KB |
Output is correct |
15 |
Correct |
208 ms |
4424 KB |
Output is correct |
16 |
Correct |
318 ms |
3656 KB |
Output is correct |
17 |
Correct |
271 ms |
3412 KB |
Output is correct |
18 |
Correct |
116 ms |
5448 KB |
Output is correct |
19 |
Correct |
158 ms |
2376 KB |
Output is correct |
20 |
Correct |
166 ms |
3912 KB |
Output is correct |
21 |
Correct |
162 ms |
4424 KB |
Output is correct |
22 |
Correct |
208 ms |
4652 KB |
Output is correct |
23 |
Correct |
439 ms |
3608 KB |
Output is correct |
24 |
Correct |
947 ms |
3388 KB |
Output is correct |
25 |
Correct |
259 ms |
8172 KB |
Output is correct |
26 |
Correct |
773 ms |
3924 KB |
Output is correct |
27 |
Correct |
583 ms |
4040 KB |
Output is correct |
28 |
Correct |
129 ms |
6472 KB |
Output is correct |
29 |
Correct |
177 ms |
2632 KB |
Output is correct |
30 |
Correct |
192 ms |
4680 KB |
Output is correct |
31 |
Correct |
484 ms |
4868 KB |
Output is correct |
32 |
Correct |
1006 ms |
4592 KB |
Output is correct |
33 |
Correct |
215 ms |
3400 KB |
Output is correct |
34 |
Correct |
725 ms |
9120 KB |
Output is correct |
35 |
Correct |
586 ms |
3876 KB |
Output is correct |
36 |
Correct |
133 ms |
6216 KB |
Output is correct |
37 |
Correct |
180 ms |
4472 KB |
Output is correct |
38 |
Correct |
200 ms |
2540 KB |
Output is correct |
39 |
Correct |
433 ms |
3656 KB |
Output is correct |
40 |
Correct |
955 ms |
3532 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 |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
2 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 |
59 ms |
20200 KB |
Output is correct |
12 |
Correct |
55 ms |
20200 KB |
Output is correct |
13 |
Correct |
113 ms |
20200 KB |
Output is correct |
14 |
Correct |
260 ms |
20204 KB |
Output is correct |
15 |
Correct |
76 ms |
20300 KB |
Output is correct |
16 |
Correct |
193 ms |
20200 KB |
Output is correct |
17 |
Correct |
178 ms |
20052 KB |
Output is correct |
18 |
Correct |
70 ms |
20456 KB |
Output is correct |
19 |
Correct |
63 ms |
20204 KB |
Output is correct |
20 |
Correct |
68 ms |
20200 KB |
Output is correct |
21 |
Correct |
121 ms |
20176 KB |
Output is correct |
22 |
Correct |
258 ms |
20200 KB |
Output is correct |
23 |
Correct |
66 ms |
20052 KB |
Output is correct |
24 |
Correct |
178 ms |
20200 KB |
Output is correct |
25 |
Correct |
151 ms |
20080 KB |
Output is correct |
26 |
Correct |
52 ms |
19944 KB |
Output is correct |
27 |
Correct |
53 ms |
20208 KB |
Output is correct |
28 |
Correct |
53 ms |
19944 KB |
Output is correct |
29 |
Correct |
111 ms |
20012 KB |
Output is correct |
30 |
Correct |
216 ms |
20204 KB |
Output is correct |
31 |
Correct |
72 ms |
20052 KB |
Output is correct |
32 |
Correct |
212 ms |
20200 KB |
Output is correct |
33 |
Correct |
167 ms |
20196 KB |
Output is correct |
34 |
Correct |
55 ms |
20056 KB |
Output is correct |
35 |
Correct |
130 ms |
20016 KB |
Output is correct |
36 |
Correct |
137 ms |
20200 KB |
Output is correct |
37 |
Correct |
133 ms |
20200 KB |
Output is correct |
38 |
Correct |
143 ms |
20200 KB |
Output is correct |
39 |
Correct |
147 ms |
20200 KB |
Output is correct |
40 |
Correct |
146 ms |
20204 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 |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
2 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 |
148 ms |
4540 KB |
Output is correct |
12 |
Correct |
165 ms |
3932 KB |
Output is correct |
13 |
Correct |
213 ms |
3400 KB |
Output is correct |
14 |
Correct |
284 ms |
3340 KB |
Output is correct |
15 |
Correct |
208 ms |
4424 KB |
Output is correct |
16 |
Correct |
318 ms |
3656 KB |
Output is correct |
17 |
Correct |
271 ms |
3412 KB |
Output is correct |
18 |
Correct |
116 ms |
5448 KB |
Output is correct |
19 |
Correct |
158 ms |
2376 KB |
Output is correct |
20 |
Correct |
166 ms |
3912 KB |
Output is correct |
21 |
Correct |
162 ms |
4424 KB |
Output is correct |
22 |
Correct |
208 ms |
4652 KB |
Output is correct |
23 |
Correct |
439 ms |
3608 KB |
Output is correct |
24 |
Correct |
947 ms |
3388 KB |
Output is correct |
25 |
Correct |
259 ms |
8172 KB |
Output is correct |
26 |
Correct |
773 ms |
3924 KB |
Output is correct |
27 |
Correct |
583 ms |
4040 KB |
Output is correct |
28 |
Correct |
129 ms |
6472 KB |
Output is correct |
29 |
Correct |
177 ms |
2632 KB |
Output is correct |
30 |
Correct |
192 ms |
4680 KB |
Output is correct |
31 |
Correct |
484 ms |
4868 KB |
Output is correct |
32 |
Correct |
1006 ms |
4592 KB |
Output is correct |
33 |
Correct |
215 ms |
3400 KB |
Output is correct |
34 |
Correct |
725 ms |
9120 KB |
Output is correct |
35 |
Correct |
586 ms |
3876 KB |
Output is correct |
36 |
Correct |
133 ms |
6216 KB |
Output is correct |
37 |
Correct |
180 ms |
4472 KB |
Output is correct |
38 |
Correct |
200 ms |
2540 KB |
Output is correct |
39 |
Correct |
433 ms |
3656 KB |
Output is correct |
40 |
Correct |
955 ms |
3532 KB |
Output is correct |
41 |
Correct |
59 ms |
20200 KB |
Output is correct |
42 |
Correct |
55 ms |
20200 KB |
Output is correct |
43 |
Correct |
113 ms |
20200 KB |
Output is correct |
44 |
Correct |
260 ms |
20204 KB |
Output is correct |
45 |
Correct |
76 ms |
20300 KB |
Output is correct |
46 |
Correct |
193 ms |
20200 KB |
Output is correct |
47 |
Correct |
178 ms |
20052 KB |
Output is correct |
48 |
Correct |
70 ms |
20456 KB |
Output is correct |
49 |
Correct |
63 ms |
20204 KB |
Output is correct |
50 |
Correct |
68 ms |
20200 KB |
Output is correct |
51 |
Correct |
121 ms |
20176 KB |
Output is correct |
52 |
Correct |
258 ms |
20200 KB |
Output is correct |
53 |
Correct |
66 ms |
20052 KB |
Output is correct |
54 |
Correct |
178 ms |
20200 KB |
Output is correct |
55 |
Correct |
151 ms |
20080 KB |
Output is correct |
56 |
Correct |
52 ms |
19944 KB |
Output is correct |
57 |
Correct |
53 ms |
20208 KB |
Output is correct |
58 |
Correct |
53 ms |
19944 KB |
Output is correct |
59 |
Correct |
111 ms |
20012 KB |
Output is correct |
60 |
Correct |
216 ms |
20204 KB |
Output is correct |
61 |
Correct |
72 ms |
20052 KB |
Output is correct |
62 |
Correct |
212 ms |
20200 KB |
Output is correct |
63 |
Correct |
167 ms |
20196 KB |
Output is correct |
64 |
Correct |
55 ms |
20056 KB |
Output is correct |
65 |
Correct |
130 ms |
20016 KB |
Output is correct |
66 |
Correct |
137 ms |
20200 KB |
Output is correct |
67 |
Correct |
133 ms |
20200 KB |
Output is correct |
68 |
Correct |
143 ms |
20200 KB |
Output is correct |
69 |
Correct |
147 ms |
20200 KB |
Output is correct |
70 |
Correct |
146 ms |
20204 KB |
Output is correct |
71 |
Correct |
387 ms |
44404 KB |
Output is correct |
72 |
Correct |
431 ms |
44520 KB |
Output is correct |
73 |
Correct |
1502 ms |
43144 KB |
Output is correct |
74 |
Execution timed out |
2065 ms |
27636 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |