// Copyright © 2022 Youngmin Park. All rights reserved.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
const ld PI = acos((ld)-1.0);
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
constexpr bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
constexpr bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
os << '{';
string sep;
for (const T &x : v)
os << sep << x, sep = ", ";
return os << '}';
}
template <typename T>
ostream &operator<<(ostream &os, const deque<T> &v) {
os << vector<T>(all(v));
return os;
}
template <typename T, typename S, typename C>
ostream &operator<<(ostream &os, priority_queue<T, S, C> pq) {
vector<T> v;
while (sz(pq)) {
v.pb(pq.top());
pq.pop();
}
os << v;
return os;
}
void dbg_out()
{
cerr << "\033[0m" << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "\033[1;35m" << __func__ << ':' << __LINE__ << " (" << #__VA_ARGS__ << "):\033[33m", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif
inline namespace RecursiveLambda
{
template <typename Fun>
struct y_combinator_result
{
Fun fun_;
template <typename T>
explicit y_combinator_result(T &&fun) : fun_(forward<T>(fun)) {}
template <typename... Args>
decltype(auto) operator()(Args &&...args)
{
return fun_(ref(*this), forward<Args>(args)...);
}
};
template <typename Fun>
decltype(auto) y_combinator(Fun &&fun)
{
return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
}
};
inline namespace Range {
class ForwardRange {
int src, dst;
public:
explicit constexpr ForwardRange(const int l, const int r) : src(l), dst(r) {}
explicit constexpr ForwardRange(const int n) : src(0), dst(n) {}
constexpr ForwardRange begin() const { return *this; }
constexpr monostate end() const { return {}; }
constexpr bool operator!=(monostate) const { return src < dst; }
constexpr void operator++() const {}
constexpr int operator*() { return src++; }
};
class BackwardRange {
int src, dst;
public:
explicit constexpr BackwardRange(const int l, const int r) : src(r), dst(l) {}
explicit constexpr BackwardRange(const int n) : src(n), dst(0) {}
constexpr BackwardRange begin() const { return *this; }
constexpr monostate end() const { return {}; }
constexpr bool operator!=(monostate) const { return src > dst; }
constexpr void operator++() const {}
constexpr int operator*() { return --src; }
};
using rep = ForwardRange;
using per = BackwardRange;
};
int dp0[1 << 20], dp1[1 << 20];
int tmp[64];
int L, Q;
void solve()
{
cin >> L >> Q;
string s;
cin >> s;
for (int i : rep(1 << L)) dp0[i] = dp1[i] = s[i] - '0';
for (int j : rep(L)) for (int i : rep(1 << L)) {
if (i >> j & 1) dp0[i] += dp0[i ^ (1 << j)];
else dp1[i] += dp1[i ^ (1 << j)];
}
for (int QQQ : rep(Q)) {
string t;
cin >> t;
reverse(all(t));
int ques{}, one{}, zr{};
int mq{}, mo{}, mz{};
int ans{};
for (int j : rep(L)) {
if (t[j] == '?') ques++, mq ^= 1 << j;
else if (t[j] == '0') zr++, mz ^= 1 << j;
else one++, mo ^= 1 << j;
}
if (int mi = min({ques, one, zr}); mi == ques) {
for (int msk = mq; ; msk = (msk - 1) & mq) {
ans += s[mo | msk] - '0';
if (msk == 0) break;
}
}else if (mi == one) {
for (int msk = mo; ; msk = (msk - 1) & mo) {
int cnt = __builtin_popcount(msk ^ mo) & 1;
if (cnt) ans -= dp0[msk | mq];
else ans += dp0[msk | mq];
if (msk == 0) break;
}
}else{
for (int msk = mz; ; msk = (msk - 1) & mz) {
int cnt = __builtin_popcount(msk) & 1;
if (cnt) ans -= dp1[msk | mo];
else ans += dp1[msk | mo];
if (msk == 0) break;
}
}
cout << ans << '\n';
}
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int testcase = 1;
// cin >> testcase;
while (testcase--)
{
solve();
}
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * (double)clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}
Compilation message
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:147:11: warning: unused variable 'QQQ' [-Wunused-variable]
147 | for (int QQQ : rep(Q)) {
| ^~~
# |
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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
196 ms |
4292 KB |
Output is correct |
12 |
Correct |
277 ms |
4032 KB |
Output is correct |
13 |
Correct |
206 ms |
3224 KB |
Output is correct |
14 |
Correct |
199 ms |
3392 KB |
Output is correct |
15 |
Correct |
207 ms |
4328 KB |
Output is correct |
16 |
Correct |
224 ms |
3412 KB |
Output is correct |
17 |
Correct |
225 ms |
3380 KB |
Output is correct |
18 |
Correct |
142 ms |
5264 KB |
Output is correct |
19 |
Correct |
179 ms |
2268 KB |
Output is correct |
20 |
Correct |
192 ms |
3916 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
196 ms |
4292 KB |
Output is correct |
12 |
Correct |
277 ms |
4032 KB |
Output is correct |
13 |
Correct |
206 ms |
3224 KB |
Output is correct |
14 |
Correct |
199 ms |
3392 KB |
Output is correct |
15 |
Correct |
207 ms |
4328 KB |
Output is correct |
16 |
Correct |
224 ms |
3412 KB |
Output is correct |
17 |
Correct |
225 ms |
3380 KB |
Output is correct |
18 |
Correct |
142 ms |
5264 KB |
Output is correct |
19 |
Correct |
179 ms |
2268 KB |
Output is correct |
20 |
Correct |
192 ms |
3916 KB |
Output is correct |
21 |
Correct |
200 ms |
4336 KB |
Output is correct |
22 |
Correct |
237 ms |
4500 KB |
Output is correct |
23 |
Correct |
243 ms |
3524 KB |
Output is correct |
24 |
Correct |
245 ms |
3408 KB |
Output is correct |
25 |
Correct |
233 ms |
5404 KB |
Output is correct |
26 |
Correct |
263 ms |
3892 KB |
Output is correct |
27 |
Correct |
268 ms |
3796 KB |
Output is correct |
28 |
Correct |
154 ms |
6540 KB |
Output is correct |
29 |
Correct |
201 ms |
2380 KB |
Output is correct |
30 |
Correct |
235 ms |
4504 KB |
Output is correct |
31 |
Correct |
271 ms |
4420 KB |
Output is correct |
32 |
Correct |
286 ms |
4428 KB |
Output is correct |
33 |
Correct |
213 ms |
3364 KB |
Output is correct |
34 |
Correct |
263 ms |
3312 KB |
Output is correct |
35 |
Correct |
269 ms |
3812 KB |
Output is correct |
36 |
Correct |
144 ms |
2304 KB |
Output is correct |
37 |
Correct |
203 ms |
4456 KB |
Output is correct |
38 |
Correct |
207 ms |
2456 KB |
Output is correct |
39 |
Correct |
241 ms |
3676 KB |
Output is correct |
40 |
Correct |
244 ms |
3536 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
41 ms |
9928 KB |
Output is correct |
12 |
Correct |
47 ms |
9876 KB |
Output is correct |
13 |
Correct |
49 ms |
9804 KB |
Output is correct |
14 |
Correct |
53 ms |
9844 KB |
Output is correct |
15 |
Correct |
48 ms |
9932 KB |
Output is correct |
16 |
Correct |
61 ms |
9820 KB |
Output is correct |
17 |
Correct |
52 ms |
9816 KB |
Output is correct |
18 |
Correct |
36 ms |
9984 KB |
Output is correct |
19 |
Correct |
37 ms |
9720 KB |
Output is correct |
20 |
Correct |
39 ms |
9820 KB |
Output is correct |
21 |
Correct |
57 ms |
9840 KB |
Output is correct |
22 |
Correct |
58 ms |
9824 KB |
Output is correct |
23 |
Correct |
39 ms |
9820 KB |
Output is correct |
24 |
Correct |
48 ms |
9772 KB |
Output is correct |
25 |
Correct |
54 ms |
9876 KB |
Output is correct |
26 |
Correct |
36 ms |
9688 KB |
Output is correct |
27 |
Correct |
40 ms |
9828 KB |
Output is correct |
28 |
Correct |
43 ms |
9708 KB |
Output is correct |
29 |
Correct |
41 ms |
9840 KB |
Output is correct |
30 |
Correct |
46 ms |
9916 KB |
Output is correct |
31 |
Correct |
38 ms |
9820 KB |
Output is correct |
32 |
Correct |
60 ms |
9820 KB |
Output is correct |
33 |
Correct |
50 ms |
9836 KB |
Output is correct |
34 |
Correct |
39 ms |
9764 KB |
Output is correct |
35 |
Correct |
49 ms |
9840 KB |
Output is correct |
36 |
Correct |
49 ms |
9868 KB |
Output is correct |
37 |
Correct |
49 ms |
9876 KB |
Output is correct |
38 |
Correct |
47 ms |
9812 KB |
Output is correct |
39 |
Correct |
53 ms |
9812 KB |
Output is correct |
40 |
Correct |
46 ms |
9848 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
196 ms |
4292 KB |
Output is correct |
12 |
Correct |
277 ms |
4032 KB |
Output is correct |
13 |
Correct |
206 ms |
3224 KB |
Output is correct |
14 |
Correct |
199 ms |
3392 KB |
Output is correct |
15 |
Correct |
207 ms |
4328 KB |
Output is correct |
16 |
Correct |
224 ms |
3412 KB |
Output is correct |
17 |
Correct |
225 ms |
3380 KB |
Output is correct |
18 |
Correct |
142 ms |
5264 KB |
Output is correct |
19 |
Correct |
179 ms |
2268 KB |
Output is correct |
20 |
Correct |
192 ms |
3916 KB |
Output is correct |
21 |
Correct |
200 ms |
4336 KB |
Output is correct |
22 |
Correct |
237 ms |
4500 KB |
Output is correct |
23 |
Correct |
243 ms |
3524 KB |
Output is correct |
24 |
Correct |
245 ms |
3408 KB |
Output is correct |
25 |
Correct |
233 ms |
5404 KB |
Output is correct |
26 |
Correct |
263 ms |
3892 KB |
Output is correct |
27 |
Correct |
268 ms |
3796 KB |
Output is correct |
28 |
Correct |
154 ms |
6540 KB |
Output is correct |
29 |
Correct |
201 ms |
2380 KB |
Output is correct |
30 |
Correct |
235 ms |
4504 KB |
Output is correct |
31 |
Correct |
271 ms |
4420 KB |
Output is correct |
32 |
Correct |
286 ms |
4428 KB |
Output is correct |
33 |
Correct |
213 ms |
3364 KB |
Output is correct |
34 |
Correct |
263 ms |
3312 KB |
Output is correct |
35 |
Correct |
269 ms |
3812 KB |
Output is correct |
36 |
Correct |
144 ms |
2304 KB |
Output is correct |
37 |
Correct |
203 ms |
4456 KB |
Output is correct |
38 |
Correct |
207 ms |
2456 KB |
Output is correct |
39 |
Correct |
241 ms |
3676 KB |
Output is correct |
40 |
Correct |
244 ms |
3536 KB |
Output is correct |
41 |
Correct |
41 ms |
9928 KB |
Output is correct |
42 |
Correct |
47 ms |
9876 KB |
Output is correct |
43 |
Correct |
49 ms |
9804 KB |
Output is correct |
44 |
Correct |
53 ms |
9844 KB |
Output is correct |
45 |
Correct |
48 ms |
9932 KB |
Output is correct |
46 |
Correct |
61 ms |
9820 KB |
Output is correct |
47 |
Correct |
52 ms |
9816 KB |
Output is correct |
48 |
Correct |
36 ms |
9984 KB |
Output is correct |
49 |
Correct |
37 ms |
9720 KB |
Output is correct |
50 |
Correct |
39 ms |
9820 KB |
Output is correct |
51 |
Correct |
57 ms |
9840 KB |
Output is correct |
52 |
Correct |
58 ms |
9824 KB |
Output is correct |
53 |
Correct |
39 ms |
9820 KB |
Output is correct |
54 |
Correct |
48 ms |
9772 KB |
Output is correct |
55 |
Correct |
54 ms |
9876 KB |
Output is correct |
56 |
Correct |
36 ms |
9688 KB |
Output is correct |
57 |
Correct |
40 ms |
9828 KB |
Output is correct |
58 |
Correct |
43 ms |
9708 KB |
Output is correct |
59 |
Correct |
41 ms |
9840 KB |
Output is correct |
60 |
Correct |
46 ms |
9916 KB |
Output is correct |
61 |
Correct |
38 ms |
9820 KB |
Output is correct |
62 |
Correct |
60 ms |
9820 KB |
Output is correct |
63 |
Correct |
50 ms |
9836 KB |
Output is correct |
64 |
Correct |
39 ms |
9764 KB |
Output is correct |
65 |
Correct |
49 ms |
9840 KB |
Output is correct |
66 |
Correct |
49 ms |
9868 KB |
Output is correct |
67 |
Correct |
49 ms |
9876 KB |
Output is correct |
68 |
Correct |
47 ms |
9812 KB |
Output is correct |
69 |
Correct |
53 ms |
9812 KB |
Output is correct |
70 |
Correct |
46 ms |
9848 KB |
Output is correct |
71 |
Correct |
322 ms |
14592 KB |
Output is correct |
72 |
Correct |
338 ms |
14852 KB |
Output is correct |
73 |
Correct |
375 ms |
13264 KB |
Output is correct |
74 |
Correct |
616 ms |
13696 KB |
Output is correct |
75 |
Correct |
361 ms |
15656 KB |
Output is correct |
76 |
Correct |
678 ms |
13968 KB |
Output is correct |
77 |
Correct |
636 ms |
13876 KB |
Output is correct |
78 |
Correct |
219 ms |
17700 KB |
Output is correct |
79 |
Correct |
302 ms |
11744 KB |
Output is correct |
80 |
Correct |
338 ms |
14820 KB |
Output is correct |
81 |
Correct |
495 ms |
14704 KB |
Output is correct |
82 |
Correct |
637 ms |
13744 KB |
Output is correct |
83 |
Correct |
327 ms |
12764 KB |
Output is correct |
84 |
Correct |
517 ms |
13680 KB |
Output is correct |
85 |
Correct |
635 ms |
13860 KB |
Output is correct |
86 |
Correct |
217 ms |
11588 KB |
Output is correct |
87 |
Correct |
316 ms |
14696 KB |
Output is correct |
88 |
Correct |
304 ms |
11640 KB |
Output is correct |
89 |
Correct |
380 ms |
13276 KB |
Output is correct |
90 |
Correct |
448 ms |
13788 KB |
Output is correct |
91 |
Correct |
317 ms |
12804 KB |
Output is correct |
92 |
Correct |
687 ms |
13944 KB |
Output is correct |
93 |
Correct |
604 ms |
13780 KB |
Output is correct |
94 |
Correct |
236 ms |
11608 KB |
Output is correct |
95 |
Correct |
530 ms |
13792 KB |
Output is correct |
96 |
Correct |
498 ms |
13704 KB |
Output is correct |
97 |
Correct |
506 ms |
13784 KB |
Output is correct |
98 |
Correct |
520 ms |
13704 KB |
Output is correct |
99 |
Correct |
497 ms |
13924 KB |
Output is correct |
100 |
Correct |
509 ms |
13932 KB |
Output is correct |