// 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 = 2e5 + 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 cnt = __builtin_popcount(mask2) & 1, ans = 0;
for(int i = mask2; i > 0; i = (i - 1) & mask2) {
if((__builtin_popcount(i) & 1) == cnt) ans += fd[i + mask];
else ans -= fd[i + mask];
}
if(cnt == 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 |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
336 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 |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
336 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 |
264 ms |
11812 KB |
Output is correct |
12 |
Correct |
254 ms |
11436 KB |
Output is correct |
13 |
Correct |
219 ms |
10624 KB |
Output is correct |
14 |
Correct |
256 ms |
10732 KB |
Output is correct |
15 |
Correct |
283 ms |
11740 KB |
Output is correct |
16 |
Correct |
261 ms |
10964 KB |
Output is correct |
17 |
Correct |
316 ms |
10940 KB |
Output is correct |
18 |
Correct |
209 ms |
12748 KB |
Output is correct |
19 |
Correct |
183 ms |
9788 KB |
Output is correct |
20 |
Correct |
339 ms |
11388 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 |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
336 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 |
264 ms |
11812 KB |
Output is correct |
12 |
Correct |
254 ms |
11436 KB |
Output is correct |
13 |
Correct |
219 ms |
10624 KB |
Output is correct |
14 |
Correct |
256 ms |
10732 KB |
Output is correct |
15 |
Correct |
283 ms |
11740 KB |
Output is correct |
16 |
Correct |
261 ms |
10964 KB |
Output is correct |
17 |
Correct |
316 ms |
10940 KB |
Output is correct |
18 |
Correct |
209 ms |
12748 KB |
Output is correct |
19 |
Correct |
183 ms |
9788 KB |
Output is correct |
20 |
Correct |
339 ms |
11388 KB |
Output is correct |
21 |
Correct |
222 ms |
11888 KB |
Output is correct |
22 |
Correct |
286 ms |
12008 KB |
Output is correct |
23 |
Correct |
289 ms |
11012 KB |
Output is correct |
24 |
Correct |
285 ms |
10872 KB |
Output is correct |
25 |
Correct |
255 ms |
12936 KB |
Output is correct |
26 |
Correct |
366 ms |
11348 KB |
Output is correct |
27 |
Correct |
338 ms |
11344 KB |
Output is correct |
28 |
Correct |
235 ms |
13924 KB |
Output is correct |
29 |
Correct |
215 ms |
9916 KB |
Output is correct |
30 |
Correct |
313 ms |
12008 KB |
Output is correct |
31 |
Correct |
365 ms |
11940 KB |
Output is correct |
32 |
Correct |
318 ms |
11876 KB |
Output is correct |
33 |
Correct |
248 ms |
10752 KB |
Output is correct |
34 |
Correct |
286 ms |
10912 KB |
Output is correct |
35 |
Correct |
322 ms |
11280 KB |
Output is correct |
36 |
Correct |
229 ms |
9904 KB |
Output is correct |
37 |
Correct |
345 ms |
11884 KB |
Output is correct |
38 |
Correct |
193 ms |
9924 KB |
Output is correct |
39 |
Correct |
271 ms |
11064 KB |
Output is correct |
40 |
Correct |
255 ms |
10956 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 |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
336 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 |
Runtime error |
6 ms |
5684 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
336 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 |
264 ms |
11812 KB |
Output is correct |
12 |
Correct |
254 ms |
11436 KB |
Output is correct |
13 |
Correct |
219 ms |
10624 KB |
Output is correct |
14 |
Correct |
256 ms |
10732 KB |
Output is correct |
15 |
Correct |
283 ms |
11740 KB |
Output is correct |
16 |
Correct |
261 ms |
10964 KB |
Output is correct |
17 |
Correct |
316 ms |
10940 KB |
Output is correct |
18 |
Correct |
209 ms |
12748 KB |
Output is correct |
19 |
Correct |
183 ms |
9788 KB |
Output is correct |
20 |
Correct |
339 ms |
11388 KB |
Output is correct |
21 |
Correct |
222 ms |
11888 KB |
Output is correct |
22 |
Correct |
286 ms |
12008 KB |
Output is correct |
23 |
Correct |
289 ms |
11012 KB |
Output is correct |
24 |
Correct |
285 ms |
10872 KB |
Output is correct |
25 |
Correct |
255 ms |
12936 KB |
Output is correct |
26 |
Correct |
366 ms |
11348 KB |
Output is correct |
27 |
Correct |
338 ms |
11344 KB |
Output is correct |
28 |
Correct |
235 ms |
13924 KB |
Output is correct |
29 |
Correct |
215 ms |
9916 KB |
Output is correct |
30 |
Correct |
313 ms |
12008 KB |
Output is correct |
31 |
Correct |
365 ms |
11940 KB |
Output is correct |
32 |
Correct |
318 ms |
11876 KB |
Output is correct |
33 |
Correct |
248 ms |
10752 KB |
Output is correct |
34 |
Correct |
286 ms |
10912 KB |
Output is correct |
35 |
Correct |
322 ms |
11280 KB |
Output is correct |
36 |
Correct |
229 ms |
9904 KB |
Output is correct |
37 |
Correct |
345 ms |
11884 KB |
Output is correct |
38 |
Correct |
193 ms |
9924 KB |
Output is correct |
39 |
Correct |
271 ms |
11064 KB |
Output is correct |
40 |
Correct |
255 ms |
10956 KB |
Output is correct |
41 |
Runtime error |
6 ms |
5684 KB |
Execution killed with signal 11 |
42 |
Halted |
0 ms |
0 KB |
- |