// JOI18_snake_escaping
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 22
#define BND 6
int n, q;
char s[1 << MAXN];
int sosub[1 << MAXN], sosup[1 << MAXN];
int32_t main(void)
{
fast_io;
cin >> n >> q >> s;
FOR(i, 0, 1 << n) sosub[i] = sosup[i] = s[i] = s[i] - '0';
FOR(i, 0, n)
{
FOR(mask, 0, 1 << n)
if (!(mask >> i & 1)) sosup[mask] += sosup[mask ^ (1 << i)];
for (int mask = (1 << n) - 1; mask >= 0; mask--)
if (mask >> i & 1) sosub[mask] += sosub[mask ^ (1 << i)];
}
while (q--)
{
char t[MAXN];
cin >> t;
int m0 = 0, m1 = 0, mq = 0;
FOR(i, 0, n)
{
if (t[n - i - 1] == '0') m0 |= (1 << i);
else if (t[n - i - 1] == '1') m1 |= (1 << i);
else mq |= (1 << i);
}
int cnt0 = __builtin_popcount(m0);
int cnt1 = __builtin_popcount(m1);
if (n - cnt0 - cnt1 <= BND)
{
int ans = s[m1];
for (int sub = mq; sub; sub = (sub - 1) & mq)
ans += s[m1 | sub];
cout << ans << endl;
continue;
}
if (cnt0 < cnt1)
{
int ans = sosup[m1];
for (int sub = m0; sub; sub = (sub - 1) & m0)
ans += (__builtin_popcount(sub) % 2 ? -1 : 1) * sosup[sub | m1];
cout << ans << endl;
}
else
{
int ans = 0;
for (int sub = m1; ; sub = (sub - 1) & m1)
{
ans += ((cnt1 - __builtin_popcount(sub)) % 2 ? -1 : 1) * sosub[mq | sub];
if (!sub) break;
}
cout << ans << endl;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
286 ms |
15052 KB |
Output is correct |
12 |
Correct |
300 ms |
14660 KB |
Output is correct |
13 |
Correct |
287 ms |
14000 KB |
Output is correct |
14 |
Correct |
312 ms |
14044 KB |
Output is correct |
15 |
Correct |
333 ms |
15028 KB |
Output is correct |
16 |
Correct |
301 ms |
14240 KB |
Output is correct |
17 |
Correct |
303 ms |
14204 KB |
Output is correct |
18 |
Correct |
202 ms |
16004 KB |
Output is correct |
19 |
Correct |
235 ms |
13124 KB |
Output is correct |
20 |
Correct |
291 ms |
14732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
286 ms |
15052 KB |
Output is correct |
12 |
Correct |
300 ms |
14660 KB |
Output is correct |
13 |
Correct |
287 ms |
14000 KB |
Output is correct |
14 |
Correct |
312 ms |
14044 KB |
Output is correct |
15 |
Correct |
333 ms |
15028 KB |
Output is correct |
16 |
Correct |
301 ms |
14240 KB |
Output is correct |
17 |
Correct |
303 ms |
14204 KB |
Output is correct |
18 |
Correct |
202 ms |
16004 KB |
Output is correct |
19 |
Correct |
235 ms |
13124 KB |
Output is correct |
20 |
Correct |
291 ms |
14732 KB |
Output is correct |
21 |
Correct |
281 ms |
18044 KB |
Output is correct |
22 |
Correct |
310 ms |
18168 KB |
Output is correct |
23 |
Correct |
343 ms |
17260 KB |
Output is correct |
24 |
Correct |
321 ms |
17160 KB |
Output is correct |
25 |
Correct |
304 ms |
19112 KB |
Output is correct |
26 |
Correct |
348 ms |
17592 KB |
Output is correct |
27 |
Correct |
350 ms |
17516 KB |
Output is correct |
28 |
Correct |
219 ms |
20072 KB |
Output is correct |
29 |
Correct |
261 ms |
16136 KB |
Output is correct |
30 |
Correct |
315 ms |
18336 KB |
Output is correct |
31 |
Correct |
384 ms |
18056 KB |
Output is correct |
32 |
Correct |
331 ms |
18024 KB |
Output is correct |
33 |
Correct |
294 ms |
16920 KB |
Output is correct |
34 |
Correct |
322 ms |
17048 KB |
Output is correct |
35 |
Correct |
353 ms |
17432 KB |
Output is correct |
36 |
Correct |
230 ms |
16076 KB |
Output is correct |
37 |
Correct |
346 ms |
17988 KB |
Output is correct |
38 |
Correct |
276 ms |
16060 KB |
Output is correct |
39 |
Correct |
318 ms |
17192 KB |
Output is correct |
40 |
Correct |
312 ms |
17024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
72 ms |
11836 KB |
Output is correct |
12 |
Correct |
71 ms |
11844 KB |
Output is correct |
13 |
Correct |
77 ms |
11724 KB |
Output is correct |
14 |
Correct |
96 ms |
11840 KB |
Output is correct |
15 |
Correct |
71 ms |
11848 KB |
Output is correct |
16 |
Correct |
103 ms |
11724 KB |
Output is correct |
17 |
Correct |
90 ms |
11840 KB |
Output is correct |
18 |
Correct |
63 ms |
11984 KB |
Output is correct |
19 |
Correct |
70 ms |
11600 KB |
Output is correct |
20 |
Correct |
73 ms |
11840 KB |
Output is correct |
21 |
Correct |
85 ms |
11760 KB |
Output is correct |
22 |
Correct |
96 ms |
11788 KB |
Output is correct |
23 |
Correct |
68 ms |
11716 KB |
Output is correct |
24 |
Correct |
80 ms |
11720 KB |
Output is correct |
25 |
Correct |
89 ms |
11716 KB |
Output is correct |
26 |
Correct |
64 ms |
11708 KB |
Output is correct |
27 |
Correct |
71 ms |
11792 KB |
Output is correct |
28 |
Correct |
68 ms |
11600 KB |
Output is correct |
29 |
Correct |
75 ms |
11724 KB |
Output is correct |
30 |
Correct |
77 ms |
11712 KB |
Output is correct |
31 |
Correct |
74 ms |
11860 KB |
Output is correct |
32 |
Correct |
100 ms |
11716 KB |
Output is correct |
33 |
Correct |
91 ms |
11712 KB |
Output is correct |
34 |
Correct |
63 ms |
11684 KB |
Output is correct |
35 |
Correct |
86 ms |
11716 KB |
Output is correct |
36 |
Correct |
88 ms |
11792 KB |
Output is correct |
37 |
Correct |
84 ms |
11748 KB |
Output is correct |
38 |
Correct |
84 ms |
11704 KB |
Output is correct |
39 |
Correct |
90 ms |
11728 KB |
Output is correct |
40 |
Correct |
86 ms |
11716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
286 ms |
15052 KB |
Output is correct |
12 |
Correct |
300 ms |
14660 KB |
Output is correct |
13 |
Correct |
287 ms |
14000 KB |
Output is correct |
14 |
Correct |
312 ms |
14044 KB |
Output is correct |
15 |
Correct |
333 ms |
15028 KB |
Output is correct |
16 |
Correct |
301 ms |
14240 KB |
Output is correct |
17 |
Correct |
303 ms |
14204 KB |
Output is correct |
18 |
Correct |
202 ms |
16004 KB |
Output is correct |
19 |
Correct |
235 ms |
13124 KB |
Output is correct |
20 |
Correct |
291 ms |
14732 KB |
Output is correct |
21 |
Correct |
281 ms |
18044 KB |
Output is correct |
22 |
Correct |
310 ms |
18168 KB |
Output is correct |
23 |
Correct |
343 ms |
17260 KB |
Output is correct |
24 |
Correct |
321 ms |
17160 KB |
Output is correct |
25 |
Correct |
304 ms |
19112 KB |
Output is correct |
26 |
Correct |
348 ms |
17592 KB |
Output is correct |
27 |
Correct |
350 ms |
17516 KB |
Output is correct |
28 |
Correct |
219 ms |
20072 KB |
Output is correct |
29 |
Correct |
261 ms |
16136 KB |
Output is correct |
30 |
Correct |
315 ms |
18336 KB |
Output is correct |
31 |
Correct |
384 ms |
18056 KB |
Output is correct |
32 |
Correct |
331 ms |
18024 KB |
Output is correct |
33 |
Correct |
294 ms |
16920 KB |
Output is correct |
34 |
Correct |
322 ms |
17048 KB |
Output is correct |
35 |
Correct |
353 ms |
17432 KB |
Output is correct |
36 |
Correct |
230 ms |
16076 KB |
Output is correct |
37 |
Correct |
346 ms |
17988 KB |
Output is correct |
38 |
Correct |
276 ms |
16060 KB |
Output is correct |
39 |
Correct |
318 ms |
17192 KB |
Output is correct |
40 |
Correct |
312 ms |
17024 KB |
Output is correct |
41 |
Correct |
72 ms |
11836 KB |
Output is correct |
42 |
Correct |
71 ms |
11844 KB |
Output is correct |
43 |
Correct |
77 ms |
11724 KB |
Output is correct |
44 |
Correct |
96 ms |
11840 KB |
Output is correct |
45 |
Correct |
71 ms |
11848 KB |
Output is correct |
46 |
Correct |
103 ms |
11724 KB |
Output is correct |
47 |
Correct |
90 ms |
11840 KB |
Output is correct |
48 |
Correct |
63 ms |
11984 KB |
Output is correct |
49 |
Correct |
70 ms |
11600 KB |
Output is correct |
50 |
Correct |
73 ms |
11840 KB |
Output is correct |
51 |
Correct |
85 ms |
11760 KB |
Output is correct |
52 |
Correct |
96 ms |
11788 KB |
Output is correct |
53 |
Correct |
68 ms |
11716 KB |
Output is correct |
54 |
Correct |
80 ms |
11720 KB |
Output is correct |
55 |
Correct |
89 ms |
11716 KB |
Output is correct |
56 |
Correct |
64 ms |
11708 KB |
Output is correct |
57 |
Correct |
71 ms |
11792 KB |
Output is correct |
58 |
Correct |
68 ms |
11600 KB |
Output is correct |
59 |
Correct |
75 ms |
11724 KB |
Output is correct |
60 |
Correct |
77 ms |
11712 KB |
Output is correct |
61 |
Correct |
74 ms |
11860 KB |
Output is correct |
62 |
Correct |
100 ms |
11716 KB |
Output is correct |
63 |
Correct |
91 ms |
11712 KB |
Output is correct |
64 |
Correct |
63 ms |
11684 KB |
Output is correct |
65 |
Correct |
86 ms |
11716 KB |
Output is correct |
66 |
Correct |
88 ms |
11792 KB |
Output is correct |
67 |
Correct |
84 ms |
11748 KB |
Output is correct |
68 |
Correct |
84 ms |
11704 KB |
Output is correct |
69 |
Correct |
90 ms |
11728 KB |
Output is correct |
70 |
Correct |
86 ms |
11716 KB |
Output is correct |
71 |
Correct |
407 ms |
15900 KB |
Output is correct |
72 |
Correct |
431 ms |
16196 KB |
Output is correct |
73 |
Correct |
510 ms |
14536 KB |
Output is correct |
74 |
Correct |
945 ms |
14940 KB |
Output is correct |
75 |
Correct |
439 ms |
16808 KB |
Output is correct |
76 |
Correct |
1020 ms |
15136 KB |
Output is correct |
77 |
Correct |
801 ms |
15028 KB |
Output is correct |
78 |
Correct |
305 ms |
18776 KB |
Output is correct |
79 |
Correct |
378 ms |
12836 KB |
Output is correct |
80 |
Correct |
480 ms |
16036 KB |
Output is correct |
81 |
Correct |
655 ms |
15896 KB |
Output is correct |
82 |
Correct |
936 ms |
14732 KB |
Output is correct |
83 |
Correct |
391 ms |
13892 KB |
Output is correct |
84 |
Correct |
539 ms |
14820 KB |
Output is correct |
85 |
Correct |
807 ms |
14920 KB |
Output is correct |
86 |
Correct |
282 ms |
12732 KB |
Output is correct |
87 |
Correct |
404 ms |
15676 KB |
Output is correct |
88 |
Correct |
415 ms |
12680 KB |
Output is correct |
89 |
Correct |
500 ms |
14484 KB |
Output is correct |
90 |
Correct |
564 ms |
14676 KB |
Output is correct |
91 |
Correct |
442 ms |
13812 KB |
Output is correct |
92 |
Correct |
1173 ms |
15008 KB |
Output is correct |
93 |
Correct |
820 ms |
14828 KB |
Output is correct |
94 |
Correct |
268 ms |
12592 KB |
Output is correct |
95 |
Correct |
702 ms |
14712 KB |
Output is correct |
96 |
Correct |
686 ms |
14856 KB |
Output is correct |
97 |
Correct |
686 ms |
14816 KB |
Output is correct |
98 |
Correct |
680 ms |
14612 KB |
Output is correct |
99 |
Correct |
717 ms |
14604 KB |
Output is correct |
100 |
Correct |
717 ms |
35224 KB |
Output is correct |