#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)a.size());mask++) {
int y = x;
for (int j = 0;j < (int)a.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 |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2540 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 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 |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2540 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 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 |
588 ms |
17288 KB |
Output is correct |
12 |
Correct |
575 ms |
16952 KB |
Output is correct |
13 |
Correct |
435 ms |
16164 KB |
Output is correct |
14 |
Correct |
454 ms |
16280 KB |
Output is correct |
15 |
Correct |
956 ms |
17228 KB |
Output is correct |
16 |
Correct |
521 ms |
16248 KB |
Output is correct |
17 |
Correct |
543 ms |
16184 KB |
Output is correct |
18 |
Correct |
268 ms |
18268 KB |
Output is correct |
19 |
Correct |
349 ms |
15204 KB |
Output is correct |
20 |
Correct |
579 ms |
16720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2540 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 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 |
588 ms |
17288 KB |
Output is correct |
12 |
Correct |
575 ms |
16952 KB |
Output is correct |
13 |
Correct |
435 ms |
16164 KB |
Output is correct |
14 |
Correct |
454 ms |
16280 KB |
Output is correct |
15 |
Correct |
956 ms |
17228 KB |
Output is correct |
16 |
Correct |
521 ms |
16248 KB |
Output is correct |
17 |
Correct |
543 ms |
16184 KB |
Output is correct |
18 |
Correct |
268 ms |
18268 KB |
Output is correct |
19 |
Correct |
349 ms |
15204 KB |
Output is correct |
20 |
Correct |
579 ms |
16720 KB |
Output is correct |
21 |
Correct |
1043 ms |
20444 KB |
Output is correct |
22 |
Correct |
615 ms |
20308 KB |
Output is correct |
23 |
Correct |
584 ms |
19232 KB |
Output is correct |
24 |
Correct |
554 ms |
19312 KB |
Output is correct |
25 |
Correct |
542 ms |
21220 KB |
Output is correct |
26 |
Correct |
705 ms |
19740 KB |
Output is correct |
27 |
Correct |
709 ms |
19524 KB |
Output is correct |
28 |
Correct |
280 ms |
22124 KB |
Output is correct |
29 |
Correct |
373 ms |
18120 KB |
Output is correct |
30 |
Correct |
835 ms |
20264 KB |
Output is correct |
31 |
Correct |
1039 ms |
20304 KB |
Output is correct |
32 |
Correct |
715 ms |
20228 KB |
Output is correct |
33 |
Correct |
461 ms |
19076 KB |
Output is correct |
34 |
Correct |
558 ms |
19128 KB |
Output is correct |
35 |
Correct |
676 ms |
19548 KB |
Output is correct |
36 |
Correct |
263 ms |
18300 KB |
Output is correct |
37 |
Correct |
952 ms |
20196 KB |
Output is correct |
38 |
Correct |
397 ms |
18084 KB |
Output is correct |
39 |
Correct |
597 ms |
19280 KB |
Output is correct |
40 |
Correct |
578 ms |
19280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2540 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 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 |
Runtime error |
60 ms |
41968 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2540 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 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 |
588 ms |
17288 KB |
Output is correct |
12 |
Correct |
575 ms |
16952 KB |
Output is correct |
13 |
Correct |
435 ms |
16164 KB |
Output is correct |
14 |
Correct |
454 ms |
16280 KB |
Output is correct |
15 |
Correct |
956 ms |
17228 KB |
Output is correct |
16 |
Correct |
521 ms |
16248 KB |
Output is correct |
17 |
Correct |
543 ms |
16184 KB |
Output is correct |
18 |
Correct |
268 ms |
18268 KB |
Output is correct |
19 |
Correct |
349 ms |
15204 KB |
Output is correct |
20 |
Correct |
579 ms |
16720 KB |
Output is correct |
21 |
Correct |
1043 ms |
20444 KB |
Output is correct |
22 |
Correct |
615 ms |
20308 KB |
Output is correct |
23 |
Correct |
584 ms |
19232 KB |
Output is correct |
24 |
Correct |
554 ms |
19312 KB |
Output is correct |
25 |
Correct |
542 ms |
21220 KB |
Output is correct |
26 |
Correct |
705 ms |
19740 KB |
Output is correct |
27 |
Correct |
709 ms |
19524 KB |
Output is correct |
28 |
Correct |
280 ms |
22124 KB |
Output is correct |
29 |
Correct |
373 ms |
18120 KB |
Output is correct |
30 |
Correct |
835 ms |
20264 KB |
Output is correct |
31 |
Correct |
1039 ms |
20304 KB |
Output is correct |
32 |
Correct |
715 ms |
20228 KB |
Output is correct |
33 |
Correct |
461 ms |
19076 KB |
Output is correct |
34 |
Correct |
558 ms |
19128 KB |
Output is correct |
35 |
Correct |
676 ms |
19548 KB |
Output is correct |
36 |
Correct |
263 ms |
18300 KB |
Output is correct |
37 |
Correct |
952 ms |
20196 KB |
Output is correct |
38 |
Correct |
397 ms |
18084 KB |
Output is correct |
39 |
Correct |
597 ms |
19280 KB |
Output is correct |
40 |
Correct |
578 ms |
19280 KB |
Output is correct |
41 |
Runtime error |
60 ms |
41968 KB |
Execution killed with signal 11 |
42 |
Halted |
0 ms |
0 KB |
- |