#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb(x) push_back(x)
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define un(x) (x).erase(unique(all(x)), x.end())
typedef long long ll;
typedef double db;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector <ll> V;
const int N = 1e7 + 1;
const int M = (1 << 20);
const int mod = 1e9 + 7;
const int inf = 1061109567;
const int block = 490;
template <class T1, class Gz> void add(T1 &x, Gz y) { x += y; if (x < 0) x += mod; if (x >= mod) x -= mod; }
template <class T1, class Gz> bool maximize(T1 &x, Gz y) { if (x < y) { x = y; return true; } return false; }
template <class T1, class Gz> bool minimize(T1 &x, Gz y) { if (x > y) { x = y; return true; } return false; }
int n, q, a[M];
ll dp[2][M];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> q;
for(int i = 0; i < (1 << n); i++) {
char x; cin >> x;
a[i] = x - '0';
for(int j = 0; j < 2; j++)
dp[j][i] = a[i];
}
for(int i = 0; i < n; i++) {
for(int mask = 0; mask < (1 << n); mask++) {
if (mask >> i & 1) {
dp[0][mask ^ (1 << i)] += dp[0][mask];
dp[1][mask] += dp[1][mask ^ (1 << i)];
}
}
}
for(int i = 1; i <= q; i++) {
string s; cin >> s;
reverse(all(s));
vector <int> mask(3, 0);
for(int j = 0; j < n; j++) {
char x = s[j];
if (x == '0') mask[0] ^= (1 << j);
if (x == '1') mask[1] ^= (1 << j);
if (x == '?') mask[2] ^= (1 << j);
}
ll ans = 0;
if (__builtin_popcount(mask[2]) <= 6) {
ans = a[mask[1]];
for(int mask1 = mask[2]; mask1; mask1 = (mask1 - 1) & mask[2]) {
ans += a[mask[1] ^ mask1];
}
} else {
if (__builtin_popcount(mask[0]) <= 6) {
ans = dp[0][mask[1]];
for(int mask1 = mask[0]; mask1; mask1 = (mask1 - 1) & mask[0]) {
if (__builtin_popcount(mask1) & 1) {
ans -= dp[0][mask[1] ^ mask1];
} else {
ans += dp[0][mask[1] ^ mask1];
}
}
} else {
ans = dp[1][mask[1] ^ mask[2]];
for(int mask1 = mask[1]; mask1; mask1 = (mask1 - 1) & mask[1]) {
if (__builtin_popcount(mask1) & 1) {
ans -= dp[1][mask1 ^ mask[2]];
} else {
ans += dp[1][mask1 ^ mask[2]];
}
}
}
} cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
198 ms |
8580 KB |
Output is correct |
12 |
Correct |
205 ms |
8008 KB |
Output is correct |
13 |
Correct |
189 ms |
7444 KB |
Output is correct |
14 |
Correct |
167 ms |
7496 KB |
Output is correct |
15 |
Correct |
234 ms |
8416 KB |
Output is correct |
16 |
Correct |
237 ms |
7752 KB |
Output is correct |
17 |
Correct |
226 ms |
7496 KB |
Output is correct |
18 |
Correct |
112 ms |
9544 KB |
Output is correct |
19 |
Correct |
140 ms |
6432 KB |
Output is correct |
20 |
Correct |
202 ms |
8008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
198 ms |
8580 KB |
Output is correct |
12 |
Correct |
205 ms |
8008 KB |
Output is correct |
13 |
Correct |
189 ms |
7444 KB |
Output is correct |
14 |
Correct |
167 ms |
7496 KB |
Output is correct |
15 |
Correct |
234 ms |
8416 KB |
Output is correct |
16 |
Correct |
237 ms |
7752 KB |
Output is correct |
17 |
Correct |
226 ms |
7496 KB |
Output is correct |
18 |
Correct |
112 ms |
9544 KB |
Output is correct |
19 |
Correct |
140 ms |
6432 KB |
Output is correct |
20 |
Correct |
202 ms |
8008 KB |
Output is correct |
21 |
Correct |
181 ms |
8520 KB |
Output is correct |
22 |
Correct |
283 ms |
8776 KB |
Output is correct |
23 |
Correct |
229 ms |
7772 KB |
Output is correct |
24 |
Correct |
210 ms |
7496 KB |
Output is correct |
25 |
Correct |
228 ms |
9544 KB |
Output is correct |
26 |
Correct |
235 ms |
8112 KB |
Output is correct |
27 |
Correct |
242 ms |
8012 KB |
Output is correct |
28 |
Correct |
130 ms |
10492 KB |
Output is correct |
29 |
Correct |
173 ms |
6428 KB |
Output is correct |
30 |
Correct |
213 ms |
8776 KB |
Output is correct |
31 |
Correct |
270 ms |
8500 KB |
Output is correct |
32 |
Correct |
260 ms |
8500 KB |
Output is correct |
33 |
Correct |
194 ms |
7496 KB |
Output is correct |
34 |
Correct |
214 ms |
7496 KB |
Output is correct |
35 |
Correct |
234 ms |
7916 KB |
Output is correct |
36 |
Correct |
118 ms |
6540 KB |
Output is correct |
37 |
Correct |
228 ms |
8532 KB |
Output is correct |
38 |
Correct |
172 ms |
6468 KB |
Output is correct |
39 |
Correct |
206 ms |
7752 KB |
Output is correct |
40 |
Correct |
201 ms |
7412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
51 ms |
21064 KB |
Output is correct |
12 |
Correct |
47 ms |
20972 KB |
Output is correct |
13 |
Incorrect |
57 ms |
21064 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
198 ms |
8580 KB |
Output is correct |
12 |
Correct |
205 ms |
8008 KB |
Output is correct |
13 |
Correct |
189 ms |
7444 KB |
Output is correct |
14 |
Correct |
167 ms |
7496 KB |
Output is correct |
15 |
Correct |
234 ms |
8416 KB |
Output is correct |
16 |
Correct |
237 ms |
7752 KB |
Output is correct |
17 |
Correct |
226 ms |
7496 KB |
Output is correct |
18 |
Correct |
112 ms |
9544 KB |
Output is correct |
19 |
Correct |
140 ms |
6432 KB |
Output is correct |
20 |
Correct |
202 ms |
8008 KB |
Output is correct |
21 |
Correct |
181 ms |
8520 KB |
Output is correct |
22 |
Correct |
283 ms |
8776 KB |
Output is correct |
23 |
Correct |
229 ms |
7772 KB |
Output is correct |
24 |
Correct |
210 ms |
7496 KB |
Output is correct |
25 |
Correct |
228 ms |
9544 KB |
Output is correct |
26 |
Correct |
235 ms |
8112 KB |
Output is correct |
27 |
Correct |
242 ms |
8012 KB |
Output is correct |
28 |
Correct |
130 ms |
10492 KB |
Output is correct |
29 |
Correct |
173 ms |
6428 KB |
Output is correct |
30 |
Correct |
213 ms |
8776 KB |
Output is correct |
31 |
Correct |
270 ms |
8500 KB |
Output is correct |
32 |
Correct |
260 ms |
8500 KB |
Output is correct |
33 |
Correct |
194 ms |
7496 KB |
Output is correct |
34 |
Correct |
214 ms |
7496 KB |
Output is correct |
35 |
Correct |
234 ms |
7916 KB |
Output is correct |
36 |
Correct |
118 ms |
6540 KB |
Output is correct |
37 |
Correct |
228 ms |
8532 KB |
Output is correct |
38 |
Correct |
172 ms |
6468 KB |
Output is correct |
39 |
Correct |
206 ms |
7752 KB |
Output is correct |
40 |
Correct |
201 ms |
7412 KB |
Output is correct |
41 |
Correct |
51 ms |
21064 KB |
Output is correct |
42 |
Correct |
47 ms |
20972 KB |
Output is correct |
43 |
Incorrect |
57 ms |
21064 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |