#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;
#ifdef LOCAL
#define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
#else
#define eprintf(...) 42
#endif
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const int N = (1 << 20);
int n, q;
char s[N];
char t[N];
int sub[N];
int sup[N];
char nextChar() {
char c = getchar();
while (!isdigit(c) && c != '?') c = getchar();
return c;
}
int main()
{
startTime = clock();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
scanf("%d%d", &n, &q);
for (int i = 0; i < (1 << n); i++)
s[i] = nextChar();
for (int i = 0; i < (1 << n); i++) {
sub[i] = (int)(s[i] - '0');
sup[i] = (int)(s[i] - '0');
}
for (int i = 0; i < n; i++)
for (int mask = 0; mask < (1 << n); mask++)
if (mask >> i & 1)
sub[mask] += sub[mask ^ (1 << i)];
for (int i = 0; i < n; i++)
for (int mask = (1 << n) - 1; mask >= 0; mask--)
if (!(mask >> i & 1))
sup[mask] += sup[mask ^ (1 << i)];
while(q--) {
for (int i = 0; i < n; i++)
t[i] = nextChar();
reverse(t, t + n);
vector<int> zero, one, qmark;
for (int i = 0; i < n; i++) {
if (t[i] == '0') zero.push_back(i);
if (t[i] == '1') one.push_back(i);
if (t[i] == '?') qmark.push_back(i);
}
int tmask = 0;
for (int i = 0; i < n; i++)
if (t[i] == '1')
tmask += (1 << i);
int ans = 0;
if (qmark.size() <= 6) {
int sz = qmark.size();
for (int mask = 0; mask < (1 << sz); mask++) {
int nmask = tmask;
for (int i = 0; i < sz; i++)
if (mask >> i & 1)
nmask += (1 << qmark[i]);
ans += (int)(s[nmask] - '0');
}
} else if (zero.size() <= 6) {
int sz = zero.size();
for (int mask = 0; mask < (1 << sz); mask++) {
int nmask = tmask;
for (int i = 0; i < sz; i++)
if (mask >> i & 1)
nmask += (1 << zero[i]);
ans += sup[nmask] * (__builtin_popcount(mask) % 2 == 0 ? 1 : -1);
}
} else {
assert(one.size() <= 6);
int sz = one.size();
tmask = 0;
for (int i = 0; i < n; i++)
if (t[i] == '?')
tmask += (1 << i);
for (int mask = 0; mask < (1 << sz); mask++) {
int nmask = tmask;
for (int i = 0; i < sz; i++)
if (mask >> i & 1)
nmask += (1 << one[i]);
ans += sub[nmask] * ((sz - __builtin_popcount(mask)) % 2 == 0 ? 1 : -1);
}
}
printf("%d\n", ans);
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
778 ms |
4212 KB |
Output is correct |
12 |
Correct |
731 ms |
3968 KB |
Output is correct |
13 |
Correct |
609 ms |
3168 KB |
Output is correct |
14 |
Correct |
636 ms |
3276 KB |
Output is correct |
15 |
Correct |
1087 ms |
4352 KB |
Output is correct |
16 |
Correct |
731 ms |
3444 KB |
Output is correct |
17 |
Correct |
743 ms |
3448 KB |
Output is correct |
18 |
Correct |
303 ms |
5308 KB |
Output is correct |
19 |
Correct |
519 ms |
2296 KB |
Output is correct |
20 |
Correct |
798 ms |
3924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
778 ms |
4212 KB |
Output is correct |
12 |
Correct |
731 ms |
3968 KB |
Output is correct |
13 |
Correct |
609 ms |
3168 KB |
Output is correct |
14 |
Correct |
636 ms |
3276 KB |
Output is correct |
15 |
Correct |
1087 ms |
4352 KB |
Output is correct |
16 |
Correct |
731 ms |
3444 KB |
Output is correct |
17 |
Correct |
743 ms |
3448 KB |
Output is correct |
18 |
Correct |
303 ms |
5308 KB |
Output is correct |
19 |
Correct |
519 ms |
2296 KB |
Output is correct |
20 |
Correct |
798 ms |
3924 KB |
Output is correct |
21 |
Correct |
628 ms |
4284 KB |
Output is correct |
22 |
Correct |
1002 ms |
4552 KB |
Output is correct |
23 |
Correct |
831 ms |
3568 KB |
Output is correct |
24 |
Correct |
764 ms |
3300 KB |
Output is correct |
25 |
Correct |
661 ms |
5380 KB |
Output is correct |
26 |
Correct |
925 ms |
3852 KB |
Output is correct |
27 |
Correct |
898 ms |
3952 KB |
Output is correct |
28 |
Correct |
348 ms |
6468 KB |
Output is correct |
29 |
Correct |
541 ms |
2372 KB |
Output is correct |
30 |
Correct |
840 ms |
4480 KB |
Output is correct |
31 |
Correct |
1234 ms |
4304 KB |
Output is correct |
32 |
Correct |
893 ms |
4372 KB |
Output is correct |
33 |
Correct |
648 ms |
3320 KB |
Output is correct |
34 |
Correct |
812 ms |
3364 KB |
Output is correct |
35 |
Correct |
985 ms |
3848 KB |
Output is correct |
36 |
Correct |
332 ms |
2396 KB |
Output is correct |
37 |
Correct |
1157 ms |
4468 KB |
Output is correct |
38 |
Correct |
614 ms |
2324 KB |
Output is correct |
39 |
Correct |
888 ms |
3552 KB |
Output is correct |
40 |
Correct |
852 ms |
3396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
104 ms |
9664 KB |
Output is correct |
12 |
Correct |
115 ms |
9844 KB |
Output is correct |
13 |
Correct |
138 ms |
9644 KB |
Output is correct |
14 |
Correct |
169 ms |
9708 KB |
Output is correct |
15 |
Correct |
100 ms |
9776 KB |
Output is correct |
16 |
Correct |
138 ms |
9636 KB |
Output is correct |
17 |
Correct |
129 ms |
9696 KB |
Output is correct |
18 |
Correct |
80 ms |
9904 KB |
Output is correct |
19 |
Correct |
98 ms |
9568 KB |
Output is correct |
20 |
Correct |
117 ms |
9732 KB |
Output is correct |
21 |
Correct |
140 ms |
9660 KB |
Output is correct |
22 |
Correct |
167 ms |
9712 KB |
Output is correct |
23 |
Correct |
132 ms |
9664 KB |
Output is correct |
24 |
Correct |
144 ms |
9664 KB |
Output is correct |
25 |
Correct |
141 ms |
9708 KB |
Output is correct |
26 |
Correct |
74 ms |
9532 KB |
Output is correct |
27 |
Correct |
115 ms |
9696 KB |
Output is correct |
28 |
Correct |
106 ms |
9516 KB |
Output is correct |
29 |
Correct |
117 ms |
9660 KB |
Output is correct |
30 |
Correct |
141 ms |
9644 KB |
Output is correct |
31 |
Correct |
101 ms |
9660 KB |
Output is correct |
32 |
Correct |
161 ms |
9704 KB |
Output is correct |
33 |
Correct |
138 ms |
9712 KB |
Output is correct |
34 |
Correct |
70 ms |
9532 KB |
Output is correct |
35 |
Correct |
127 ms |
9676 KB |
Output is correct |
36 |
Correct |
155 ms |
9720 KB |
Output is correct |
37 |
Correct |
135 ms |
9624 KB |
Output is correct |
38 |
Correct |
140 ms |
9656 KB |
Output is correct |
39 |
Correct |
115 ms |
9788 KB |
Output is correct |
40 |
Correct |
132 ms |
9716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
778 ms |
4212 KB |
Output is correct |
12 |
Correct |
731 ms |
3968 KB |
Output is correct |
13 |
Correct |
609 ms |
3168 KB |
Output is correct |
14 |
Correct |
636 ms |
3276 KB |
Output is correct |
15 |
Correct |
1087 ms |
4352 KB |
Output is correct |
16 |
Correct |
731 ms |
3444 KB |
Output is correct |
17 |
Correct |
743 ms |
3448 KB |
Output is correct |
18 |
Correct |
303 ms |
5308 KB |
Output is correct |
19 |
Correct |
519 ms |
2296 KB |
Output is correct |
20 |
Correct |
798 ms |
3924 KB |
Output is correct |
21 |
Correct |
628 ms |
4284 KB |
Output is correct |
22 |
Correct |
1002 ms |
4552 KB |
Output is correct |
23 |
Correct |
831 ms |
3568 KB |
Output is correct |
24 |
Correct |
764 ms |
3300 KB |
Output is correct |
25 |
Correct |
661 ms |
5380 KB |
Output is correct |
26 |
Correct |
925 ms |
3852 KB |
Output is correct |
27 |
Correct |
898 ms |
3952 KB |
Output is correct |
28 |
Correct |
348 ms |
6468 KB |
Output is correct |
29 |
Correct |
541 ms |
2372 KB |
Output is correct |
30 |
Correct |
840 ms |
4480 KB |
Output is correct |
31 |
Correct |
1234 ms |
4304 KB |
Output is correct |
32 |
Correct |
893 ms |
4372 KB |
Output is correct |
33 |
Correct |
648 ms |
3320 KB |
Output is correct |
34 |
Correct |
812 ms |
3364 KB |
Output is correct |
35 |
Correct |
985 ms |
3848 KB |
Output is correct |
36 |
Correct |
332 ms |
2396 KB |
Output is correct |
37 |
Correct |
1157 ms |
4468 KB |
Output is correct |
38 |
Correct |
614 ms |
2324 KB |
Output is correct |
39 |
Correct |
888 ms |
3552 KB |
Output is correct |
40 |
Correct |
852 ms |
3396 KB |
Output is correct |
41 |
Correct |
104 ms |
9664 KB |
Output is correct |
42 |
Correct |
115 ms |
9844 KB |
Output is correct |
43 |
Correct |
138 ms |
9644 KB |
Output is correct |
44 |
Correct |
169 ms |
9708 KB |
Output is correct |
45 |
Correct |
100 ms |
9776 KB |
Output is correct |
46 |
Correct |
138 ms |
9636 KB |
Output is correct |
47 |
Correct |
129 ms |
9696 KB |
Output is correct |
48 |
Correct |
80 ms |
9904 KB |
Output is correct |
49 |
Correct |
98 ms |
9568 KB |
Output is correct |
50 |
Correct |
117 ms |
9732 KB |
Output is correct |
51 |
Correct |
140 ms |
9660 KB |
Output is correct |
52 |
Correct |
167 ms |
9712 KB |
Output is correct |
53 |
Correct |
132 ms |
9664 KB |
Output is correct |
54 |
Correct |
144 ms |
9664 KB |
Output is correct |
55 |
Correct |
141 ms |
9708 KB |
Output is correct |
56 |
Correct |
74 ms |
9532 KB |
Output is correct |
57 |
Correct |
115 ms |
9696 KB |
Output is correct |
58 |
Correct |
106 ms |
9516 KB |
Output is correct |
59 |
Correct |
117 ms |
9660 KB |
Output is correct |
60 |
Correct |
141 ms |
9644 KB |
Output is correct |
61 |
Correct |
101 ms |
9660 KB |
Output is correct |
62 |
Correct |
161 ms |
9704 KB |
Output is correct |
63 |
Correct |
138 ms |
9712 KB |
Output is correct |
64 |
Correct |
70 ms |
9532 KB |
Output is correct |
65 |
Correct |
127 ms |
9676 KB |
Output is correct |
66 |
Correct |
155 ms |
9720 KB |
Output is correct |
67 |
Correct |
135 ms |
9624 KB |
Output is correct |
68 |
Correct |
140 ms |
9656 KB |
Output is correct |
69 |
Correct |
115 ms |
9788 KB |
Output is correct |
70 |
Correct |
132 ms |
9716 KB |
Output is correct |
71 |
Correct |
1085 ms |
14432 KB |
Output is correct |
72 |
Correct |
1018 ms |
14668 KB |
Output is correct |
73 |
Correct |
1377 ms |
13180 KB |
Output is correct |
74 |
Execution timed out |
2096 ms |
13060 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |