// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
//#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
//#pragma GCC target("avx2,fma")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define endl '\n'
constexpr ll inf = 1e18, N = 2e6 + 10, mod = 1e9 + 7, pr = 1000696969;
int a[N], fd[N], fu[N], fu2[N];
int main() {
ios :: sync_with_stdio(0), cin.tie(0);
int l, q; cin >>l >>q;
string s; cin >>s;
for(int i = 0; i < Sz(s); i++) {
a[i] = s[i] - '0';
}
int to = (1 << l) - 1;
for(int i = 0; i < (1 << l); i++) {
fd[i] = a[i], fu2[i ^ to] = a[i];
}
for(int i = 0; i < l; i++) {
for(int mask = 0; mask < (1 << l); mask++) {
if((mask >> i) & 1) {
fd[mask] += fd[mask ^ (1 << i)];
fu2[mask] += fu2[mask ^ (1 << i)];
}
}
}
for(int mask = 0; mask < (1 << l); mask++) {
fu[mask] = fu2[mask ^ to];
}
while(q--) {
cin >>s;
int cnt0 = 0, cnt1 = 0, cnt = 0;
for(int i = 0; i < Sz(s); i++) {
cnt0 += (s[i] == '0');
cnt1 += (s[i] == '1');
cnt += (s[i] == '?');
}
int mask = 0, mask2 = 0;
if(cnt <= 6) {
for(int i = 0; i < Sz(s); i++) {
if(s[i] == '?') mask2 += (1 << (Sz(s) - i - 1));
if(s[i] == '1') mask += (1 << (Sz(s) - i - 1));
}
int ans = a[mask];
for(int i = mask2; i > 0; i = (i - 1) & mask2) {
ans += a[i + mask];
}
cout<<ans <<endl;
continue;
}
if(cnt1 <= 6) {
for(int i = 0; i < Sz(s); i++) {
if(s[i] == '1') mask2 += (1 << (Sz(s) - i - 1));
if(s[i] == '?') mask += (1 << (Sz(s) - i - 1));
}
int cnte = __builtin_popcount(mask2) & 1, ans = 0;
for(int i = mask2; i > 0; i = (i - 1) & mask2) {
if((__builtin_popcount(i) & 1) == cnte) ans += fd[i + mask];
else ans -= fd[i + mask];
}
if(cnte == 0) ans += fd[mask];
else ans -= fd[mask];
cout<<ans <<endl;
continue;
}
for(int i = 0; i < Sz(s); i++) {
if(s[i] == '0') mask2 += (1 << (Sz(s) - i - 1));
if(s[i] == '1') mask += (1 << (Sz(s) - i - 1));
}
int cnte = 0, ans = 0;
for(int i = mask2; i > 0; i = (i - 1) & mask2) {
if((__builtin_popcount(i) & 1) == cnte) ans += fu[i + mask];
else ans -= fu[i + mask];
}
ans += fu[mask];
cout<<ans <<endl;
}
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 |
221 ms |
4332 KB |
Output is correct |
12 |
Correct |
223 ms |
3956 KB |
Output is correct |
13 |
Correct |
184 ms |
3240 KB |
Output is correct |
14 |
Correct |
190 ms |
3296 KB |
Output is correct |
15 |
Correct |
253 ms |
4292 KB |
Output is correct |
16 |
Correct |
206 ms |
3492 KB |
Output is correct |
17 |
Correct |
206 ms |
3476 KB |
Output is correct |
18 |
Correct |
161 ms |
5324 KB |
Output is correct |
19 |
Correct |
153 ms |
2252 KB |
Output is correct |
20 |
Correct |
229 ms |
3940 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 |
221 ms |
4332 KB |
Output is correct |
12 |
Correct |
223 ms |
3956 KB |
Output is correct |
13 |
Correct |
184 ms |
3240 KB |
Output is correct |
14 |
Correct |
190 ms |
3296 KB |
Output is correct |
15 |
Correct |
253 ms |
4292 KB |
Output is correct |
16 |
Correct |
206 ms |
3492 KB |
Output is correct |
17 |
Correct |
206 ms |
3476 KB |
Output is correct |
18 |
Correct |
161 ms |
5324 KB |
Output is correct |
19 |
Correct |
153 ms |
2252 KB |
Output is correct |
20 |
Correct |
229 ms |
3940 KB |
Output is correct |
21 |
Correct |
523 ms |
4428 KB |
Output is correct |
22 |
Correct |
233 ms |
4568 KB |
Output is correct |
23 |
Correct |
219 ms |
3664 KB |
Output is correct |
24 |
Correct |
221 ms |
3396 KB |
Output is correct |
25 |
Correct |
226 ms |
5424 KB |
Output is correct |
26 |
Correct |
230 ms |
4008 KB |
Output is correct |
27 |
Correct |
228 ms |
3944 KB |
Output is correct |
28 |
Correct |
177 ms |
6484 KB |
Output is correct |
29 |
Correct |
159 ms |
2456 KB |
Output is correct |
30 |
Correct |
324 ms |
4596 KB |
Output is correct |
31 |
Correct |
275 ms |
4432 KB |
Output is correct |
32 |
Correct |
237 ms |
4388 KB |
Output is correct |
33 |
Correct |
198 ms |
3364 KB |
Output is correct |
34 |
Correct |
210 ms |
3532 KB |
Output is correct |
35 |
Correct |
230 ms |
3960 KB |
Output is correct |
36 |
Correct |
160 ms |
2380 KB |
Output is correct |
37 |
Correct |
293 ms |
4488 KB |
Output is correct |
38 |
Correct |
163 ms |
2444 KB |
Output is correct |
39 |
Correct |
220 ms |
3568 KB |
Output is correct |
40 |
Correct |
220 ms |
3476 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 |
43 ms |
18936 KB |
Output is correct |
12 |
Correct |
45 ms |
20136 KB |
Output is correct |
13 |
Correct |
48 ms |
20128 KB |
Output is correct |
14 |
Correct |
63 ms |
20148 KB |
Output is correct |
15 |
Correct |
49 ms |
20332 KB |
Output is correct |
16 |
Correct |
66 ms |
20176 KB |
Output is correct |
17 |
Correct |
61 ms |
20076 KB |
Output is correct |
18 |
Correct |
41 ms |
20328 KB |
Output is correct |
19 |
Correct |
44 ms |
19956 KB |
Output is correct |
20 |
Correct |
45 ms |
20224 KB |
Output is correct |
21 |
Correct |
56 ms |
20188 KB |
Output is correct |
22 |
Correct |
57 ms |
20056 KB |
Output is correct |
23 |
Correct |
43 ms |
20100 KB |
Output is correct |
24 |
Correct |
50 ms |
20132 KB |
Output is correct |
25 |
Correct |
62 ms |
20080 KB |
Output is correct |
26 |
Correct |
43 ms |
19992 KB |
Output is correct |
27 |
Correct |
42 ms |
20204 KB |
Output is correct |
28 |
Correct |
42 ms |
20000 KB |
Output is correct |
29 |
Correct |
50 ms |
20072 KB |
Output is correct |
30 |
Correct |
49 ms |
20060 KB |
Output is correct |
31 |
Correct |
44 ms |
20072 KB |
Output is correct |
32 |
Correct |
62 ms |
20076 KB |
Output is correct |
33 |
Correct |
58 ms |
20160 KB |
Output is correct |
34 |
Correct |
41 ms |
20064 KB |
Output is correct |
35 |
Correct |
53 ms |
20148 KB |
Output is correct |
36 |
Correct |
57 ms |
20060 KB |
Output is correct |
37 |
Correct |
58 ms |
20072 KB |
Output is correct |
38 |
Correct |
58 ms |
20072 KB |
Output is correct |
39 |
Correct |
60 ms |
20076 KB |
Output is correct |
40 |
Correct |
56 ms |
20192 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 |
221 ms |
4332 KB |
Output is correct |
12 |
Correct |
223 ms |
3956 KB |
Output is correct |
13 |
Correct |
184 ms |
3240 KB |
Output is correct |
14 |
Correct |
190 ms |
3296 KB |
Output is correct |
15 |
Correct |
253 ms |
4292 KB |
Output is correct |
16 |
Correct |
206 ms |
3492 KB |
Output is correct |
17 |
Correct |
206 ms |
3476 KB |
Output is correct |
18 |
Correct |
161 ms |
5324 KB |
Output is correct |
19 |
Correct |
153 ms |
2252 KB |
Output is correct |
20 |
Correct |
229 ms |
3940 KB |
Output is correct |
21 |
Correct |
523 ms |
4428 KB |
Output is correct |
22 |
Correct |
233 ms |
4568 KB |
Output is correct |
23 |
Correct |
219 ms |
3664 KB |
Output is correct |
24 |
Correct |
221 ms |
3396 KB |
Output is correct |
25 |
Correct |
226 ms |
5424 KB |
Output is correct |
26 |
Correct |
230 ms |
4008 KB |
Output is correct |
27 |
Correct |
228 ms |
3944 KB |
Output is correct |
28 |
Correct |
177 ms |
6484 KB |
Output is correct |
29 |
Correct |
159 ms |
2456 KB |
Output is correct |
30 |
Correct |
324 ms |
4596 KB |
Output is correct |
31 |
Correct |
275 ms |
4432 KB |
Output is correct |
32 |
Correct |
237 ms |
4388 KB |
Output is correct |
33 |
Correct |
198 ms |
3364 KB |
Output is correct |
34 |
Correct |
210 ms |
3532 KB |
Output is correct |
35 |
Correct |
230 ms |
3960 KB |
Output is correct |
36 |
Correct |
160 ms |
2380 KB |
Output is correct |
37 |
Correct |
293 ms |
4488 KB |
Output is correct |
38 |
Correct |
163 ms |
2444 KB |
Output is correct |
39 |
Correct |
220 ms |
3568 KB |
Output is correct |
40 |
Correct |
220 ms |
3476 KB |
Output is correct |
41 |
Correct |
43 ms |
18936 KB |
Output is correct |
42 |
Correct |
45 ms |
20136 KB |
Output is correct |
43 |
Correct |
48 ms |
20128 KB |
Output is correct |
44 |
Correct |
63 ms |
20148 KB |
Output is correct |
45 |
Correct |
49 ms |
20332 KB |
Output is correct |
46 |
Correct |
66 ms |
20176 KB |
Output is correct |
47 |
Correct |
61 ms |
20076 KB |
Output is correct |
48 |
Correct |
41 ms |
20328 KB |
Output is correct |
49 |
Correct |
44 ms |
19956 KB |
Output is correct |
50 |
Correct |
45 ms |
20224 KB |
Output is correct |
51 |
Correct |
56 ms |
20188 KB |
Output is correct |
52 |
Correct |
57 ms |
20056 KB |
Output is correct |
53 |
Correct |
43 ms |
20100 KB |
Output is correct |
54 |
Correct |
50 ms |
20132 KB |
Output is correct |
55 |
Correct |
62 ms |
20080 KB |
Output is correct |
56 |
Correct |
43 ms |
19992 KB |
Output is correct |
57 |
Correct |
42 ms |
20204 KB |
Output is correct |
58 |
Correct |
42 ms |
20000 KB |
Output is correct |
59 |
Correct |
50 ms |
20072 KB |
Output is correct |
60 |
Correct |
49 ms |
20060 KB |
Output is correct |
61 |
Correct |
44 ms |
20072 KB |
Output is correct |
62 |
Correct |
62 ms |
20076 KB |
Output is correct |
63 |
Correct |
58 ms |
20160 KB |
Output is correct |
64 |
Correct |
41 ms |
20064 KB |
Output is correct |
65 |
Correct |
53 ms |
20148 KB |
Output is correct |
66 |
Correct |
57 ms |
20060 KB |
Output is correct |
67 |
Correct |
58 ms |
20072 KB |
Output is correct |
68 |
Correct |
58 ms |
20072 KB |
Output is correct |
69 |
Correct |
60 ms |
20076 KB |
Output is correct |
70 |
Correct |
56 ms |
20192 KB |
Output is correct |
71 |
Correct |
234 ms |
23360 KB |
Output is correct |
72 |
Correct |
282 ms |
23476 KB |
Output is correct |
73 |
Correct |
342 ms |
22252 KB |
Output is correct |
74 |
Correct |
649 ms |
22404 KB |
Output is correct |
75 |
Correct |
364 ms |
24392 KB |
Output is correct |
76 |
Correct |
772 ms |
22844 KB |
Output is correct |
77 |
Correct |
591 ms |
22512 KB |
Output is correct |
78 |
Correct |
243 ms |
26348 KB |
Output is correct |
79 |
Correct |
240 ms |
20428 KB |
Output is correct |
80 |
Correct |
276 ms |
23452 KB |
Output is correct |
81 |
Correct |
447 ms |
23492 KB |
Output is correct |
82 |
Correct |
584 ms |
22456 KB |
Output is correct |
83 |
Correct |
281 ms |
21552 KB |
Output is correct |
84 |
Correct |
353 ms |
22292 KB |
Output is correct |
85 |
Correct |
590 ms |
22676 KB |
Output is correct |
86 |
Correct |
206 ms |
20456 KB |
Output is correct |
87 |
Correct |
231 ms |
23400 KB |
Output is correct |
88 |
Correct |
220 ms |
20296 KB |
Output is correct |
89 |
Correct |
329 ms |
22032 KB |
Output is correct |
90 |
Correct |
385 ms |
22392 KB |
Output is correct |
91 |
Correct |
296 ms |
21532 KB |
Output is correct |
92 |
Correct |
638 ms |
22816 KB |
Output is correct |
93 |
Correct |
594 ms |
22564 KB |
Output is correct |
94 |
Correct |
213 ms |
20336 KB |
Output is correct |
95 |
Correct |
494 ms |
22480 KB |
Output is correct |
96 |
Correct |
555 ms |
22380 KB |
Output is correct |
97 |
Correct |
503 ms |
22456 KB |
Output is correct |
98 |
Correct |
492 ms |
22476 KB |
Output is correct |
99 |
Correct |
498 ms |
22504 KB |
Output is correct |
100 |
Correct |
501 ms |
22436 KB |
Output is correct |