#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#ifdef _DEBUG
#define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
#define dout(x)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define uid(a,b) uniform_int_distribution<int>(a,b)(rng)
#define ins insert
#define ssize(x) (int((x).size()))
#define bs(args...) binary_search(args)
#define lb(args...) lower_bound(args)
#define ub(args...) upper_bound(args)
#define all(x) (x).begin(),(x).end()
#define mp(a, b) make_pair(a, b)
#define mt(args...) make_tuple(args)
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define die exit(0)
template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp>
using pq = std::priority_queue<T, vc<T>, Comp>;
template<typename T>
using maxpq = pq<T, less<T>>;
template<typename T>
using minpq = pq<T, greater<T>>;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vc<int>;
using vll = vc<ll>;
using vpi = vc<pi>;
using vpll = vc<pll>;
using str = string;
constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int inf = 0x3f3f3f3f;
constexpr ll llinf = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------
const int N = 20;
const int MM = 1<<N;
int n, q, ar[MM], sub[MM], sup[MM];
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cout << fixed; clog << fixed; clog << unitbuf;
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("debug.txt", "w", stderr);
#else
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
#endif
cin >> n >> q;
for(int i = 0; i < (1<<n); ++i) {
char c;
cin >> c;
sub[i] = sup[i] = ar[i] = c-'0';
}
for(int i = 0; i < n; ++i){
for(int j = 1; j < (1<<n); ++j)
if((j>>i)&1)
sub[j] += sub[j^(1<<i)];
for(int j = (1<<n)-1; j >= 0; --j)
if((j>>i)&1^1)
sup[j] += sup[j^(1<<i)];
}
#ifdef _DEBUG
clog << "Line " << __LINE__ << ":\n";
for(int i = 0; i < (1<<n); ++i)
clog << bitset<N>(i) << sp << sub[i] << el;
clog << el;
#endif
#ifdef _DEBUG
clog << "Line " << __LINE__ << ":\n";
for(int i = 0; i < (1<<n); ++i)
clog << bitset<N>(i) << sp << sup[i] << el;
clog << el;
#endif
while(q--){
str s;
cin >> s;
reverse(all(s));
int a = 0, b = 0, c = 0, a_cnt = 0, b_cnt = 0, c_cnt = 0;
for(int i = 0; i < n; ++i){
if(s[i]=='0') {
a |= 1 << i;
++a_cnt;
}else if(s[i]=='1') {
b |= 1 << i;
++b_cnt;
}else {
c |= 1 << i;
++c_cnt;
}
}
ll ans = 0;
if(a_cnt == min({a_cnt, b_cnt, c_cnt})){
for(int x = a;; x = (x-1)&a) {
if (__builtin_popcount(x) & 1)
ans -= sup[b | x];
else
ans += sup[b | x];
if(x==0)
break;
}
}else if(b_cnt==min({a_cnt, b_cnt, c_cnt})){
for(int x = b; ; x = (x-1)&b){
if((b_cnt-__builtin_popcount(x))&1)
ans -= sub[c|x];
else
ans += sub[c|x];
if(x==0)
break;
}
}else{
for(int x = c; ; x = (x-1)&c){
ans += ar[b|x];
if(x==0)
break;
}
}
cout << ans << el;
}
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:90:22: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
90 | if((j>>i)&1^1)
| ~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
193 ms |
4320 KB |
Output is correct |
12 |
Correct |
211 ms |
3976 KB |
Output is correct |
13 |
Correct |
227 ms |
3352 KB |
Output is correct |
14 |
Correct |
282 ms |
3380 KB |
Output is correct |
15 |
Correct |
219 ms |
4416 KB |
Output is correct |
16 |
Correct |
245 ms |
3472 KB |
Output is correct |
17 |
Correct |
263 ms |
3404 KB |
Output is correct |
18 |
Correct |
151 ms |
5332 KB |
Output is correct |
19 |
Correct |
202 ms |
2340 KB |
Output is correct |
20 |
Correct |
203 ms |
3992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
193 ms |
4320 KB |
Output is correct |
12 |
Correct |
211 ms |
3976 KB |
Output is correct |
13 |
Correct |
227 ms |
3352 KB |
Output is correct |
14 |
Correct |
282 ms |
3380 KB |
Output is correct |
15 |
Correct |
219 ms |
4416 KB |
Output is correct |
16 |
Correct |
245 ms |
3472 KB |
Output is correct |
17 |
Correct |
263 ms |
3404 KB |
Output is correct |
18 |
Correct |
151 ms |
5332 KB |
Output is correct |
19 |
Correct |
202 ms |
2340 KB |
Output is correct |
20 |
Correct |
203 ms |
3992 KB |
Output is correct |
21 |
Correct |
251 ms |
4428 KB |
Output is correct |
22 |
Correct |
217 ms |
4548 KB |
Output is correct |
23 |
Correct |
310 ms |
3600 KB |
Output is correct |
24 |
Correct |
328 ms |
3364 KB |
Output is correct |
25 |
Correct |
231 ms |
5452 KB |
Output is correct |
26 |
Correct |
313 ms |
3848 KB |
Output is correct |
27 |
Correct |
290 ms |
3884 KB |
Output is correct |
28 |
Correct |
179 ms |
6364 KB |
Output is correct |
29 |
Correct |
217 ms |
2340 KB |
Output is correct |
30 |
Correct |
219 ms |
4528 KB |
Output is correct |
31 |
Correct |
280 ms |
4516 KB |
Output is correct |
32 |
Correct |
293 ms |
4400 KB |
Output is correct |
33 |
Correct |
276 ms |
3276 KB |
Output is correct |
34 |
Correct |
299 ms |
3500 KB |
Output is correct |
35 |
Correct |
294 ms |
3864 KB |
Output is correct |
36 |
Correct |
190 ms |
2380 KB |
Output is correct |
37 |
Correct |
212 ms |
4360 KB |
Output is correct |
38 |
Correct |
248 ms |
2380 KB |
Output is correct |
39 |
Correct |
254 ms |
3600 KB |
Output is correct |
40 |
Correct |
348 ms |
3404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
70 ms |
12872 KB |
Output is correct |
12 |
Correct |
79 ms |
12872 KB |
Output is correct |
13 |
Correct |
92 ms |
12744 KB |
Output is correct |
14 |
Correct |
88 ms |
12872 KB |
Output is correct |
15 |
Correct |
68 ms |
12868 KB |
Output is correct |
16 |
Correct |
96 ms |
12868 KB |
Output is correct |
17 |
Correct |
126 ms |
12744 KB |
Output is correct |
18 |
Correct |
72 ms |
12988 KB |
Output is correct |
19 |
Correct |
76 ms |
12616 KB |
Output is correct |
20 |
Correct |
69 ms |
12860 KB |
Output is correct |
21 |
Correct |
79 ms |
12872 KB |
Output is correct |
22 |
Correct |
98 ms |
12740 KB |
Output is correct |
23 |
Correct |
78 ms |
12748 KB |
Output is correct |
24 |
Correct |
95 ms |
12748 KB |
Output is correct |
25 |
Correct |
95 ms |
12744 KB |
Output is correct |
26 |
Correct |
80 ms |
12628 KB |
Output is correct |
27 |
Correct |
76 ms |
12872 KB |
Output is correct |
28 |
Correct |
74 ms |
12680 KB |
Output is correct |
29 |
Correct |
83 ms |
12720 KB |
Output is correct |
30 |
Correct |
86 ms |
12760 KB |
Output is correct |
31 |
Correct |
84 ms |
12712 KB |
Output is correct |
32 |
Correct |
95 ms |
12752 KB |
Output is correct |
33 |
Correct |
114 ms |
12744 KB |
Output is correct |
34 |
Correct |
71 ms |
12616 KB |
Output is correct |
35 |
Correct |
96 ms |
12780 KB |
Output is correct |
36 |
Correct |
85 ms |
12744 KB |
Output is correct |
37 |
Correct |
93 ms |
12720 KB |
Output is correct |
38 |
Correct |
96 ms |
12776 KB |
Output is correct |
39 |
Correct |
91 ms |
12744 KB |
Output is correct |
40 |
Correct |
81 ms |
12756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
193 ms |
4320 KB |
Output is correct |
12 |
Correct |
211 ms |
3976 KB |
Output is correct |
13 |
Correct |
227 ms |
3352 KB |
Output is correct |
14 |
Correct |
282 ms |
3380 KB |
Output is correct |
15 |
Correct |
219 ms |
4416 KB |
Output is correct |
16 |
Correct |
245 ms |
3472 KB |
Output is correct |
17 |
Correct |
263 ms |
3404 KB |
Output is correct |
18 |
Correct |
151 ms |
5332 KB |
Output is correct |
19 |
Correct |
202 ms |
2340 KB |
Output is correct |
20 |
Correct |
203 ms |
3992 KB |
Output is correct |
21 |
Correct |
251 ms |
4428 KB |
Output is correct |
22 |
Correct |
217 ms |
4548 KB |
Output is correct |
23 |
Correct |
310 ms |
3600 KB |
Output is correct |
24 |
Correct |
328 ms |
3364 KB |
Output is correct |
25 |
Correct |
231 ms |
5452 KB |
Output is correct |
26 |
Correct |
313 ms |
3848 KB |
Output is correct |
27 |
Correct |
290 ms |
3884 KB |
Output is correct |
28 |
Correct |
179 ms |
6364 KB |
Output is correct |
29 |
Correct |
217 ms |
2340 KB |
Output is correct |
30 |
Correct |
219 ms |
4528 KB |
Output is correct |
31 |
Correct |
280 ms |
4516 KB |
Output is correct |
32 |
Correct |
293 ms |
4400 KB |
Output is correct |
33 |
Correct |
276 ms |
3276 KB |
Output is correct |
34 |
Correct |
299 ms |
3500 KB |
Output is correct |
35 |
Correct |
294 ms |
3864 KB |
Output is correct |
36 |
Correct |
190 ms |
2380 KB |
Output is correct |
37 |
Correct |
212 ms |
4360 KB |
Output is correct |
38 |
Correct |
248 ms |
2380 KB |
Output is correct |
39 |
Correct |
254 ms |
3600 KB |
Output is correct |
40 |
Correct |
348 ms |
3404 KB |
Output is correct |
41 |
Correct |
70 ms |
12872 KB |
Output is correct |
42 |
Correct |
79 ms |
12872 KB |
Output is correct |
43 |
Correct |
92 ms |
12744 KB |
Output is correct |
44 |
Correct |
88 ms |
12872 KB |
Output is correct |
45 |
Correct |
68 ms |
12868 KB |
Output is correct |
46 |
Correct |
96 ms |
12868 KB |
Output is correct |
47 |
Correct |
126 ms |
12744 KB |
Output is correct |
48 |
Correct |
72 ms |
12988 KB |
Output is correct |
49 |
Correct |
76 ms |
12616 KB |
Output is correct |
50 |
Correct |
69 ms |
12860 KB |
Output is correct |
51 |
Correct |
79 ms |
12872 KB |
Output is correct |
52 |
Correct |
98 ms |
12740 KB |
Output is correct |
53 |
Correct |
78 ms |
12748 KB |
Output is correct |
54 |
Correct |
95 ms |
12748 KB |
Output is correct |
55 |
Correct |
95 ms |
12744 KB |
Output is correct |
56 |
Correct |
80 ms |
12628 KB |
Output is correct |
57 |
Correct |
76 ms |
12872 KB |
Output is correct |
58 |
Correct |
74 ms |
12680 KB |
Output is correct |
59 |
Correct |
83 ms |
12720 KB |
Output is correct |
60 |
Correct |
86 ms |
12760 KB |
Output is correct |
61 |
Correct |
84 ms |
12712 KB |
Output is correct |
62 |
Correct |
95 ms |
12752 KB |
Output is correct |
63 |
Correct |
114 ms |
12744 KB |
Output is correct |
64 |
Correct |
71 ms |
12616 KB |
Output is correct |
65 |
Correct |
96 ms |
12780 KB |
Output is correct |
66 |
Correct |
85 ms |
12744 KB |
Output is correct |
67 |
Correct |
93 ms |
12720 KB |
Output is correct |
68 |
Correct |
96 ms |
12776 KB |
Output is correct |
69 |
Correct |
91 ms |
12744 KB |
Output is correct |
70 |
Correct |
81 ms |
12756 KB |
Output is correct |
71 |
Correct |
452 ms |
17552 KB |
Output is correct |
72 |
Correct |
398 ms |
17760 KB |
Output is correct |
73 |
Correct |
575 ms |
16228 KB |
Output is correct |
74 |
Correct |
748 ms |
16580 KB |
Output is correct |
75 |
Correct |
402 ms |
18636 KB |
Output is correct |
76 |
Correct |
852 ms |
16948 KB |
Output is correct |
77 |
Correct |
796 ms |
16764 KB |
Output is correct |
78 |
Correct |
278 ms |
20556 KB |
Output is correct |
79 |
Correct |
435 ms |
14612 KB |
Output is correct |
80 |
Correct |
456 ms |
17768 KB |
Output is correct |
81 |
Correct |
639 ms |
17700 KB |
Output is correct |
82 |
Correct |
825 ms |
16604 KB |
Output is correct |
83 |
Correct |
435 ms |
15760 KB |
Output is correct |
84 |
Correct |
1065 ms |
17264 KB |
Output is correct |
85 |
Correct |
827 ms |
17440 KB |
Output is correct |
86 |
Correct |
271 ms |
15176 KB |
Output is correct |
87 |
Correct |
347 ms |
18340 KB |
Output is correct |
88 |
Correct |
361 ms |
15180 KB |
Output is correct |
89 |
Correct |
481 ms |
16972 KB |
Output is correct |
90 |
Correct |
494 ms |
17148 KB |
Output is correct |
91 |
Correct |
400 ms |
16592 KB |
Output is correct |
92 |
Correct |
885 ms |
17484 KB |
Output is correct |
93 |
Correct |
760 ms |
17360 KB |
Output is correct |
94 |
Correct |
262 ms |
15308 KB |
Output is correct |
95 |
Correct |
633 ms |
17376 KB |
Output is correct |
96 |
Correct |
673 ms |
17288 KB |
Output is correct |
97 |
Correct |
673 ms |
17252 KB |
Output is correct |
98 |
Correct |
652 ms |
17296 KB |
Output is correct |
99 |
Correct |
634 ms |
17320 KB |
Output is correct |
100 |
Correct |
640 ms |
17416 KB |
Output is correct |