#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 mask1 = 0, mask2 = 0, mask3 = 0;
reverse(all(s));
for (int i = 0; i < n; ++i) {
if (s[i] == '0') {
mask1 |= (1 << i);
}
else if (s[i] == '1') {
mask2 |= (1 << i);
}
else {
mask3 |= (1 << i);
}
}
int ans = 0, x = __builtin_popcount(mask1), y = __builtin_popcount(mask2), z = __builtin_popcount(mask3);
if (z <= 6) {
for (int mask = mask3; mask > 0; mask = (mask - 1) & mask3) {
ans += a[(mask2 | mask)];
}
ans += a[mask2];
}
else if (y <= 6) {
for (int mask = mask2; mask > 0; mask = (mask - 1) & mask2) {
if ((__builtin_popcount(mask2) - __builtin_popcount(mask)) % 2) {
ans -= sub[(mask | mask3)];
}
else {
ans += sub[(mask | mask3)];
}
}
if (__builtin_popcount(mask2) % 2) {
ans -= sub[mask3];
}
else {
ans += sub[mask3];
}
}
else {
for (int mask = mask1; mask > 0; mask = (mask - 1) & mask1) {
if (__builtin_popcount(mask) % 2) {
ans -= sup[(mask | mask2)];
}
else {
ans += sup[(mask | mask2)];
}
}
ans += sup[mask2];
}
cout << ans << '\n';
}
}
signed main() {
speed;
int T = 1;
//cin >> T;
while (T--) {
solve();
}
}
/*
НЕ ЗАХОДИТ РЕШЕНИЕ?
1) ПРОВЕРЬ НА ОЧЕВИДНЫЕ ОШИБКИ В КОДЕ
2) ПРОВЕРЬ НА ПЕРЕПОЛНЕНИЯ
3) УБЕДИСЬ В ПРАВИЛЬНОСТИ АЛГОРИТМА
*/
Compilation message
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:138:22: warning: unused variable 'x' [-Wunused-variable]
138 | int ans = 0, x = __builtin_popcount(mask1), y = __builtin_popcount(mask2), z = __builtin_popcount(mask3);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 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 |
4444 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 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 |
4444 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 |
125 ms |
14420 KB |
Output is correct |
12 |
Correct |
134 ms |
13684 KB |
Output is correct |
13 |
Correct |
152 ms |
12880 KB |
Output is correct |
14 |
Correct |
134 ms |
13012 KB |
Output is correct |
15 |
Correct |
152 ms |
14184 KB |
Output is correct |
16 |
Correct |
141 ms |
13140 KB |
Output is correct |
17 |
Correct |
159 ms |
13344 KB |
Output is correct |
18 |
Correct |
90 ms |
15208 KB |
Output is correct |
19 |
Correct |
128 ms |
12168 KB |
Output is correct |
20 |
Correct |
145 ms |
13836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 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 |
4444 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 |
125 ms |
14420 KB |
Output is correct |
12 |
Correct |
134 ms |
13684 KB |
Output is correct |
13 |
Correct |
152 ms |
12880 KB |
Output is correct |
14 |
Correct |
134 ms |
13012 KB |
Output is correct |
15 |
Correct |
152 ms |
14184 KB |
Output is correct |
16 |
Correct |
141 ms |
13140 KB |
Output is correct |
17 |
Correct |
159 ms |
13344 KB |
Output is correct |
18 |
Correct |
90 ms |
15208 KB |
Output is correct |
19 |
Correct |
128 ms |
12168 KB |
Output is correct |
20 |
Correct |
145 ms |
13836 KB |
Output is correct |
21 |
Correct |
199 ms |
16216 KB |
Output is correct |
22 |
Correct |
145 ms |
16208 KB |
Output is correct |
23 |
Correct |
167 ms |
15188 KB |
Output is correct |
24 |
Correct |
164 ms |
14860 KB |
Output is correct |
25 |
Correct |
141 ms |
16976 KB |
Output is correct |
26 |
Correct |
166 ms |
15440 KB |
Output is correct |
27 |
Correct |
169 ms |
15324 KB |
Output is correct |
28 |
Correct |
97 ms |
18000 KB |
Output is correct |
29 |
Correct |
129 ms |
14060 KB |
Output is correct |
30 |
Correct |
170 ms |
16016 KB |
Output is correct |
31 |
Correct |
179 ms |
15952 KB |
Output is correct |
32 |
Correct |
165 ms |
15840 KB |
Output is correct |
33 |
Correct |
138 ms |
14720 KB |
Output is correct |
34 |
Correct |
166 ms |
14840 KB |
Output is correct |
35 |
Correct |
203 ms |
15488 KB |
Output is correct |
36 |
Correct |
92 ms |
13840 KB |
Output is correct |
37 |
Correct |
160 ms |
15976 KB |
Output is correct |
38 |
Correct |
136 ms |
13908 KB |
Output is correct |
39 |
Correct |
156 ms |
15184 KB |
Output is correct |
40 |
Correct |
154 ms |
14928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 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 |
4444 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 |
38 ms |
27220 KB |
Output is correct |
12 |
Correct |
38 ms |
27212 KB |
Output is correct |
13 |
Correct |
42 ms |
27036 KB |
Output is correct |
14 |
Correct |
53 ms |
26960 KB |
Output is correct |
15 |
Correct |
45 ms |
27216 KB |
Output is correct |
16 |
Correct |
57 ms |
27048 KB |
Output is correct |
17 |
Correct |
46 ms |
26940 KB |
Output is correct |
18 |
Correct |
37 ms |
27404 KB |
Output is correct |
19 |
Correct |
34 ms |
27256 KB |
Output is correct |
20 |
Correct |
39 ms |
27204 KB |
Output is correct |
21 |
Correct |
40 ms |
27216 KB |
Output is correct |
22 |
Correct |
45 ms |
26944 KB |
Output is correct |
23 |
Correct |
38 ms |
26964 KB |
Output is correct |
24 |
Correct |
44 ms |
27116 KB |
Output is correct |
25 |
Correct |
42 ms |
26960 KB |
Output is correct |
26 |
Correct |
31 ms |
27108 KB |
Output is correct |
27 |
Correct |
36 ms |
27216 KB |
Output is correct |
28 |
Correct |
37 ms |
26960 KB |
Output is correct |
29 |
Correct |
40 ms |
27076 KB |
Output is correct |
30 |
Correct |
42 ms |
27476 KB |
Output is correct |
31 |
Correct |
37 ms |
26960 KB |
Output is correct |
32 |
Correct |
44 ms |
27100 KB |
Output is correct |
33 |
Correct |
39 ms |
27220 KB |
Output is correct |
34 |
Correct |
34 ms |
27000 KB |
Output is correct |
35 |
Correct |
39 ms |
27216 KB |
Output is correct |
36 |
Correct |
43 ms |
27148 KB |
Output is correct |
37 |
Correct |
42 ms |
26948 KB |
Output is correct |
38 |
Correct |
41 ms |
26960 KB |
Output is correct |
39 |
Correct |
42 ms |
26964 KB |
Output is correct |
40 |
Correct |
46 ms |
27164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 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 |
4444 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 |
125 ms |
14420 KB |
Output is correct |
12 |
Correct |
134 ms |
13684 KB |
Output is correct |
13 |
Correct |
152 ms |
12880 KB |
Output is correct |
14 |
Correct |
134 ms |
13012 KB |
Output is correct |
15 |
Correct |
152 ms |
14184 KB |
Output is correct |
16 |
Correct |
141 ms |
13140 KB |
Output is correct |
17 |
Correct |
159 ms |
13344 KB |
Output is correct |
18 |
Correct |
90 ms |
15208 KB |
Output is correct |
19 |
Correct |
128 ms |
12168 KB |
Output is correct |
20 |
Correct |
145 ms |
13836 KB |
Output is correct |
21 |
Correct |
199 ms |
16216 KB |
Output is correct |
22 |
Correct |
145 ms |
16208 KB |
Output is correct |
23 |
Correct |
167 ms |
15188 KB |
Output is correct |
24 |
Correct |
164 ms |
14860 KB |
Output is correct |
25 |
Correct |
141 ms |
16976 KB |
Output is correct |
26 |
Correct |
166 ms |
15440 KB |
Output is correct |
27 |
Correct |
169 ms |
15324 KB |
Output is correct |
28 |
Correct |
97 ms |
18000 KB |
Output is correct |
29 |
Correct |
129 ms |
14060 KB |
Output is correct |
30 |
Correct |
170 ms |
16016 KB |
Output is correct |
31 |
Correct |
179 ms |
15952 KB |
Output is correct |
32 |
Correct |
165 ms |
15840 KB |
Output is correct |
33 |
Correct |
138 ms |
14720 KB |
Output is correct |
34 |
Correct |
166 ms |
14840 KB |
Output is correct |
35 |
Correct |
203 ms |
15488 KB |
Output is correct |
36 |
Correct |
92 ms |
13840 KB |
Output is correct |
37 |
Correct |
160 ms |
15976 KB |
Output is correct |
38 |
Correct |
136 ms |
13908 KB |
Output is correct |
39 |
Correct |
156 ms |
15184 KB |
Output is correct |
40 |
Correct |
154 ms |
14928 KB |
Output is correct |
41 |
Correct |
38 ms |
27220 KB |
Output is correct |
42 |
Correct |
38 ms |
27212 KB |
Output is correct |
43 |
Correct |
42 ms |
27036 KB |
Output is correct |
44 |
Correct |
53 ms |
26960 KB |
Output is correct |
45 |
Correct |
45 ms |
27216 KB |
Output is correct |
46 |
Correct |
57 ms |
27048 KB |
Output is correct |
47 |
Correct |
46 ms |
26940 KB |
Output is correct |
48 |
Correct |
37 ms |
27404 KB |
Output is correct |
49 |
Correct |
34 ms |
27256 KB |
Output is correct |
50 |
Correct |
39 ms |
27204 KB |
Output is correct |
51 |
Correct |
40 ms |
27216 KB |
Output is correct |
52 |
Correct |
45 ms |
26944 KB |
Output is correct |
53 |
Correct |
38 ms |
26964 KB |
Output is correct |
54 |
Correct |
44 ms |
27116 KB |
Output is correct |
55 |
Correct |
42 ms |
26960 KB |
Output is correct |
56 |
Correct |
31 ms |
27108 KB |
Output is correct |
57 |
Correct |
36 ms |
27216 KB |
Output is correct |
58 |
Correct |
37 ms |
26960 KB |
Output is correct |
59 |
Correct |
40 ms |
27076 KB |
Output is correct |
60 |
Correct |
42 ms |
27476 KB |
Output is correct |
61 |
Correct |
37 ms |
26960 KB |
Output is correct |
62 |
Correct |
44 ms |
27100 KB |
Output is correct |
63 |
Correct |
39 ms |
27220 KB |
Output is correct |
64 |
Correct |
34 ms |
27000 KB |
Output is correct |
65 |
Correct |
39 ms |
27216 KB |
Output is correct |
66 |
Correct |
43 ms |
27148 KB |
Output is correct |
67 |
Correct |
42 ms |
26948 KB |
Output is correct |
68 |
Correct |
41 ms |
26960 KB |
Output is correct |
69 |
Correct |
42 ms |
26964 KB |
Output is correct |
70 |
Correct |
46 ms |
27164 KB |
Output is correct |
71 |
Correct |
229 ms |
41848 KB |
Output is correct |
72 |
Correct |
252 ms |
42292 KB |
Output is correct |
73 |
Correct |
295 ms |
40440 KB |
Output is correct |
74 |
Correct |
373 ms |
41988 KB |
Output is correct |
75 |
Correct |
250 ms |
53328 KB |
Output is correct |
76 |
Correct |
345 ms |
51540 KB |
Output is correct |
77 |
Correct |
336 ms |
51568 KB |
Output is correct |
78 |
Correct |
163 ms |
55120 KB |
Output is correct |
79 |
Correct |
239 ms |
49236 KB |
Output is correct |
80 |
Correct |
244 ms |
52376 KB |
Output is correct |
81 |
Correct |
294 ms |
52496 KB |
Output is correct |
82 |
Correct |
354 ms |
51296 KB |
Output is correct |
83 |
Correct |
248 ms |
50704 KB |
Output is correct |
84 |
Correct |
323 ms |
51284 KB |
Output is correct |
85 |
Correct |
319 ms |
51540 KB |
Output is correct |
86 |
Correct |
144 ms |
49236 KB |
Output is correct |
87 |
Correct |
210 ms |
52304 KB |
Output is correct |
88 |
Correct |
244 ms |
49336 KB |
Output is correct |
89 |
Correct |
267 ms |
50940 KB |
Output is correct |
90 |
Correct |
322 ms |
51400 KB |
Output is correct |
91 |
Correct |
244 ms |
50464 KB |
Output is correct |
92 |
Correct |
371 ms |
51756 KB |
Output is correct |
93 |
Correct |
333 ms |
51536 KB |
Output is correct |
94 |
Correct |
146 ms |
49456 KB |
Output is correct |
95 |
Correct |
283 ms |
51280 KB |
Output is correct |
96 |
Correct |
321 ms |
51280 KB |
Output is correct |
97 |
Correct |
309 ms |
51292 KB |
Output is correct |
98 |
Correct |
332 ms |
51304 KB |
Output is correct |
99 |
Correct |
288 ms |
51280 KB |
Output is correct |
100 |
Correct |
310 ms |
51280 KB |
Output is correct |