//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) (int)a.size()
#define s second
#define f first
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<pii> rid = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<pii> dir = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
const int N = 2e5 + 1, mod = 1e9 + 7;
const ll inf = 1e9;
double eps = 1e-15;
bool flg = 0;
int dp[2][(1 << 20)];
int f(int x, int mk, vector<int> &s) {
int ans = dp[x][mk];
for (int msk = 1; msk < (1 << sz(s)); msk++) {
int mask = mk;
for (int i = 0; i < sz(s); i++) {
if (msk >> i & 1) mask += (1 << s[i]);
}
if ((__builtin_popcount(msk)) & 1) ans -= dp[x][mask];
else ans += dp[x][mask];
}
return ans;
}
void slv() {
int l, q;
cin >> l >> q;
string s;
cin >> s;
for (int i = 0; i < sz(s); i++) {
int m0 = 0, m1 = 0;
for (int b = 0; b < l; b++) {
if (i >> b & 1) m1 += (1 << b);
else m0 += (1 << b);
}
dp[0][m0] += s[i] - '0';
dp[1][m1] += s[i] - '0';
}
for (int i = 0; i < l; i++) {
for (int msk = 0; msk < (1 << l); msk++) {
if (!(msk >> i & 1)) {
dp[1][msk] += dp[1][msk + (1 << i)];
dp[0][msk] += dp[0][msk + (1 << i)];
}
}
}
while (q--) {
string h;
cin >> h;
vector<int> p, p0, p1;
int msk0 = 0, msk1 = 0;
for (int i = 0; i < sz(h); i++) {
if (h[i] == '0') {
p0.push_back(sz(h) - i - 1);
msk0 += (1 << p0.back());
}
if (h[i] == '1') {
p1.push_back(sz(h) - i - 1);
msk1 += (1 << p1.back());
}
if (h[i] == '?') p.push_back(sz(h) - i - 1);
}
if (sz(p) < 7) {
int ans = 0;
for (int mask = 0; mask < (1 << sz(p)); mask++) {
int msk = msk1;
for (int i = 0; i < sz(p); i++) {
if (mask >> i & 1) msk += (1 << p[i]);
}
ans += s[msk] - '0';
}
cout << ans << '\n';
continue;
}
if (sz(p0) > sz(p1)) cout << f(0, msk0, p1) << '\n';
else cout << f(1, msk1, p0) << '\n';
}
}
main() {
//freopen("rsq.in", "r", stdin);
//freopen("rsq.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int tp = 1;
if (flg) cin >> tp;
while (tp--) {
slv();
}
}
//wenomechainsama
Compilation message
snake_escaping.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
101 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
561 ms |
9952 KB |
Output is correct |
12 |
Correct |
540 ms |
9912 KB |
Output is correct |
13 |
Correct |
435 ms |
8888 KB |
Output is correct |
14 |
Correct |
434 ms |
9000 KB |
Output is correct |
15 |
Correct |
835 ms |
10068 KB |
Output is correct |
16 |
Correct |
513 ms |
9288 KB |
Output is correct |
17 |
Correct |
510 ms |
9144 KB |
Output is correct |
18 |
Correct |
258 ms |
11088 KB |
Output is correct |
19 |
Correct |
349 ms |
7984 KB |
Output is correct |
20 |
Correct |
558 ms |
9724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
561 ms |
9952 KB |
Output is correct |
12 |
Correct |
540 ms |
9912 KB |
Output is correct |
13 |
Correct |
435 ms |
8888 KB |
Output is correct |
14 |
Correct |
434 ms |
9000 KB |
Output is correct |
15 |
Correct |
835 ms |
10068 KB |
Output is correct |
16 |
Correct |
513 ms |
9288 KB |
Output is correct |
17 |
Correct |
510 ms |
9144 KB |
Output is correct |
18 |
Correct |
258 ms |
11088 KB |
Output is correct |
19 |
Correct |
349 ms |
7984 KB |
Output is correct |
20 |
Correct |
558 ms |
9724 KB |
Output is correct |
21 |
Correct |
396 ms |
11700 KB |
Output is correct |
22 |
Correct |
559 ms |
11876 KB |
Output is correct |
23 |
Correct |
584 ms |
10828 KB |
Output is correct |
24 |
Correct |
546 ms |
10524 KB |
Output is correct |
25 |
Correct |
442 ms |
12644 KB |
Output is correct |
26 |
Correct |
676 ms |
11064 KB |
Output is correct |
27 |
Correct |
650 ms |
11344 KB |
Output is correct |
28 |
Correct |
273 ms |
13500 KB |
Output is correct |
29 |
Correct |
380 ms |
9560 KB |
Output is correct |
30 |
Correct |
557 ms |
12096 KB |
Output is correct |
31 |
Correct |
999 ms |
11544 KB |
Output is correct |
32 |
Correct |
651 ms |
12152 KB |
Output is correct |
33 |
Correct |
447 ms |
10756 KB |
Output is correct |
34 |
Correct |
560 ms |
10836 KB |
Output is correct |
35 |
Correct |
645 ms |
11144 KB |
Output is correct |
36 |
Correct |
263 ms |
9524 KB |
Output is correct |
37 |
Correct |
844 ms |
11740 KB |
Output is correct |
38 |
Correct |
397 ms |
9708 KB |
Output is correct |
39 |
Correct |
585 ms |
10716 KB |
Output is correct |
40 |
Correct |
560 ms |
10932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
120 ms |
11252 KB |
Output is correct |
12 |
Correct |
116 ms |
11164 KB |
Output is correct |
13 |
Correct |
153 ms |
11252 KB |
Output is correct |
14 |
Correct |
154 ms |
11252 KB |
Output is correct |
15 |
Correct |
127 ms |
11248 KB |
Output is correct |
16 |
Correct |
149 ms |
11252 KB |
Output is correct |
17 |
Correct |
153 ms |
11248 KB |
Output is correct |
18 |
Correct |
106 ms |
11248 KB |
Output is correct |
19 |
Correct |
118 ms |
10940 KB |
Output is correct |
20 |
Correct |
117 ms |
11256 KB |
Output is correct |
21 |
Correct |
129 ms |
11252 KB |
Output is correct |
22 |
Correct |
160 ms |
11248 KB |
Output is correct |
23 |
Correct |
127 ms |
10992 KB |
Output is correct |
24 |
Correct |
173 ms |
11252 KB |
Output is correct |
25 |
Correct |
151 ms |
11196 KB |
Output is correct |
26 |
Correct |
104 ms |
11008 KB |
Output is correct |
27 |
Correct |
121 ms |
11312 KB |
Output is correct |
28 |
Correct |
117 ms |
11004 KB |
Output is correct |
29 |
Correct |
135 ms |
11248 KB |
Output is correct |
30 |
Correct |
158 ms |
11256 KB |
Output is correct |
31 |
Correct |
140 ms |
11412 KB |
Output is correct |
32 |
Correct |
151 ms |
11248 KB |
Output is correct |
33 |
Correct |
140 ms |
11244 KB |
Output is correct |
34 |
Correct |
115 ms |
10992 KB |
Output is correct |
35 |
Correct |
141 ms |
11188 KB |
Output is correct |
36 |
Correct |
140 ms |
11252 KB |
Output is correct |
37 |
Correct |
164 ms |
11076 KB |
Output is correct |
38 |
Correct |
134 ms |
11252 KB |
Output is correct |
39 |
Correct |
135 ms |
11236 KB |
Output is correct |
40 |
Correct |
137 ms |
11080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
561 ms |
9952 KB |
Output is correct |
12 |
Correct |
540 ms |
9912 KB |
Output is correct |
13 |
Correct |
435 ms |
8888 KB |
Output is correct |
14 |
Correct |
434 ms |
9000 KB |
Output is correct |
15 |
Correct |
835 ms |
10068 KB |
Output is correct |
16 |
Correct |
513 ms |
9288 KB |
Output is correct |
17 |
Correct |
510 ms |
9144 KB |
Output is correct |
18 |
Correct |
258 ms |
11088 KB |
Output is correct |
19 |
Correct |
349 ms |
7984 KB |
Output is correct |
20 |
Correct |
558 ms |
9724 KB |
Output is correct |
21 |
Correct |
396 ms |
11700 KB |
Output is correct |
22 |
Correct |
559 ms |
11876 KB |
Output is correct |
23 |
Correct |
584 ms |
10828 KB |
Output is correct |
24 |
Correct |
546 ms |
10524 KB |
Output is correct |
25 |
Correct |
442 ms |
12644 KB |
Output is correct |
26 |
Correct |
676 ms |
11064 KB |
Output is correct |
27 |
Correct |
650 ms |
11344 KB |
Output is correct |
28 |
Correct |
273 ms |
13500 KB |
Output is correct |
29 |
Correct |
380 ms |
9560 KB |
Output is correct |
30 |
Correct |
557 ms |
12096 KB |
Output is correct |
31 |
Correct |
999 ms |
11544 KB |
Output is correct |
32 |
Correct |
651 ms |
12152 KB |
Output is correct |
33 |
Correct |
447 ms |
10756 KB |
Output is correct |
34 |
Correct |
560 ms |
10836 KB |
Output is correct |
35 |
Correct |
645 ms |
11144 KB |
Output is correct |
36 |
Correct |
263 ms |
9524 KB |
Output is correct |
37 |
Correct |
844 ms |
11740 KB |
Output is correct |
38 |
Correct |
397 ms |
9708 KB |
Output is correct |
39 |
Correct |
585 ms |
10716 KB |
Output is correct |
40 |
Correct |
560 ms |
10932 KB |
Output is correct |
41 |
Correct |
120 ms |
11252 KB |
Output is correct |
42 |
Correct |
116 ms |
11164 KB |
Output is correct |
43 |
Correct |
153 ms |
11252 KB |
Output is correct |
44 |
Correct |
154 ms |
11252 KB |
Output is correct |
45 |
Correct |
127 ms |
11248 KB |
Output is correct |
46 |
Correct |
149 ms |
11252 KB |
Output is correct |
47 |
Correct |
153 ms |
11248 KB |
Output is correct |
48 |
Correct |
106 ms |
11248 KB |
Output is correct |
49 |
Correct |
118 ms |
10940 KB |
Output is correct |
50 |
Correct |
117 ms |
11256 KB |
Output is correct |
51 |
Correct |
129 ms |
11252 KB |
Output is correct |
52 |
Correct |
160 ms |
11248 KB |
Output is correct |
53 |
Correct |
127 ms |
10992 KB |
Output is correct |
54 |
Correct |
173 ms |
11252 KB |
Output is correct |
55 |
Correct |
151 ms |
11196 KB |
Output is correct |
56 |
Correct |
104 ms |
11008 KB |
Output is correct |
57 |
Correct |
121 ms |
11312 KB |
Output is correct |
58 |
Correct |
117 ms |
11004 KB |
Output is correct |
59 |
Correct |
135 ms |
11248 KB |
Output is correct |
60 |
Correct |
158 ms |
11256 KB |
Output is correct |
61 |
Correct |
140 ms |
11412 KB |
Output is correct |
62 |
Correct |
151 ms |
11248 KB |
Output is correct |
63 |
Correct |
140 ms |
11244 KB |
Output is correct |
64 |
Correct |
115 ms |
10992 KB |
Output is correct |
65 |
Correct |
141 ms |
11188 KB |
Output is correct |
66 |
Correct |
140 ms |
11252 KB |
Output is correct |
67 |
Correct |
164 ms |
11076 KB |
Output is correct |
68 |
Correct |
134 ms |
11252 KB |
Output is correct |
69 |
Correct |
135 ms |
11236 KB |
Output is correct |
70 |
Correct |
137 ms |
11080 KB |
Output is correct |
71 |
Correct |
647 ms |
25684 KB |
Output is correct |
72 |
Correct |
675 ms |
25860 KB |
Output is correct |
73 |
Correct |
1008 ms |
24492 KB |
Output is correct |
74 |
Correct |
1493 ms |
24684 KB |
Output is correct |
75 |
Correct |
730 ms |
26820 KB |
Output is correct |
76 |
Correct |
1502 ms |
24980 KB |
Output is correct |
77 |
Correct |
1221 ms |
25036 KB |
Output is correct |
78 |
Correct |
441 ms |
28800 KB |
Output is correct |
79 |
Correct |
626 ms |
31152 KB |
Output is correct |
80 |
Correct |
688 ms |
36536 KB |
Output is correct |
81 |
Correct |
971 ms |
36372 KB |
Output is correct |
82 |
Correct |
1480 ms |
35540 KB |
Output is correct |
83 |
Correct |
760 ms |
34472 KB |
Output is correct |
84 |
Correct |
1266 ms |
35144 KB |
Output is correct |
85 |
Correct |
1263 ms |
35344 KB |
Output is correct |
86 |
Correct |
449 ms |
33268 KB |
Output is correct |
87 |
Correct |
686 ms |
36192 KB |
Output is correct |
88 |
Correct |
641 ms |
33288 KB |
Output is correct |
89 |
Correct |
941 ms |
34860 KB |
Output is correct |
90 |
Correct |
1224 ms |
35428 KB |
Output is correct |
91 |
Correct |
791 ms |
34620 KB |
Output is correct |
92 |
Correct |
1347 ms |
35676 KB |
Output is correct |
93 |
Correct |
1222 ms |
35516 KB |
Output is correct |
94 |
Correct |
427 ms |
33412 KB |
Output is correct |
95 |
Correct |
1022 ms |
35644 KB |
Output is correct |
96 |
Correct |
1074 ms |
35492 KB |
Output is correct |
97 |
Correct |
991 ms |
35340 KB |
Output is correct |
98 |
Correct |
1096 ms |
35356 KB |
Output is correct |
99 |
Correct |
999 ms |
35468 KB |
Output is correct |
100 |
Correct |
1038 ms |
35504 KB |
Output is correct |