#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 20;
int n, q;
string s, t;
int dp[(1 << maxN) + 5];
int f[(1 << maxN) + 5];
void WriteInt(int x)
{
if (x > 9)
WriteInt(x / 10);
putchar(x % 10 + '0');
}
inline int inv(int i){
return ((1 << n) - 1) ^ i;
}
void Init(){
cin >> n >> q >> s;
for (int i = 0; i < int(s.length()); ++i){
dp[i] = s[i] - '0';
f[inv(i)] = dp[i];
}
for (int i = 0; i < n; ++i){
for (int j = 1; j < 1 << n; ++j){
if (j & (1 << i)){
dp[j] += dp[j ^ (1 << i)];
}
}
}
for (int i = 0; i < n; ++i){
for (int j = 0; j < 1 << n; ++j){
if (j & (1 << i)){
f[j] += f[j ^ (1 << i)];
}
}
}
int x, y, z;
int mask1, mask0, tem;
while (q--){
cin >> t;
x = y = z = mask1 = mask0 = tem = 0;
reverse(t.begin(), t.end());
for (int i = 0; i < n; ++i){
if (t[i] == '0') ++x, mask0 ^= 1 << i;
if (t[i] == '1') ++y, mask1 ^= 1 << i;
if (t[i] == '?') ++z, tem ^= 1 << i;
}
if (z == min(min(x, y), z)){
//brute all ?s
int ans = s[mask1] - '0';
for (int i = tem; i; i = (i - 1) & tem){
ans += s[i + mask1] - '0';
}
WriteInt(ans);
putchar('\n');
}
else if (x == min(x, y)){
int ans = -f[tem];
for (int i = mask0; i; i = (i - 1) & mask0){
bool cnt = __builtin_parity(i);
if (cnt) ans += f[i + tem];
else ans -= f[i + tem];
}
ans = abs(ans);
WriteInt(ans);
putchar('\n');
}
else{
int ans = -dp[tem];
for (int i = mask1; i; i = (i - 1) & mask1){
bool cnt = __builtin_parity(i);
if (cnt) ans += dp[i + tem];
else ans -= dp[i + tem];
}
ans = abs(ans);
WriteInt(ans);
putchar('\n');
}
}
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
154 ms |
4296 KB |
Output is correct |
12 |
Correct |
157 ms |
3900 KB |
Output is correct |
13 |
Correct |
202 ms |
3188 KB |
Output is correct |
14 |
Correct |
175 ms |
3324 KB |
Output is correct |
15 |
Correct |
158 ms |
4284 KB |
Output is correct |
16 |
Correct |
181 ms |
3548 KB |
Output is correct |
17 |
Correct |
185 ms |
3492 KB |
Output is correct |
18 |
Correct |
119 ms |
5300 KB |
Output is correct |
19 |
Correct |
140 ms |
2380 KB |
Output is correct |
20 |
Correct |
160 ms |
3968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
154 ms |
4296 KB |
Output is correct |
12 |
Correct |
157 ms |
3900 KB |
Output is correct |
13 |
Correct |
202 ms |
3188 KB |
Output is correct |
14 |
Correct |
175 ms |
3324 KB |
Output is correct |
15 |
Correct |
158 ms |
4284 KB |
Output is correct |
16 |
Correct |
181 ms |
3548 KB |
Output is correct |
17 |
Correct |
185 ms |
3492 KB |
Output is correct |
18 |
Correct |
119 ms |
5300 KB |
Output is correct |
19 |
Correct |
140 ms |
2380 KB |
Output is correct |
20 |
Correct |
160 ms |
3968 KB |
Output is correct |
21 |
Correct |
191 ms |
4384 KB |
Output is correct |
22 |
Correct |
210 ms |
4528 KB |
Output is correct |
23 |
Correct |
223 ms |
3532 KB |
Output is correct |
24 |
Correct |
242 ms |
3320 KB |
Output is correct |
25 |
Correct |
199 ms |
5352 KB |
Output is correct |
26 |
Correct |
247 ms |
3872 KB |
Output is correct |
27 |
Correct |
242 ms |
3936 KB |
Output is correct |
28 |
Correct |
154 ms |
6412 KB |
Output is correct |
29 |
Correct |
180 ms |
2384 KB |
Output is correct |
30 |
Correct |
199 ms |
4488 KB |
Output is correct |
31 |
Correct |
231 ms |
4364 KB |
Output is correct |
32 |
Correct |
244 ms |
4424 KB |
Output is correct |
33 |
Correct |
187 ms |
3224 KB |
Output is correct |
34 |
Correct |
238 ms |
3368 KB |
Output is correct |
35 |
Correct |
240 ms |
3856 KB |
Output is correct |
36 |
Correct |
133 ms |
2304 KB |
Output is correct |
37 |
Correct |
186 ms |
4324 KB |
Output is correct |
38 |
Correct |
196 ms |
2364 KB |
Output is correct |
39 |
Correct |
224 ms |
3592 KB |
Output is correct |
40 |
Correct |
237 ms |
3388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
58 ms |
10464 KB |
Output is correct |
12 |
Correct |
65 ms |
11968 KB |
Output is correct |
13 |
Correct |
63 ms |
11892 KB |
Output is correct |
14 |
Correct |
67 ms |
11824 KB |
Output is correct |
15 |
Correct |
62 ms |
11976 KB |
Output is correct |
16 |
Correct |
71 ms |
11844 KB |
Output is correct |
17 |
Correct |
66 ms |
11844 KB |
Output is correct |
18 |
Correct |
54 ms |
12076 KB |
Output is correct |
19 |
Correct |
53 ms |
11816 KB |
Output is correct |
20 |
Correct |
56 ms |
11944 KB |
Output is correct |
21 |
Correct |
58 ms |
11972 KB |
Output is correct |
22 |
Correct |
63 ms |
11860 KB |
Output is correct |
23 |
Correct |
58 ms |
11848 KB |
Output is correct |
24 |
Correct |
65 ms |
11844 KB |
Output is correct |
25 |
Correct |
65 ms |
11868 KB |
Output is correct |
26 |
Correct |
48 ms |
11712 KB |
Output is correct |
27 |
Correct |
58 ms |
12008 KB |
Output is correct |
28 |
Correct |
55 ms |
11828 KB |
Output is correct |
29 |
Correct |
58 ms |
11892 KB |
Output is correct |
30 |
Correct |
59 ms |
11844 KB |
Output is correct |
31 |
Correct |
57 ms |
11788 KB |
Output is correct |
32 |
Correct |
68 ms |
11884 KB |
Output is correct |
33 |
Correct |
70 ms |
11840 KB |
Output is correct |
34 |
Correct |
49 ms |
11716 KB |
Output is correct |
35 |
Correct |
60 ms |
11876 KB |
Output is correct |
36 |
Correct |
61 ms |
11860 KB |
Output is correct |
37 |
Correct |
60 ms |
11832 KB |
Output is correct |
38 |
Correct |
75 ms |
11936 KB |
Output is correct |
39 |
Correct |
63 ms |
11840 KB |
Output is correct |
40 |
Correct |
59 ms |
11840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
154 ms |
4296 KB |
Output is correct |
12 |
Correct |
157 ms |
3900 KB |
Output is correct |
13 |
Correct |
202 ms |
3188 KB |
Output is correct |
14 |
Correct |
175 ms |
3324 KB |
Output is correct |
15 |
Correct |
158 ms |
4284 KB |
Output is correct |
16 |
Correct |
181 ms |
3548 KB |
Output is correct |
17 |
Correct |
185 ms |
3492 KB |
Output is correct |
18 |
Correct |
119 ms |
5300 KB |
Output is correct |
19 |
Correct |
140 ms |
2380 KB |
Output is correct |
20 |
Correct |
160 ms |
3968 KB |
Output is correct |
21 |
Correct |
191 ms |
4384 KB |
Output is correct |
22 |
Correct |
210 ms |
4528 KB |
Output is correct |
23 |
Correct |
223 ms |
3532 KB |
Output is correct |
24 |
Correct |
242 ms |
3320 KB |
Output is correct |
25 |
Correct |
199 ms |
5352 KB |
Output is correct |
26 |
Correct |
247 ms |
3872 KB |
Output is correct |
27 |
Correct |
242 ms |
3936 KB |
Output is correct |
28 |
Correct |
154 ms |
6412 KB |
Output is correct |
29 |
Correct |
180 ms |
2384 KB |
Output is correct |
30 |
Correct |
199 ms |
4488 KB |
Output is correct |
31 |
Correct |
231 ms |
4364 KB |
Output is correct |
32 |
Correct |
244 ms |
4424 KB |
Output is correct |
33 |
Correct |
187 ms |
3224 KB |
Output is correct |
34 |
Correct |
238 ms |
3368 KB |
Output is correct |
35 |
Correct |
240 ms |
3856 KB |
Output is correct |
36 |
Correct |
133 ms |
2304 KB |
Output is correct |
37 |
Correct |
186 ms |
4324 KB |
Output is correct |
38 |
Correct |
196 ms |
2364 KB |
Output is correct |
39 |
Correct |
224 ms |
3592 KB |
Output is correct |
40 |
Correct |
237 ms |
3388 KB |
Output is correct |
41 |
Correct |
58 ms |
10464 KB |
Output is correct |
42 |
Correct |
65 ms |
11968 KB |
Output is correct |
43 |
Correct |
63 ms |
11892 KB |
Output is correct |
44 |
Correct |
67 ms |
11824 KB |
Output is correct |
45 |
Correct |
62 ms |
11976 KB |
Output is correct |
46 |
Correct |
71 ms |
11844 KB |
Output is correct |
47 |
Correct |
66 ms |
11844 KB |
Output is correct |
48 |
Correct |
54 ms |
12076 KB |
Output is correct |
49 |
Correct |
53 ms |
11816 KB |
Output is correct |
50 |
Correct |
56 ms |
11944 KB |
Output is correct |
51 |
Correct |
58 ms |
11972 KB |
Output is correct |
52 |
Correct |
63 ms |
11860 KB |
Output is correct |
53 |
Correct |
58 ms |
11848 KB |
Output is correct |
54 |
Correct |
65 ms |
11844 KB |
Output is correct |
55 |
Correct |
65 ms |
11868 KB |
Output is correct |
56 |
Correct |
48 ms |
11712 KB |
Output is correct |
57 |
Correct |
58 ms |
12008 KB |
Output is correct |
58 |
Correct |
55 ms |
11828 KB |
Output is correct |
59 |
Correct |
58 ms |
11892 KB |
Output is correct |
60 |
Correct |
59 ms |
11844 KB |
Output is correct |
61 |
Correct |
57 ms |
11788 KB |
Output is correct |
62 |
Correct |
68 ms |
11884 KB |
Output is correct |
63 |
Correct |
70 ms |
11840 KB |
Output is correct |
64 |
Correct |
49 ms |
11716 KB |
Output is correct |
65 |
Correct |
60 ms |
11876 KB |
Output is correct |
66 |
Correct |
61 ms |
11860 KB |
Output is correct |
67 |
Correct |
60 ms |
11832 KB |
Output is correct |
68 |
Correct |
75 ms |
11936 KB |
Output is correct |
69 |
Correct |
63 ms |
11840 KB |
Output is correct |
70 |
Correct |
59 ms |
11840 KB |
Output is correct |
71 |
Correct |
346 ms |
36288 KB |
Output is correct |
72 |
Correct |
360 ms |
36568 KB |
Output is correct |
73 |
Correct |
394 ms |
34908 KB |
Output is correct |
74 |
Correct |
583 ms |
35328 KB |
Output is correct |
75 |
Correct |
344 ms |
37204 KB |
Output is correct |
76 |
Correct |
561 ms |
35536 KB |
Output is correct |
77 |
Correct |
550 ms |
35384 KB |
Output is correct |
78 |
Correct |
249 ms |
39272 KB |
Output is correct |
79 |
Correct |
313 ms |
33164 KB |
Output is correct |
80 |
Correct |
372 ms |
36420 KB |
Output is correct |
81 |
Correct |
439 ms |
36340 KB |
Output is correct |
82 |
Correct |
496 ms |
35200 KB |
Output is correct |
83 |
Correct |
306 ms |
34420 KB |
Output is correct |
84 |
Correct |
480 ms |
35200 KB |
Output is correct |
85 |
Correct |
524 ms |
35380 KB |
Output is correct |
86 |
Correct |
214 ms |
33228 KB |
Output is correct |
87 |
Correct |
330 ms |
36192 KB |
Output is correct |
88 |
Correct |
304 ms |
33260 KB |
Output is correct |
89 |
Correct |
393 ms |
34944 KB |
Output is correct |
90 |
Correct |
432 ms |
35364 KB |
Output is correct |
91 |
Correct |
338 ms |
34392 KB |
Output is correct |
92 |
Correct |
613 ms |
35564 KB |
Output is correct |
93 |
Correct |
515 ms |
35412 KB |
Output is correct |
94 |
Correct |
220 ms |
33240 KB |
Output is correct |
95 |
Correct |
451 ms |
35308 KB |
Output is correct |
96 |
Correct |
475 ms |
35328 KB |
Output is correct |
97 |
Correct |
467 ms |
35288 KB |
Output is correct |
98 |
Correct |
461 ms |
35368 KB |
Output is correct |
99 |
Correct |
467 ms |
35284 KB |
Output is correct |
100 |
Correct |
457 ms |
35376 KB |
Output is correct |