#include<bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
const int maxn = 21;
int a[1 << maxn], cnt[1 << maxn], n, q, cnt1[1 << maxn];
string s;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> q >> s;
for (int i = 0;i < (1 << n);i++)
cnt[i] = cnt1[i] = (s[i] - '0');
for (int i = 0;i < n;i++) {
for (int mask = 0;mask < (1 << n);mask++) {
if (mask & (1 << i))
cnt[mask] += cnt[mask ^ (1 << i)];
}
for (int mask = (1 << n) - 1;mask >= 0;mask--) {
if (!(mask & (1 << i)))
cnt1[mask] += cnt1[mask ^ (1 << i)];
}
}
while (q--) {
string t;
cin >> t;
reverse(t.begin(), t.end());
vector<int> a, b, c;
int x = 0;
for (int i = 0;i < n;i++) {
x += (t[i] == '1' ? (1 << i) : 0);
if (t[i] == '1')
a.push_back(i);
else if (t[i] == '0')
b.push_back(i);
else
c.push_back(i);
}
int ans = 0;
if (c.size() <= 6) {
for (int mask = 0;mask < (1 << (int)c.size());mask++) {
int y = x;
for (int j = 0;j < (int)c.size();j++) {
if (mask & (1 << j))
y |= (1 << c[j]);
}
ans += (s[y] - '0');
}
}
else if (a.size() <= 6) {
int tx = x;
for (int i = 0;i < n;i++)
tx += (t[i] == '?' ? (1 << i) : 0);
for (int mask = 0;mask < (1 << (int)a.size());mask++) {
int y = tx;
for (int j = 0;j < (int)a.size();j++) {
if (mask & (1 << j))
y -= (1 << a[j]);
}
ans += cnt[y] * (__builtin_popcount(mask) % 2 == 0 ? 1 : -1);
}
}
else {
for (int mask = 0;mask < (1 << (int)b.size());mask++) {
int y = x;
for (int j = 0;j < (int)b.size();j++) {
if (mask & (1 << j))
y += (1 << b[j]);
}
ans += cnt1[y] * (__builtin_popcount(mask) % 2 == 0 ? 1 : -1);
}
}
cout << ans << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
571 ms |
12168 KB |
Output is correct |
12 |
Correct |
566 ms |
11704 KB |
Output is correct |
13 |
Correct |
449 ms |
10932 KB |
Output is correct |
14 |
Correct |
435 ms |
10932 KB |
Output is correct |
15 |
Correct |
919 ms |
12148 KB |
Output is correct |
16 |
Correct |
529 ms |
11124 KB |
Output is correct |
17 |
Correct |
521 ms |
11284 KB |
Output is correct |
18 |
Correct |
263 ms |
13236 KB |
Output is correct |
19 |
Correct |
372 ms |
10068 KB |
Output is correct |
20 |
Correct |
566 ms |
11604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
571 ms |
12168 KB |
Output is correct |
12 |
Correct |
566 ms |
11704 KB |
Output is correct |
13 |
Correct |
449 ms |
10932 KB |
Output is correct |
14 |
Correct |
435 ms |
10932 KB |
Output is correct |
15 |
Correct |
919 ms |
12148 KB |
Output is correct |
16 |
Correct |
529 ms |
11124 KB |
Output is correct |
17 |
Correct |
521 ms |
11284 KB |
Output is correct |
18 |
Correct |
263 ms |
13236 KB |
Output is correct |
19 |
Correct |
372 ms |
10068 KB |
Output is correct |
20 |
Correct |
566 ms |
11604 KB |
Output is correct |
21 |
Correct |
1064 ms |
13580 KB |
Output is correct |
22 |
Correct |
622 ms |
13888 KB |
Output is correct |
23 |
Correct |
610 ms |
12840 KB |
Output is correct |
24 |
Correct |
560 ms |
12628 KB |
Output is correct |
25 |
Correct |
549 ms |
14676 KB |
Output is correct |
26 |
Correct |
682 ms |
13336 KB |
Output is correct |
27 |
Correct |
691 ms |
5968 KB |
Output is correct |
28 |
Correct |
329 ms |
8540 KB |
Output is correct |
29 |
Correct |
383 ms |
4396 KB |
Output is correct |
30 |
Correct |
810 ms |
6632 KB |
Output is correct |
31 |
Correct |
1022 ms |
6516 KB |
Output is correct |
32 |
Correct |
683 ms |
6476 KB |
Output is correct |
33 |
Correct |
471 ms |
5460 KB |
Output is correct |
34 |
Correct |
559 ms |
5556 KB |
Output is correct |
35 |
Correct |
674 ms |
5896 KB |
Output is correct |
36 |
Correct |
263 ms |
4364 KB |
Output is correct |
37 |
Correct |
889 ms |
6468 KB |
Output is correct |
38 |
Correct |
400 ms |
4528 KB |
Output is correct |
39 |
Correct |
617 ms |
5596 KB |
Output is correct |
40 |
Correct |
571 ms |
5540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
64 ms |
21172 KB |
Output is correct |
12 |
Correct |
75 ms |
22512 KB |
Output is correct |
13 |
Correct |
92 ms |
22876 KB |
Output is correct |
14 |
Correct |
120 ms |
22260 KB |
Output is correct |
15 |
Correct |
75 ms |
22516 KB |
Output is correct |
16 |
Correct |
117 ms |
22516 KB |
Output is correct |
17 |
Correct |
97 ms |
22452 KB |
Output is correct |
18 |
Correct |
55 ms |
22552 KB |
Output is correct |
19 |
Correct |
62 ms |
22256 KB |
Output is correct |
20 |
Correct |
85 ms |
22444 KB |
Output is correct |
21 |
Correct |
101 ms |
22512 KB |
Output is correct |
22 |
Correct |
108 ms |
22472 KB |
Output is correct |
23 |
Correct |
73 ms |
22260 KB |
Output is correct |
24 |
Correct |
99 ms |
22432 KB |
Output is correct |
25 |
Correct |
99 ms |
22288 KB |
Output is correct |
26 |
Correct |
64 ms |
22280 KB |
Output is correct |
27 |
Correct |
72 ms |
22536 KB |
Output is correct |
28 |
Correct |
63 ms |
22280 KB |
Output is correct |
29 |
Correct |
81 ms |
22284 KB |
Output is correct |
30 |
Correct |
95 ms |
22260 KB |
Output is correct |
31 |
Correct |
68 ms |
22256 KB |
Output is correct |
32 |
Correct |
103 ms |
22708 KB |
Output is correct |
33 |
Correct |
105 ms |
22292 KB |
Output is correct |
34 |
Correct |
60 ms |
22256 KB |
Output is correct |
35 |
Correct |
97 ms |
22444 KB |
Output is correct |
36 |
Correct |
102 ms |
22460 KB |
Output is correct |
37 |
Correct |
87 ms |
22256 KB |
Output is correct |
38 |
Correct |
90 ms |
22360 KB |
Output is correct |
39 |
Correct |
96 ms |
22264 KB |
Output is correct |
40 |
Correct |
88 ms |
22256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
571 ms |
12168 KB |
Output is correct |
12 |
Correct |
566 ms |
11704 KB |
Output is correct |
13 |
Correct |
449 ms |
10932 KB |
Output is correct |
14 |
Correct |
435 ms |
10932 KB |
Output is correct |
15 |
Correct |
919 ms |
12148 KB |
Output is correct |
16 |
Correct |
529 ms |
11124 KB |
Output is correct |
17 |
Correct |
521 ms |
11284 KB |
Output is correct |
18 |
Correct |
263 ms |
13236 KB |
Output is correct |
19 |
Correct |
372 ms |
10068 KB |
Output is correct |
20 |
Correct |
566 ms |
11604 KB |
Output is correct |
21 |
Correct |
1064 ms |
13580 KB |
Output is correct |
22 |
Correct |
622 ms |
13888 KB |
Output is correct |
23 |
Correct |
610 ms |
12840 KB |
Output is correct |
24 |
Correct |
560 ms |
12628 KB |
Output is correct |
25 |
Correct |
549 ms |
14676 KB |
Output is correct |
26 |
Correct |
682 ms |
13336 KB |
Output is correct |
27 |
Correct |
691 ms |
5968 KB |
Output is correct |
28 |
Correct |
329 ms |
8540 KB |
Output is correct |
29 |
Correct |
383 ms |
4396 KB |
Output is correct |
30 |
Correct |
810 ms |
6632 KB |
Output is correct |
31 |
Correct |
1022 ms |
6516 KB |
Output is correct |
32 |
Correct |
683 ms |
6476 KB |
Output is correct |
33 |
Correct |
471 ms |
5460 KB |
Output is correct |
34 |
Correct |
559 ms |
5556 KB |
Output is correct |
35 |
Correct |
674 ms |
5896 KB |
Output is correct |
36 |
Correct |
263 ms |
4364 KB |
Output is correct |
37 |
Correct |
889 ms |
6468 KB |
Output is correct |
38 |
Correct |
400 ms |
4528 KB |
Output is correct |
39 |
Correct |
617 ms |
5596 KB |
Output is correct |
40 |
Correct |
571 ms |
5540 KB |
Output is correct |
41 |
Correct |
64 ms |
21172 KB |
Output is correct |
42 |
Correct |
75 ms |
22512 KB |
Output is correct |
43 |
Correct |
92 ms |
22876 KB |
Output is correct |
44 |
Correct |
120 ms |
22260 KB |
Output is correct |
45 |
Correct |
75 ms |
22516 KB |
Output is correct |
46 |
Correct |
117 ms |
22516 KB |
Output is correct |
47 |
Correct |
97 ms |
22452 KB |
Output is correct |
48 |
Correct |
55 ms |
22552 KB |
Output is correct |
49 |
Correct |
62 ms |
22256 KB |
Output is correct |
50 |
Correct |
85 ms |
22444 KB |
Output is correct |
51 |
Correct |
101 ms |
22512 KB |
Output is correct |
52 |
Correct |
108 ms |
22472 KB |
Output is correct |
53 |
Correct |
73 ms |
22260 KB |
Output is correct |
54 |
Correct |
99 ms |
22432 KB |
Output is correct |
55 |
Correct |
99 ms |
22288 KB |
Output is correct |
56 |
Correct |
64 ms |
22280 KB |
Output is correct |
57 |
Correct |
72 ms |
22536 KB |
Output is correct |
58 |
Correct |
63 ms |
22280 KB |
Output is correct |
59 |
Correct |
81 ms |
22284 KB |
Output is correct |
60 |
Correct |
95 ms |
22260 KB |
Output is correct |
61 |
Correct |
68 ms |
22256 KB |
Output is correct |
62 |
Correct |
103 ms |
22708 KB |
Output is correct |
63 |
Correct |
105 ms |
22292 KB |
Output is correct |
64 |
Correct |
60 ms |
22256 KB |
Output is correct |
65 |
Correct |
97 ms |
22444 KB |
Output is correct |
66 |
Correct |
102 ms |
22460 KB |
Output is correct |
67 |
Correct |
87 ms |
22256 KB |
Output is correct |
68 |
Correct |
90 ms |
22360 KB |
Output is correct |
69 |
Correct |
96 ms |
22264 KB |
Output is correct |
70 |
Correct |
88 ms |
22256 KB |
Output is correct |
71 |
Correct |
579 ms |
47088 KB |
Output is correct |
72 |
Correct |
746 ms |
47100 KB |
Output is correct |
73 |
Correct |
938 ms |
45396 KB |
Output is correct |
74 |
Correct |
1454 ms |
45460 KB |
Output is correct |
75 |
Correct |
837 ms |
47908 KB |
Output is correct |
76 |
Correct |
1518 ms |
46376 KB |
Output is correct |
77 |
Correct |
1245 ms |
46000 KB |
Output is correct |
78 |
Correct |
386 ms |
49648 KB |
Output is correct |
79 |
Correct |
563 ms |
43796 KB |
Output is correct |
80 |
Correct |
657 ms |
46832 KB |
Output is correct |
81 |
Correct |
1099 ms |
46928 KB |
Output is correct |
82 |
Correct |
1317 ms |
45608 KB |
Output is correct |
83 |
Correct |
806 ms |
44896 KB |
Output is correct |
84 |
Correct |
1220 ms |
45812 KB |
Output is correct |
85 |
Correct |
1238 ms |
46120 KB |
Output is correct |
86 |
Correct |
371 ms |
43732 KB |
Output is correct |
87 |
Correct |
689 ms |
46832 KB |
Output is correct |
88 |
Correct |
570 ms |
43996 KB |
Output is correct |
89 |
Correct |
928 ms |
45292 KB |
Output is correct |
90 |
Correct |
1216 ms |
45676 KB |
Output is correct |
91 |
Correct |
723 ms |
44852 KB |
Output is correct |
92 |
Correct |
1371 ms |
45348 KB |
Output is correct |
93 |
Correct |
1329 ms |
46108 KB |
Output is correct |
94 |
Correct |
385 ms |
43820 KB |
Output is correct |
95 |
Correct |
1073 ms |
46072 KB |
Output is correct |
96 |
Correct |
1114 ms |
45816 KB |
Output is correct |
97 |
Correct |
1052 ms |
45916 KB |
Output is correct |
98 |
Correct |
1053 ms |
45932 KB |
Output is correct |
99 |
Correct |
1088 ms |
45784 KB |
Output is correct |
100 |
Correct |
1063 ms |
45816 KB |
Output is correct |