#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;
for(int i = 0; i < n; ++i){
if(s[i]=='0')
a |= 1<<i;
else if(s[i]=='1')
b |= 1<<i;
else
c |= 1<<i;
}
ll ans = 0;
if(a==min({a, b, c})){
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==min({a, b, c})){
for(int x = b; ; x = (x-1)&b){
if((__builtin_popcount(b) - __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)
| ~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
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 |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
200 ms |
7868 KB |
Output is correct |
12 |
Correct |
211 ms |
7524 KB |
Output is correct |
13 |
Correct |
251 ms |
6852 KB |
Output is correct |
14 |
Correct |
260 ms |
6872 KB |
Output is correct |
15 |
Correct |
248 ms |
7832 KB |
Output is correct |
16 |
Correct |
252 ms |
6948 KB |
Output is correct |
17 |
Correct |
251 ms |
7012 KB |
Output is correct |
18 |
Correct |
226 ms |
8948 KB |
Output is correct |
19 |
Correct |
186 ms |
5756 KB |
Output is correct |
20 |
Correct |
226 ms |
7524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
200 ms |
7868 KB |
Output is correct |
12 |
Correct |
211 ms |
7524 KB |
Output is correct |
13 |
Correct |
251 ms |
6852 KB |
Output is correct |
14 |
Correct |
260 ms |
6872 KB |
Output is correct |
15 |
Correct |
248 ms |
7832 KB |
Output is correct |
16 |
Correct |
252 ms |
6948 KB |
Output is correct |
17 |
Correct |
251 ms |
7012 KB |
Output is correct |
18 |
Correct |
226 ms |
8948 KB |
Output is correct |
19 |
Correct |
186 ms |
5756 KB |
Output is correct |
20 |
Correct |
226 ms |
7524 KB |
Output is correct |
21 |
Correct |
219 ms |
7876 KB |
Output is correct |
22 |
Correct |
274 ms |
8104 KB |
Output is correct |
23 |
Correct |
320 ms |
7072 KB |
Output is correct |
24 |
Correct |
298 ms |
6844 KB |
Output is correct |
25 |
Correct |
291 ms |
8920 KB |
Output is correct |
26 |
Correct |
320 ms |
7460 KB |
Output is correct |
27 |
Correct |
326 ms |
7484 KB |
Output is correct |
28 |
Correct |
191 ms |
9868 KB |
Output is correct |
29 |
Correct |
215 ms |
5868 KB |
Output is correct |
30 |
Correct |
237 ms |
8080 KB |
Output is correct |
31 |
Correct |
318 ms |
7984 KB |
Output is correct |
32 |
Correct |
292 ms |
7840 KB |
Output is correct |
33 |
Correct |
302 ms |
6796 KB |
Output is correct |
34 |
Correct |
329 ms |
6956 KB |
Output is correct |
35 |
Correct |
322 ms |
7360 KB |
Output is correct |
36 |
Correct |
186 ms |
5940 KB |
Output is correct |
37 |
Correct |
246 ms |
7984 KB |
Output is correct |
38 |
Correct |
221 ms |
5836 KB |
Output is correct |
39 |
Correct |
327 ms |
7128 KB |
Output is correct |
40 |
Correct |
295 ms |
6960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
79 ms |
14472 KB |
Output is correct |
12 |
Correct |
77 ms |
13704 KB |
Output is correct |
13 |
Correct |
114 ms |
13516 KB |
Output is correct |
14 |
Correct |
114 ms |
13632 KB |
Output is correct |
15 |
Correct |
128 ms |
13648 KB |
Output is correct |
16 |
Correct |
114 ms |
13648 KB |
Output is correct |
17 |
Correct |
126 ms |
13580 KB |
Output is correct |
18 |
Correct |
64 ms |
13768 KB |
Output is correct |
19 |
Correct |
81 ms |
13640 KB |
Output is correct |
20 |
Correct |
73 ms |
13636 KB |
Output is correct |
21 |
Correct |
99 ms |
13576 KB |
Output is correct |
22 |
Correct |
121 ms |
13644 KB |
Output is correct |
23 |
Correct |
184 ms |
13644 KB |
Output is correct |
24 |
Correct |
116 ms |
13556 KB |
Output is correct |
25 |
Correct |
140 ms |
13656 KB |
Output is correct |
26 |
Correct |
77 ms |
13528 KB |
Output is correct |
27 |
Correct |
67 ms |
13700 KB |
Output is correct |
28 |
Correct |
72 ms |
13524 KB |
Output is correct |
29 |
Correct |
128 ms |
13576 KB |
Output is correct |
30 |
Correct |
144 ms |
13572 KB |
Output is correct |
31 |
Correct |
177 ms |
13680 KB |
Output is correct |
32 |
Correct |
129 ms |
13552 KB |
Output is correct |
33 |
Correct |
130 ms |
13564 KB |
Output is correct |
34 |
Correct |
75 ms |
13488 KB |
Output is correct |
35 |
Correct |
138 ms |
13604 KB |
Output is correct |
36 |
Correct |
154 ms |
13636 KB |
Output is correct |
37 |
Correct |
116 ms |
13628 KB |
Output is correct |
38 |
Correct |
128 ms |
13644 KB |
Output is correct |
39 |
Correct |
118 ms |
13580 KB |
Output is correct |
40 |
Correct |
116 ms |
13644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
200 ms |
7868 KB |
Output is correct |
12 |
Correct |
211 ms |
7524 KB |
Output is correct |
13 |
Correct |
251 ms |
6852 KB |
Output is correct |
14 |
Correct |
260 ms |
6872 KB |
Output is correct |
15 |
Correct |
248 ms |
7832 KB |
Output is correct |
16 |
Correct |
252 ms |
6948 KB |
Output is correct |
17 |
Correct |
251 ms |
7012 KB |
Output is correct |
18 |
Correct |
226 ms |
8948 KB |
Output is correct |
19 |
Correct |
186 ms |
5756 KB |
Output is correct |
20 |
Correct |
226 ms |
7524 KB |
Output is correct |
21 |
Correct |
219 ms |
7876 KB |
Output is correct |
22 |
Correct |
274 ms |
8104 KB |
Output is correct |
23 |
Correct |
320 ms |
7072 KB |
Output is correct |
24 |
Correct |
298 ms |
6844 KB |
Output is correct |
25 |
Correct |
291 ms |
8920 KB |
Output is correct |
26 |
Correct |
320 ms |
7460 KB |
Output is correct |
27 |
Correct |
326 ms |
7484 KB |
Output is correct |
28 |
Correct |
191 ms |
9868 KB |
Output is correct |
29 |
Correct |
215 ms |
5868 KB |
Output is correct |
30 |
Correct |
237 ms |
8080 KB |
Output is correct |
31 |
Correct |
318 ms |
7984 KB |
Output is correct |
32 |
Correct |
292 ms |
7840 KB |
Output is correct |
33 |
Correct |
302 ms |
6796 KB |
Output is correct |
34 |
Correct |
329 ms |
6956 KB |
Output is correct |
35 |
Correct |
322 ms |
7360 KB |
Output is correct |
36 |
Correct |
186 ms |
5940 KB |
Output is correct |
37 |
Correct |
246 ms |
7984 KB |
Output is correct |
38 |
Correct |
221 ms |
5836 KB |
Output is correct |
39 |
Correct |
327 ms |
7128 KB |
Output is correct |
40 |
Correct |
295 ms |
6960 KB |
Output is correct |
41 |
Correct |
79 ms |
14472 KB |
Output is correct |
42 |
Correct |
77 ms |
13704 KB |
Output is correct |
43 |
Correct |
114 ms |
13516 KB |
Output is correct |
44 |
Correct |
114 ms |
13632 KB |
Output is correct |
45 |
Correct |
128 ms |
13648 KB |
Output is correct |
46 |
Correct |
114 ms |
13648 KB |
Output is correct |
47 |
Correct |
126 ms |
13580 KB |
Output is correct |
48 |
Correct |
64 ms |
13768 KB |
Output is correct |
49 |
Correct |
81 ms |
13640 KB |
Output is correct |
50 |
Correct |
73 ms |
13636 KB |
Output is correct |
51 |
Correct |
99 ms |
13576 KB |
Output is correct |
52 |
Correct |
121 ms |
13644 KB |
Output is correct |
53 |
Correct |
184 ms |
13644 KB |
Output is correct |
54 |
Correct |
116 ms |
13556 KB |
Output is correct |
55 |
Correct |
140 ms |
13656 KB |
Output is correct |
56 |
Correct |
77 ms |
13528 KB |
Output is correct |
57 |
Correct |
67 ms |
13700 KB |
Output is correct |
58 |
Correct |
72 ms |
13524 KB |
Output is correct |
59 |
Correct |
128 ms |
13576 KB |
Output is correct |
60 |
Correct |
144 ms |
13572 KB |
Output is correct |
61 |
Correct |
177 ms |
13680 KB |
Output is correct |
62 |
Correct |
129 ms |
13552 KB |
Output is correct |
63 |
Correct |
130 ms |
13564 KB |
Output is correct |
64 |
Correct |
75 ms |
13488 KB |
Output is correct |
65 |
Correct |
138 ms |
13604 KB |
Output is correct |
66 |
Correct |
154 ms |
13636 KB |
Output is correct |
67 |
Correct |
116 ms |
13628 KB |
Output is correct |
68 |
Correct |
128 ms |
13644 KB |
Output is correct |
69 |
Correct |
118 ms |
13580 KB |
Output is correct |
70 |
Correct |
116 ms |
13644 KB |
Output is correct |
71 |
Correct |
414 ms |
18152 KB |
Output is correct |
72 |
Correct |
453 ms |
18480 KB |
Output is correct |
73 |
Correct |
1397 ms |
17104 KB |
Output is correct |
74 |
Correct |
1257 ms |
17180 KB |
Output is correct |
75 |
Correct |
1145 ms |
19260 KB |
Output is correct |
76 |
Correct |
1362 ms |
17740 KB |
Output is correct |
77 |
Correct |
1483 ms |
17524 KB |
Output is correct |
78 |
Correct |
295 ms |
21176 KB |
Output is correct |
79 |
Correct |
394 ms |
15296 KB |
Output is correct |
80 |
Correct |
449 ms |
18448 KB |
Output is correct |
81 |
Correct |
1204 ms |
18372 KB |
Output is correct |
82 |
Correct |
1266 ms |
17344 KB |
Output is correct |
83 |
Execution timed out |
2080 ms |
16584 KB |
Time limit exceeded |
84 |
Halted |
0 ms |
0 KB |
- |