#include <cstdio>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stdlib.h>
#include <cstring>
#include <string.h>
#include <string>
#include <sstream>
#include <assert.h>
#include <climits>
#include <sstream>
#include <numeric>
#include <time.h>
#include <limits.h>
#include <list>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <iomanip>
#include <complex>
#include <chrono>
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,lzcnt,popcnt")
#define lowbit(x) ((x) & -(x))
#define ml(a, b) ((1ll * (a) * (b)) % M)
#define tml(a, b) (a) = ((1ll * (a) * (b)) % M)
#define ad(a, b) ((0ll + (a) + (b)) % M)
#define tad(a, b) (a) = ((0ll + (a) + (b)) % M)
#define mi(a, b) ((0ll + M + (a) - (b)) % M)
#define tmi(a, b) (a) = ((0ll + M + (a) - (b)) % M)
#define tmin(a, b) (a) = min((a), (b))
#define tmax(a, b) (a) = max((a), (b))
#define iter(a) (a).begin(), (a).end()
#define riter(a) (a).rbegin(), (a).rend()
#define init(a, b) memset((a), (b), sizeof(a))
#define cpy(a, b) memcpy((a), (b), sizeof(a))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define pb push_back
#define mpr make_pair
#define ls(i) ((i) << 1)
#define rs(i) ((i) << 1 | 1)
#define INF 0x3f3f3f3f
#define eps 1e-9
#define F first
#define S second
#define N 20
#define AC cin.tie(0)->sync_with_stdio(0)
using namespace std;
typedef long long llt;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<llt, llt> pll;
typedef complex<double> cd;
// const int M = 998244353;
// random_device rm;
// mt19937 rg(rm());
// default_random_engine rg(rm());
// uniform_int_distribution<int> rd(INT_MIN, INT_MAX);
// uniform_real_distribution<double> rd(0, M_PI);
void db() { cout << "\n"; }
template <class T, class... U>
void db(T a, U... b) { cout << a << " ", db(b...); }
char s[1 << N], qs[N];
int dp[2][1 << N], n, ca, cb, cc, an, cr, ct;
void df1(int p)
{
if (p == n)
{
an += s[cr];
return;
}
if (qs[p] != '1')
df1(p + 1);
if (qs[p] != '0')
cr ^= 1 << p, df1(p + 1), cr ^= 1 << p;
}
void df2(int p)
{
if (p == n)
{
an += dp[0][cr] * (ct & 1 ? -1 : 1);
return;
}
if (qs[p] != '1')
df2(p + 1);
if (qs[p] != '?')
{
cr ^= 1 << p, ct += qs[p] == '0';
df2(p + 1);
cr ^= 1 << p, ct -= qs[p] == '0';
}
}
void df3(int p)
{
if (p == n)
{
an += dp[1][cr] * (ct & 1 ? -1 : 1);
return;
}
if (qs[p] != '0')
cr ^= 1 << p, df3(p + 1), cr ^= 1 << p;
if (qs[p] != '?')
{
ct += qs[p] == '1';
df3(p + 1);
ct -= qs[p] == '1';
}
}
signed main()
{
int q;
scanf("%d%d%s", &n, &q, s);
for (int i = 0; i < 1 << n; i++)
dp[0][i] = dp[1][i] = s[i] -= '0';
for (int j = 0; j < n; j++)
{
for (int i = 0; i < 1 << n; i++)
if ((i >> j) & 1)
dp[1][i] += dp[1][i ^ (1 << j)];
for (int i = (1 << n) - 1; i >= 0; i--)
if (~(i >> j) & 1)
dp[0][i] += dp[0][i ^ (1 << j)];
}
while (q-- && scanf("%s", qs))
{
reverse(qs, qs + n);
ca = cb = cc = an = 0;
for (int i = 0; i < n; i++)
(qs[i] == '0' ? ca : (qs[i] == '1' ? cb : cc))++;
if (cc <= ca && cc <= cb)
df1(0);
else if (ca <= cb && ca <= cc)
df2(0);
else
df3(0), assert(cb <= ca && cb <= cc);
printf("%d\n", an);
assert(cr == 0 && ct == 0);
}
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:130:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | scanf("%d%d%s", &n, &q, s);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
312 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 |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
312 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
272 ms |
4908 KB |
Output is correct |
12 |
Correct |
289 ms |
4640 KB |
Output is correct |
13 |
Correct |
402 ms |
3880 KB |
Output is correct |
14 |
Correct |
492 ms |
4008 KB |
Output is correct |
15 |
Correct |
369 ms |
5096 KB |
Output is correct |
16 |
Correct |
501 ms |
4172 KB |
Output is correct |
17 |
Correct |
464 ms |
4200 KB |
Output is correct |
18 |
Correct |
233 ms |
5936 KB |
Output is correct |
19 |
Correct |
270 ms |
3036 KB |
Output is correct |
20 |
Correct |
296 ms |
4684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
312 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
272 ms |
4908 KB |
Output is correct |
12 |
Correct |
289 ms |
4640 KB |
Output is correct |
13 |
Correct |
402 ms |
3880 KB |
Output is correct |
14 |
Correct |
492 ms |
4008 KB |
Output is correct |
15 |
Correct |
369 ms |
5096 KB |
Output is correct |
16 |
Correct |
501 ms |
4172 KB |
Output is correct |
17 |
Correct |
464 ms |
4200 KB |
Output is correct |
18 |
Correct |
233 ms |
5936 KB |
Output is correct |
19 |
Correct |
270 ms |
3036 KB |
Output is correct |
20 |
Correct |
296 ms |
4684 KB |
Output is correct |
21 |
Correct |
335 ms |
5052 KB |
Output is correct |
22 |
Correct |
383 ms |
5184 KB |
Output is correct |
23 |
Correct |
591 ms |
4216 KB |
Output is correct |
24 |
Correct |
776 ms |
4068 KB |
Output is correct |
25 |
Correct |
429 ms |
6092 KB |
Output is correct |
26 |
Correct |
707 ms |
4512 KB |
Output is correct |
27 |
Correct |
633 ms |
4564 KB |
Output is correct |
28 |
Correct |
252 ms |
7020 KB |
Output is correct |
29 |
Correct |
320 ms |
3020 KB |
Output is correct |
30 |
Correct |
352 ms |
5132 KB |
Output is correct |
31 |
Correct |
561 ms |
5024 KB |
Output is correct |
32 |
Correct |
811 ms |
5004 KB |
Output is correct |
33 |
Correct |
451 ms |
3916 KB |
Output is correct |
34 |
Correct |
721 ms |
3880 KB |
Output is correct |
35 |
Correct |
631 ms |
4388 KB |
Output is correct |
36 |
Correct |
253 ms |
2768 KB |
Output is correct |
37 |
Correct |
326 ms |
4856 KB |
Output is correct |
38 |
Correct |
342 ms |
2768 KB |
Output is correct |
39 |
Correct |
574 ms |
3924 KB |
Output is correct |
40 |
Correct |
773 ms |
3688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
312 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
81 ms |
10056 KB |
Output is correct |
12 |
Correct |
77 ms |
10168 KB |
Output is correct |
13 |
Correct |
112 ms |
10040 KB |
Output is correct |
14 |
Correct |
176 ms |
10040 KB |
Output is correct |
15 |
Correct |
96 ms |
10164 KB |
Output is correct |
16 |
Correct |
157 ms |
10040 KB |
Output is correct |
17 |
Correct |
151 ms |
10188 KB |
Output is correct |
18 |
Correct |
67 ms |
10292 KB |
Output is correct |
19 |
Correct |
77 ms |
9916 KB |
Output is correct |
20 |
Correct |
76 ms |
10060 KB |
Output is correct |
21 |
Correct |
118 ms |
9984 KB |
Output is correct |
22 |
Correct |
165 ms |
9972 KB |
Output is correct |
23 |
Correct |
100 ms |
9904 KB |
Output is correct |
24 |
Correct |
161 ms |
10024 KB |
Output is correct |
25 |
Correct |
142 ms |
10088 KB |
Output is correct |
26 |
Correct |
68 ms |
9876 KB |
Output is correct |
27 |
Correct |
76 ms |
10092 KB |
Output is correct |
28 |
Correct |
80 ms |
9896 KB |
Output is correct |
29 |
Correct |
111 ms |
9964 KB |
Output is correct |
30 |
Correct |
154 ms |
9964 KB |
Output is correct |
31 |
Correct |
90 ms |
9912 KB |
Output is correct |
32 |
Correct |
165 ms |
9980 KB |
Output is correct |
33 |
Correct |
143 ms |
10020 KB |
Output is correct |
34 |
Correct |
69 ms |
9872 KB |
Output is correct |
35 |
Correct |
121 ms |
10108 KB |
Output is correct |
36 |
Correct |
124 ms |
10056 KB |
Output is correct |
37 |
Correct |
121 ms |
9964 KB |
Output is correct |
38 |
Correct |
122 ms |
9968 KB |
Output is correct |
39 |
Correct |
132 ms |
9968 KB |
Output is correct |
40 |
Correct |
127 ms |
9804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
312 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
272 ms |
4908 KB |
Output is correct |
12 |
Correct |
289 ms |
4640 KB |
Output is correct |
13 |
Correct |
402 ms |
3880 KB |
Output is correct |
14 |
Correct |
492 ms |
4008 KB |
Output is correct |
15 |
Correct |
369 ms |
5096 KB |
Output is correct |
16 |
Correct |
501 ms |
4172 KB |
Output is correct |
17 |
Correct |
464 ms |
4200 KB |
Output is correct |
18 |
Correct |
233 ms |
5936 KB |
Output is correct |
19 |
Correct |
270 ms |
3036 KB |
Output is correct |
20 |
Correct |
296 ms |
4684 KB |
Output is correct |
21 |
Correct |
335 ms |
5052 KB |
Output is correct |
22 |
Correct |
383 ms |
5184 KB |
Output is correct |
23 |
Correct |
591 ms |
4216 KB |
Output is correct |
24 |
Correct |
776 ms |
4068 KB |
Output is correct |
25 |
Correct |
429 ms |
6092 KB |
Output is correct |
26 |
Correct |
707 ms |
4512 KB |
Output is correct |
27 |
Correct |
633 ms |
4564 KB |
Output is correct |
28 |
Correct |
252 ms |
7020 KB |
Output is correct |
29 |
Correct |
320 ms |
3020 KB |
Output is correct |
30 |
Correct |
352 ms |
5132 KB |
Output is correct |
31 |
Correct |
561 ms |
5024 KB |
Output is correct |
32 |
Correct |
811 ms |
5004 KB |
Output is correct |
33 |
Correct |
451 ms |
3916 KB |
Output is correct |
34 |
Correct |
721 ms |
3880 KB |
Output is correct |
35 |
Correct |
631 ms |
4388 KB |
Output is correct |
36 |
Correct |
253 ms |
2768 KB |
Output is correct |
37 |
Correct |
326 ms |
4856 KB |
Output is correct |
38 |
Correct |
342 ms |
2768 KB |
Output is correct |
39 |
Correct |
574 ms |
3924 KB |
Output is correct |
40 |
Correct |
773 ms |
3688 KB |
Output is correct |
41 |
Correct |
81 ms |
10056 KB |
Output is correct |
42 |
Correct |
77 ms |
10168 KB |
Output is correct |
43 |
Correct |
112 ms |
10040 KB |
Output is correct |
44 |
Correct |
176 ms |
10040 KB |
Output is correct |
45 |
Correct |
96 ms |
10164 KB |
Output is correct |
46 |
Correct |
157 ms |
10040 KB |
Output is correct |
47 |
Correct |
151 ms |
10188 KB |
Output is correct |
48 |
Correct |
67 ms |
10292 KB |
Output is correct |
49 |
Correct |
77 ms |
9916 KB |
Output is correct |
50 |
Correct |
76 ms |
10060 KB |
Output is correct |
51 |
Correct |
118 ms |
9984 KB |
Output is correct |
52 |
Correct |
165 ms |
9972 KB |
Output is correct |
53 |
Correct |
100 ms |
9904 KB |
Output is correct |
54 |
Correct |
161 ms |
10024 KB |
Output is correct |
55 |
Correct |
142 ms |
10088 KB |
Output is correct |
56 |
Correct |
68 ms |
9876 KB |
Output is correct |
57 |
Correct |
76 ms |
10092 KB |
Output is correct |
58 |
Correct |
80 ms |
9896 KB |
Output is correct |
59 |
Correct |
111 ms |
9964 KB |
Output is correct |
60 |
Correct |
154 ms |
9964 KB |
Output is correct |
61 |
Correct |
90 ms |
9912 KB |
Output is correct |
62 |
Correct |
165 ms |
9980 KB |
Output is correct |
63 |
Correct |
143 ms |
10020 KB |
Output is correct |
64 |
Correct |
69 ms |
9872 KB |
Output is correct |
65 |
Correct |
121 ms |
10108 KB |
Output is correct |
66 |
Correct |
124 ms |
10056 KB |
Output is correct |
67 |
Correct |
121 ms |
9964 KB |
Output is correct |
68 |
Correct |
122 ms |
9968 KB |
Output is correct |
69 |
Correct |
132 ms |
9968 KB |
Output is correct |
70 |
Correct |
127 ms |
9804 KB |
Output is correct |
71 |
Correct |
557 ms |
14856 KB |
Output is correct |
72 |
Correct |
591 ms |
14884 KB |
Output is correct |
73 |
Correct |
1266 ms |
13420 KB |
Output is correct |
74 |
Execution timed out |
2067 ms |
12568 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |