#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,lzcnt,mmx,abm,avx,avx2,fma")
using namespace std;
// using namespace __gnu_pbds;
// typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define static_assert(...);
#include <tr2/dynamic_bitset>
using custom_bitset = tr2::dynamic_bitset<__uint128_t>;
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define bigInt __int128
#define dl double long
#define fl float
#define all(s) s.begin(), s.end()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pob_front
#define ins insert
#define F first
#define S second
#define len length
const int N = 100010;
const int M = 1010;
const int LN = 131072;
const int INF = 1000000000000000000;
const int MOD = 1e9 + 7;
const int BLOCK = 500;
int binpow(int a, int b, int MOD) {
int res = 1;
while (b > 0) {
if (b & 1) {
res = res * a;
res %= MOD;
}
a = a * a;
a %= MOD;
b >>= 1;
}
return res;
}
int MMI(int a, int MOD) {
int ret = binpow(a, MOD - 2, MOD);
return ret;
}
// int mdiv(int a, int b) {
// int ret = a*MMI(b);
// ret %= MOD;
// return ret;
// }
int mmul(int a, int b) {
int ret = (a % MOD) * (b % MOD);
ret %= MOD;
return ret;
}
int madd(int a, int b) {
int ret = (a % MOD) + (b % MOD);
ret %= MOD;
return ret;
}
int msub(int a, int b) {
int ret = (a % MOD) - (b % MOD);
ret += MOD;
ret %= MOD;
return ret;
}
int GCD(int a, int b) {
if (b == 0) {
return a;
}
return GCD(b, a % b);
}
int LCM(int a, int b) {
return a*b / GCD(a, b);
}
struct pqComp
{
bool operator()(const pair<int, int>& p1, const pair<int, int>& p2) const
{
return (p1.F < p2.F) || (p1.F == p2.F && p1.S < p2.S);
}
};
bool pCompF(pair<int, int>& p1, pair<int, int>& p2)
{
return p1.F < p2.F;
}
bool pCompS(pair<int, int>& p1, pair<int, int>& p2)
{
return p1.S < p2.S;
}
bool pCompFS(pair<int, int>& p1, pair<int, int>& p2)
{
return (p1.S < p2.S) || (p1.S == p2.S && p1.F < p2.F);
}
vector <pair<int, int>> DS {{1, 0}, {0, 1}};//{{0, -1}, {1, 0}, {0, 1}, {-1, 0}};//, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
int n, q, a[11*N], sub[11*N], sup[11*N];
void solve() {
cin >> n >> q;
for (int i = 0; i < (1 << n); ++i) {
char c; cin >> c;
a[i] = (c - '0');
}
for (int i = 0; i < (1 << n); ++i) {
sub[i] = a[i];
sup[i] = a[i];
}
for (int i = 0; i < n; ++i) {
for (int mask = 0; mask < (1 << n); ++mask) {
if (((mask >> i) & 1)) {
sub[mask] += sub[mask - (1 << i)];
}
}
}
for (int i = 0; i < n; ++i) {
for (int mask = (1 << n) - 1; mask >= 0; --mask) {
if (!((mask >> i) & 1)) {
sup[mask] += sup[mask + (1 << i)];
}
}
}
while (q--) {
string s; cin >> s;
int x = 0, y = 0, z = 0;
reverse(all(s));
int mask = 0;
vector<int> vx, vy, vz;
for (int i = 0; i < n; ++i) {
if (s[i] == '?') {
vx.pub(i);
++x;
}
else if (s[i] == '1') {
vy.pub(i);
++y;
}
else {
vz.pub(i);
++z;
}
if (s[i] != '?') {
mask ^= (1 << i)*(s[i] - '0');
}
}
int ans = 0;
if (x <= 6) {
for (int mask1 = 0; mask1 < (1 << x); ++mask1) {
int nmask = mask;
for (int i = 0; i < x; ++i) {
if ((mask1 >> i) & 1) {
nmask ^= (1 << vx[i]);
}
}
ans += a[nmask];
}
}
else if (y <= 6) {
for (int i: vx) {
mask += (1 << i);
}
for (int mask1 = 0; mask1 < (1 << y); ++mask1) {
int nmask = mask;
for (int i = 0; i < y; ++i) {
if ((mask1 >> i) & 1) {
nmask ^= (1 << vy[i]);
}
}
if (__builtin_popcount(mask1) % 2) {
ans -= sub[nmask];
}
else {
ans += sub[nmask];
}
}
}
else {
for (int mask1 = 0; mask1 < (1 << z); ++mask1) {
int nmask = mask;
for (int i = 0; i < z; ++i) {
if ((mask1 >> i) & 1) {
nmask ^= (1 << vz[i]);
}
}
if (__builtin_popcount(mask1) % 2) {
ans -= sup[nmask];
}
else {
ans += sup[nmask];
}
}
}
cout << ans << '\n';
}
}
signed main() {
speed;
int T = 1;
//cin >> T;
while (T--) {
solve();
}
}
/*
НЕ ЗАХОДИТ РЕШЕНИЕ?
1) ПРОВЕРЬ НА ОЧЕВИДНЫЕ ОШИБКИ В КОДЕ
2) ПРОВЕРЬ НА ПЕРЕПОЛНЕНИЯ
3) УБЕДИСЬ В ПРАВИЛЬНОСТИ АЛГОРИТМА
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4448 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4596 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4448 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4596 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
405 ms |
14452 KB |
Output is correct |
12 |
Correct |
412 ms |
14164 KB |
Output is correct |
13 |
Correct |
354 ms |
13464 KB |
Output is correct |
14 |
Correct |
322 ms |
13532 KB |
Output is correct |
15 |
Correct |
526 ms |
14556 KB |
Output is correct |
16 |
Correct |
429 ms |
13656 KB |
Output is correct |
17 |
Correct |
476 ms |
13816 KB |
Output is correct |
18 |
Correct |
257 ms |
13536 KB |
Output is correct |
19 |
Correct |
372 ms |
12580 KB |
Output is correct |
20 |
Correct |
404 ms |
14160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4448 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4596 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
405 ms |
14452 KB |
Output is correct |
12 |
Correct |
412 ms |
14164 KB |
Output is correct |
13 |
Correct |
354 ms |
13464 KB |
Output is correct |
14 |
Correct |
322 ms |
13532 KB |
Output is correct |
15 |
Correct |
526 ms |
14556 KB |
Output is correct |
16 |
Correct |
429 ms |
13656 KB |
Output is correct |
17 |
Correct |
476 ms |
13816 KB |
Output is correct |
18 |
Correct |
257 ms |
13536 KB |
Output is correct |
19 |
Correct |
372 ms |
12580 KB |
Output is correct |
20 |
Correct |
404 ms |
14160 KB |
Output is correct |
21 |
Correct |
664 ms |
15932 KB |
Output is correct |
22 |
Correct |
458 ms |
16152 KB |
Output is correct |
23 |
Correct |
498 ms |
15084 KB |
Output is correct |
24 |
Correct |
464 ms |
14904 KB |
Output is correct |
25 |
Correct |
349 ms |
17032 KB |
Output is correct |
26 |
Correct |
473 ms |
15440 KB |
Output is correct |
27 |
Correct |
515 ms |
15512 KB |
Output is correct |
28 |
Correct |
221 ms |
17896 KB |
Output is correct |
29 |
Correct |
282 ms |
14024 KB |
Output is correct |
30 |
Correct |
594 ms |
16100 KB |
Output is correct |
31 |
Correct |
654 ms |
16212 KB |
Output is correct |
32 |
Correct |
429 ms |
15960 KB |
Output is correct |
33 |
Correct |
347 ms |
14892 KB |
Output is correct |
34 |
Correct |
457 ms |
14988 KB |
Output is correct |
35 |
Correct |
525 ms |
15404 KB |
Output is correct |
36 |
Correct |
184 ms |
14164 KB |
Output is correct |
37 |
Correct |
630 ms |
15804 KB |
Output is correct |
38 |
Correct |
332 ms |
14160 KB |
Output is correct |
39 |
Correct |
444 ms |
15212 KB |
Output is correct |
40 |
Correct |
399 ms |
14836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4448 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4596 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
45 ms |
27468 KB |
Output is correct |
12 |
Correct |
56 ms |
27552 KB |
Output is correct |
13 |
Correct |
65 ms |
27472 KB |
Output is correct |
14 |
Correct |
71 ms |
27524 KB |
Output is correct |
15 |
Correct |
52 ms |
27588 KB |
Output is correct |
16 |
Correct |
93 ms |
27460 KB |
Output is correct |
17 |
Correct |
86 ms |
27472 KB |
Output is correct |
18 |
Correct |
37 ms |
27984 KB |
Output is correct |
19 |
Correct |
53 ms |
27412 KB |
Output is correct |
20 |
Correct |
48 ms |
27732 KB |
Output is correct |
21 |
Correct |
71 ms |
27544 KB |
Output is correct |
22 |
Correct |
76 ms |
27480 KB |
Output is correct |
23 |
Correct |
58 ms |
27472 KB |
Output is correct |
24 |
Correct |
84 ms |
27520 KB |
Output is correct |
25 |
Correct |
64 ms |
27564 KB |
Output is correct |
26 |
Correct |
56 ms |
27456 KB |
Output is correct |
27 |
Correct |
63 ms |
27540 KB |
Output is correct |
28 |
Correct |
65 ms |
27480 KB |
Output is correct |
29 |
Correct |
62 ms |
27472 KB |
Output is correct |
30 |
Correct |
63 ms |
27536 KB |
Output is correct |
31 |
Correct |
59 ms |
27436 KB |
Output is correct |
32 |
Correct |
76 ms |
27408 KB |
Output is correct |
33 |
Correct |
65 ms |
27520 KB |
Output is correct |
34 |
Correct |
44 ms |
27476 KB |
Output is correct |
35 |
Correct |
71 ms |
27468 KB |
Output is correct |
36 |
Correct |
95 ms |
27584 KB |
Output is correct |
37 |
Correct |
60 ms |
27364 KB |
Output is correct |
38 |
Correct |
83 ms |
27428 KB |
Output is correct |
39 |
Correct |
66 ms |
27472 KB |
Output is correct |
40 |
Correct |
60 ms |
27440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4448 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4596 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
405 ms |
14452 KB |
Output is correct |
12 |
Correct |
412 ms |
14164 KB |
Output is correct |
13 |
Correct |
354 ms |
13464 KB |
Output is correct |
14 |
Correct |
322 ms |
13532 KB |
Output is correct |
15 |
Correct |
526 ms |
14556 KB |
Output is correct |
16 |
Correct |
429 ms |
13656 KB |
Output is correct |
17 |
Correct |
476 ms |
13816 KB |
Output is correct |
18 |
Correct |
257 ms |
13536 KB |
Output is correct |
19 |
Correct |
372 ms |
12580 KB |
Output is correct |
20 |
Correct |
404 ms |
14160 KB |
Output is correct |
21 |
Correct |
664 ms |
15932 KB |
Output is correct |
22 |
Correct |
458 ms |
16152 KB |
Output is correct |
23 |
Correct |
498 ms |
15084 KB |
Output is correct |
24 |
Correct |
464 ms |
14904 KB |
Output is correct |
25 |
Correct |
349 ms |
17032 KB |
Output is correct |
26 |
Correct |
473 ms |
15440 KB |
Output is correct |
27 |
Correct |
515 ms |
15512 KB |
Output is correct |
28 |
Correct |
221 ms |
17896 KB |
Output is correct |
29 |
Correct |
282 ms |
14024 KB |
Output is correct |
30 |
Correct |
594 ms |
16100 KB |
Output is correct |
31 |
Correct |
654 ms |
16212 KB |
Output is correct |
32 |
Correct |
429 ms |
15960 KB |
Output is correct |
33 |
Correct |
347 ms |
14892 KB |
Output is correct |
34 |
Correct |
457 ms |
14988 KB |
Output is correct |
35 |
Correct |
525 ms |
15404 KB |
Output is correct |
36 |
Correct |
184 ms |
14164 KB |
Output is correct |
37 |
Correct |
630 ms |
15804 KB |
Output is correct |
38 |
Correct |
332 ms |
14160 KB |
Output is correct |
39 |
Correct |
444 ms |
15212 KB |
Output is correct |
40 |
Correct |
399 ms |
14836 KB |
Output is correct |
41 |
Correct |
45 ms |
27468 KB |
Output is correct |
42 |
Correct |
56 ms |
27552 KB |
Output is correct |
43 |
Correct |
65 ms |
27472 KB |
Output is correct |
44 |
Correct |
71 ms |
27524 KB |
Output is correct |
45 |
Correct |
52 ms |
27588 KB |
Output is correct |
46 |
Correct |
93 ms |
27460 KB |
Output is correct |
47 |
Correct |
86 ms |
27472 KB |
Output is correct |
48 |
Correct |
37 ms |
27984 KB |
Output is correct |
49 |
Correct |
53 ms |
27412 KB |
Output is correct |
50 |
Correct |
48 ms |
27732 KB |
Output is correct |
51 |
Correct |
71 ms |
27544 KB |
Output is correct |
52 |
Correct |
76 ms |
27480 KB |
Output is correct |
53 |
Correct |
58 ms |
27472 KB |
Output is correct |
54 |
Correct |
84 ms |
27520 KB |
Output is correct |
55 |
Correct |
64 ms |
27564 KB |
Output is correct |
56 |
Correct |
56 ms |
27456 KB |
Output is correct |
57 |
Correct |
63 ms |
27540 KB |
Output is correct |
58 |
Correct |
65 ms |
27480 KB |
Output is correct |
59 |
Correct |
62 ms |
27472 KB |
Output is correct |
60 |
Correct |
63 ms |
27536 KB |
Output is correct |
61 |
Correct |
59 ms |
27436 KB |
Output is correct |
62 |
Correct |
76 ms |
27408 KB |
Output is correct |
63 |
Correct |
65 ms |
27520 KB |
Output is correct |
64 |
Correct |
44 ms |
27476 KB |
Output is correct |
65 |
Correct |
71 ms |
27468 KB |
Output is correct |
66 |
Correct |
95 ms |
27584 KB |
Output is correct |
67 |
Correct |
60 ms |
27364 KB |
Output is correct |
68 |
Correct |
83 ms |
27428 KB |
Output is correct |
69 |
Correct |
66 ms |
27472 KB |
Output is correct |
70 |
Correct |
60 ms |
27440 KB |
Output is correct |
71 |
Correct |
460 ms |
42364 KB |
Output is correct |
72 |
Correct |
544 ms |
42556 KB |
Output is correct |
73 |
Correct |
657 ms |
40980 KB |
Output is correct |
74 |
Correct |
906 ms |
41192 KB |
Output is correct |
75 |
Correct |
562 ms |
42700 KB |
Output is correct |
76 |
Correct |
882 ms |
41044 KB |
Output is correct |
77 |
Correct |
884 ms |
40868 KB |
Output is correct |
78 |
Correct |
311 ms |
44884 KB |
Output is correct |
79 |
Correct |
436 ms |
38872 KB |
Output is correct |
80 |
Correct |
491 ms |
41808 KB |
Output is correct |
81 |
Correct |
740 ms |
41684 KB |
Output is correct |
82 |
Correct |
949 ms |
40704 KB |
Output is correct |
83 |
Correct |
577 ms |
40016 KB |
Output is correct |
84 |
Correct |
962 ms |
40768 KB |
Output is correct |
85 |
Correct |
859 ms |
40880 KB |
Output is correct |
86 |
Correct |
288 ms |
38728 KB |
Output is correct |
87 |
Correct |
484 ms |
41812 KB |
Output is correct |
88 |
Correct |
505 ms |
38652 KB |
Output is correct |
89 |
Correct |
682 ms |
40340 KB |
Output is correct |
90 |
Correct |
868 ms |
40876 KB |
Output is correct |
91 |
Correct |
567 ms |
39940 KB |
Output is correct |
92 |
Correct |
903 ms |
41044 KB |
Output is correct |
93 |
Correct |
845 ms |
30004 KB |
Output is correct |
94 |
Correct |
242 ms |
27728 KB |
Output is correct |
95 |
Correct |
631 ms |
29780 KB |
Output is correct |
96 |
Correct |
606 ms |
29780 KB |
Output is correct |
97 |
Correct |
623 ms |
30036 KB |
Output is correct |
98 |
Correct |
621 ms |
29796 KB |
Output is correct |
99 |
Correct |
647 ms |
30036 KB |
Output is correct |
100 |
Correct |
633 ms |
29784 KB |
Output is correct |