// 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 msk{};
int ans{};
for (int j : rep(L)) {
if (t[j] == '?') ques++;
else if (t[j] == '0') zr++;
else one++;
}
if (int mi = min({ques, one, zr}); mi == ques) {
vi ind;
for (int j : rep(L)) {
if (t[j] == '1') msk ^= 1 << j;
if (t[j] == '?') ind.pb(j);
}
for (int i : rep(1 << sz(ind))) {
int mk{msk};
for (int j : rep(sz(ind))) {
if (i >> j & 1) mk ^= (1 << ind[j]);
}
ans += s[mk] - '0';
}
}else if (mi == one) {
vi ind;
for (int j : rep(L)) {
if (t[j] == '?') msk ^= 1 << j;
else if (t[j] == '1') ind.pb(j);
}
for (int i : rep(1 << sz(ind))) {
int mk{msk};
for (int j : rep(sz(ind))) {
if (i >> j & 1) mk ^= (1 << ind[j]);
}
tmp[i] = dp0[mk];
}
for (int j : rep(sz(ind))) for (int mk : rep(1 << sz(ind))) {
if (mk >> j & 1) tmp[mk] -= tmp[mk ^ (1 << j)];
}
ans = tmp[(1 << sz(ind)) - 1];
}else{
vi ind;
for (int j : rep(L)) {
if (t[j] == '1') msk ^= 1 << j;
else if (t[j] == '0') ind.pb(j);
}
for (int i : rep(1 << sz(ind))) {
int mk{msk};
for (int j : rep(sz(ind))) {
if (i >> j & 1) mk ^= (1 << ind[j]);
}
tmp[i] = dp1[mk];
}
for (int j : rep(sz(ind))) for (int mk : rep(1 << sz(ind))) {
if (mk >> j & 1) tmp[mk ^ (1 << j)] -= tmp[mk];
}
ans = tmp[0];
}
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)) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 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 |
2 ms |
340 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 |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 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 |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
248 ms |
10340 KB |
Output is correct |
12 |
Correct |
270 ms |
14660 KB |
Output is correct |
13 |
Correct |
356 ms |
14000 KB |
Output is correct |
14 |
Correct |
495 ms |
14012 KB |
Output is correct |
15 |
Correct |
399 ms |
15084 KB |
Output is correct |
16 |
Correct |
481 ms |
14244 KB |
Output is correct |
17 |
Correct |
398 ms |
14280 KB |
Output is correct |
18 |
Correct |
174 ms |
16024 KB |
Output is correct |
19 |
Correct |
211 ms |
13000 KB |
Output is correct |
20 |
Correct |
228 ms |
14856 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 |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 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 |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
248 ms |
10340 KB |
Output is correct |
12 |
Correct |
270 ms |
14660 KB |
Output is correct |
13 |
Correct |
356 ms |
14000 KB |
Output is correct |
14 |
Correct |
495 ms |
14012 KB |
Output is correct |
15 |
Correct |
399 ms |
15084 KB |
Output is correct |
16 |
Correct |
481 ms |
14244 KB |
Output is correct |
17 |
Correct |
398 ms |
14280 KB |
Output is correct |
18 |
Correct |
174 ms |
16024 KB |
Output is correct |
19 |
Correct |
211 ms |
13000 KB |
Output is correct |
20 |
Correct |
228 ms |
14856 KB |
Output is correct |
21 |
Correct |
292 ms |
18076 KB |
Output is correct |
22 |
Correct |
304 ms |
18216 KB |
Output is correct |
23 |
Correct |
455 ms |
17172 KB |
Output is correct |
24 |
Correct |
541 ms |
17184 KB |
Output is correct |
25 |
Correct |
371 ms |
18996 KB |
Output is correct |
26 |
Correct |
520 ms |
17576 KB |
Output is correct |
27 |
Correct |
514 ms |
17456 KB |
Output is correct |
28 |
Correct |
185 ms |
20020 KB |
Output is correct |
29 |
Correct |
253 ms |
16016 KB |
Output is correct |
30 |
Correct |
317 ms |
18156 KB |
Output is correct |
31 |
Correct |
471 ms |
18120 KB |
Output is correct |
32 |
Correct |
623 ms |
18096 KB |
Output is correct |
33 |
Correct |
370 ms |
17004 KB |
Output is correct |
34 |
Correct |
502 ms |
17192 KB |
Output is correct |
35 |
Correct |
490 ms |
17472 KB |
Output is correct |
36 |
Correct |
175 ms |
16004 KB |
Output is correct |
37 |
Correct |
263 ms |
18088 KB |
Output is correct |
38 |
Correct |
289 ms |
15980 KB |
Output is correct |
39 |
Correct |
469 ms |
17192 KB |
Output is correct |
40 |
Correct |
512 ms |
17024 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 |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 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 |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
44 ms |
11964 KB |
Output is correct |
12 |
Correct |
51 ms |
11884 KB |
Output is correct |
13 |
Correct |
62 ms |
11828 KB |
Output is correct |
14 |
Correct |
130 ms |
11988 KB |
Output is correct |
15 |
Correct |
54 ms |
12048 KB |
Output is correct |
16 |
Correct |
105 ms |
11840 KB |
Output is correct |
17 |
Correct |
86 ms |
11888 KB |
Output is correct |
18 |
Correct |
39 ms |
12016 KB |
Output is correct |
19 |
Correct |
47 ms |
11836 KB |
Output is correct |
20 |
Correct |
45 ms |
11892 KB |
Output is correct |
21 |
Correct |
68 ms |
11928 KB |
Output is correct |
22 |
Correct |
110 ms |
11836 KB |
Output is correct |
23 |
Correct |
54 ms |
11836 KB |
Output is correct |
24 |
Correct |
83 ms |
11812 KB |
Output is correct |
25 |
Correct |
82 ms |
11848 KB |
Output is correct |
26 |
Correct |
37 ms |
11756 KB |
Output is correct |
27 |
Correct |
50 ms |
11916 KB |
Output is correct |
28 |
Correct |
49 ms |
11812 KB |
Output is correct |
29 |
Correct |
61 ms |
11908 KB |
Output is correct |
30 |
Correct |
82 ms |
11852 KB |
Output is correct |
31 |
Correct |
53 ms |
11772 KB |
Output is correct |
32 |
Correct |
103 ms |
11888 KB |
Output is correct |
33 |
Correct |
84 ms |
11888 KB |
Output is correct |
34 |
Correct |
40 ms |
11736 KB |
Output is correct |
35 |
Correct |
95 ms |
11804 KB |
Output is correct |
36 |
Correct |
76 ms |
11852 KB |
Output is correct |
37 |
Correct |
74 ms |
11840 KB |
Output is correct |
38 |
Correct |
97 ms |
11836 KB |
Output is correct |
39 |
Correct |
74 ms |
11884 KB |
Output is correct |
40 |
Correct |
77 ms |
11848 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 |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 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 |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
248 ms |
10340 KB |
Output is correct |
12 |
Correct |
270 ms |
14660 KB |
Output is correct |
13 |
Correct |
356 ms |
14000 KB |
Output is correct |
14 |
Correct |
495 ms |
14012 KB |
Output is correct |
15 |
Correct |
399 ms |
15084 KB |
Output is correct |
16 |
Correct |
481 ms |
14244 KB |
Output is correct |
17 |
Correct |
398 ms |
14280 KB |
Output is correct |
18 |
Correct |
174 ms |
16024 KB |
Output is correct |
19 |
Correct |
211 ms |
13000 KB |
Output is correct |
20 |
Correct |
228 ms |
14856 KB |
Output is correct |
21 |
Correct |
292 ms |
18076 KB |
Output is correct |
22 |
Correct |
304 ms |
18216 KB |
Output is correct |
23 |
Correct |
455 ms |
17172 KB |
Output is correct |
24 |
Correct |
541 ms |
17184 KB |
Output is correct |
25 |
Correct |
371 ms |
18996 KB |
Output is correct |
26 |
Correct |
520 ms |
17576 KB |
Output is correct |
27 |
Correct |
514 ms |
17456 KB |
Output is correct |
28 |
Correct |
185 ms |
20020 KB |
Output is correct |
29 |
Correct |
253 ms |
16016 KB |
Output is correct |
30 |
Correct |
317 ms |
18156 KB |
Output is correct |
31 |
Correct |
471 ms |
18120 KB |
Output is correct |
32 |
Correct |
623 ms |
18096 KB |
Output is correct |
33 |
Correct |
370 ms |
17004 KB |
Output is correct |
34 |
Correct |
502 ms |
17192 KB |
Output is correct |
35 |
Correct |
490 ms |
17472 KB |
Output is correct |
36 |
Correct |
175 ms |
16004 KB |
Output is correct |
37 |
Correct |
263 ms |
18088 KB |
Output is correct |
38 |
Correct |
289 ms |
15980 KB |
Output is correct |
39 |
Correct |
469 ms |
17192 KB |
Output is correct |
40 |
Correct |
512 ms |
17024 KB |
Output is correct |
41 |
Correct |
44 ms |
11964 KB |
Output is correct |
42 |
Correct |
51 ms |
11884 KB |
Output is correct |
43 |
Correct |
62 ms |
11828 KB |
Output is correct |
44 |
Correct |
130 ms |
11988 KB |
Output is correct |
45 |
Correct |
54 ms |
12048 KB |
Output is correct |
46 |
Correct |
105 ms |
11840 KB |
Output is correct |
47 |
Correct |
86 ms |
11888 KB |
Output is correct |
48 |
Correct |
39 ms |
12016 KB |
Output is correct |
49 |
Correct |
47 ms |
11836 KB |
Output is correct |
50 |
Correct |
45 ms |
11892 KB |
Output is correct |
51 |
Correct |
68 ms |
11928 KB |
Output is correct |
52 |
Correct |
110 ms |
11836 KB |
Output is correct |
53 |
Correct |
54 ms |
11836 KB |
Output is correct |
54 |
Correct |
83 ms |
11812 KB |
Output is correct |
55 |
Correct |
82 ms |
11848 KB |
Output is correct |
56 |
Correct |
37 ms |
11756 KB |
Output is correct |
57 |
Correct |
50 ms |
11916 KB |
Output is correct |
58 |
Correct |
49 ms |
11812 KB |
Output is correct |
59 |
Correct |
61 ms |
11908 KB |
Output is correct |
60 |
Correct |
82 ms |
11852 KB |
Output is correct |
61 |
Correct |
53 ms |
11772 KB |
Output is correct |
62 |
Correct |
103 ms |
11888 KB |
Output is correct |
63 |
Correct |
84 ms |
11888 KB |
Output is correct |
64 |
Correct |
40 ms |
11736 KB |
Output is correct |
65 |
Correct |
95 ms |
11804 KB |
Output is correct |
66 |
Correct |
76 ms |
11852 KB |
Output is correct |
67 |
Correct |
74 ms |
11840 KB |
Output is correct |
68 |
Correct |
97 ms |
11836 KB |
Output is correct |
69 |
Correct |
74 ms |
11884 KB |
Output is correct |
70 |
Correct |
77 ms |
11848 KB |
Output is correct |
71 |
Correct |
432 ms |
36332 KB |
Output is correct |
72 |
Correct |
465 ms |
36568 KB |
Output is correct |
73 |
Correct |
773 ms |
34932 KB |
Output is correct |
74 |
Correct |
1854 ms |
35160 KB |
Output is correct |
75 |
Correct |
601 ms |
37124 KB |
Output is correct |
76 |
Correct |
1637 ms |
35460 KB |
Output is correct |
77 |
Correct |
1275 ms |
35528 KB |
Output is correct |
78 |
Correct |
268 ms |
39164 KB |
Output is correct |
79 |
Correct |
426 ms |
33188 KB |
Output is correct |
80 |
Correct |
470 ms |
36328 KB |
Output is correct |
81 |
Correct |
940 ms |
36196 KB |
Output is correct |
82 |
Correct |
1693 ms |
35260 KB |
Output is correct |
83 |
Correct |
557 ms |
34304 KB |
Output is correct |
84 |
Correct |
1201 ms |
35164 KB |
Output is correct |
85 |
Correct |
1245 ms |
35404 KB |
Output is correct |
86 |
Correct |
248 ms |
33220 KB |
Output is correct |
87 |
Correct |
452 ms |
36208 KB |
Output is correct |
88 |
Correct |
448 ms |
33264 KB |
Output is correct |
89 |
Correct |
834 ms |
34864 KB |
Output is correct |
90 |
Correct |
1220 ms |
35284 KB |
Output is correct |
91 |
Correct |
561 ms |
34360 KB |
Output is correct |
92 |
Correct |
1503 ms |
35556 KB |
Output is correct |
93 |
Correct |
1234 ms |
35424 KB |
Output is correct |
94 |
Correct |
260 ms |
33160 KB |
Output is correct |
95 |
Correct |
965 ms |
35272 KB |
Output is correct |
96 |
Correct |
985 ms |
35540 KB |
Output is correct |
97 |
Correct |
994 ms |
35288 KB |
Output is correct |
98 |
Correct |
1000 ms |
35300 KB |
Output is correct |
99 |
Correct |
995 ms |
35240 KB |
Output is correct |
100 |
Correct |
959 ms |
35396 KB |
Output is correct |