#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
using namespace std;
using namespace __gnu_pbds;
#define orz
#ifdef orz
#define print(a...)cerr<<"Line "<<__LINE__<<":",kout("[" + string(#a) + "] = ", a)
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...); }
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
#else
#define print(a...)
void kout() {}
template<class T1, class ... T2> void kout(T1 a, T2 ... e) {}
#define printv(a, b)
#endif
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
int main(){
StarBurstStream
int L, q;
cin >> L >> q;
int n = 1 << L;
string ts;
cin >> ts;
vector<int> a(n);
for(int i = 0; i < n; i++){
a[i] = ts[i] - '0';
}
vector<int> one(n), zero(n);
for(int i = 0; i < n; i++){
int tmp = 0;
for(int j = 0; j < L; j++){
if(1 << j & i) tmp |= 1 << j;
}
one[tmp] += a[i];
zero[((1 << L) - 1) ^ tmp] += a [i];
}
auto solvedp = [&](vector<int>& dp){
for(int i = 0; i < L; i++){
for(int j = 0; j < n; j++){
if(1 << i & j) continue;
dp[j] += dp[j ^ (1 << i)];
}
}
};
solvedp(zero);
solvedp(one);
/*for(int i = 0; i < n; i++){
cerr << "zero " << bitset<5>(i) << ": " << zero[i] << "\n";
}*/
auto solveq = [&](string& s){
int now = 0;
vector<int> pos;
for(int i = 0; i < L; i++){
if(s[i] == '?') pos.eb(i);
else if(s[i] == '1') now |= 1 << (L - 1 - i);
}
function<ll(int)> dfs = [&](int i){
if(i == pos.size()){
//cerr << bitset<20>(now) << "\n";
return (ll)a[now];
}
int p = pos[i];
ll ans = 0;
ans += dfs(i + 1);
now ^= 1 << (L - 1 - p);
ans += dfs(i + 1);
now ^= 1 << (L - 1 - p);
return ans;
};
return dfs(0);
};
auto solve0 = [&](string& s){
int now = 0;
vector<int> pos;
for(int i = 0; i < L; i++){
if(s[i] == '0') pos.eb(i);
else if(s[i] == '1') now |= 1 << (L - 1 - i);
}
int cnt = 0;
function<ll(int)> dfs = [&](int i){
if(i == pos.size()){
//cerr << bitset<20>(now) << "\n";
return (ll)one[now] * (cnt % 2 ? -1 : 1);
}
int p = pos[i];
ll ans = 0;
ans += dfs(i + 1);
now ^= 1 << (L - 1 - p);
cnt++;
ans += dfs(i + 1);
now ^= 1 << (L - 1 - p);
cnt--;
return ans;
};
return dfs(0);
};
auto solve1 = [&](string& s){
int now = 0;
int cnt = 0;
vector<int> pos;
for(int i = 0; i < L; i++){
if(s[i] == '1') pos.eb(i);
else if(s[i] == '0') now |= 1 << (L - 1 - i);
}
//printv(pos, cerr);
function<ll(int)> dfs = [&](int i){
if(i == pos.size()){
//cerr << bitset<20>(now) << "\n";
return (ll)zero[now] * (cnt % 2 ? -1 : 1);
}
int p = pos[i];
ll ans = 0;
ans += dfs(i + 1);
now ^= 1 << (L - 1 - p);
cnt++;
ans += dfs(i + 1);
now ^= 1 << (L - 1 - p);
cnt--;
return ans;
};
return dfs(0);
};
while(q--){
string s;
cin >> s;
int o = 0, z = 0, qm = 0;
for(char i : s){
if(i == '0') z++;
else if(i == '1') o++;
else qm++;
}
int mn = min({o, z, qm});
ll ans = 0;
if(mn == z) ans = solve0(s);
else if(mn == o) ans = solve1(s);
else ans = solveq(s);
//ans = solve1(s);
cout << ans << "\n";
}
return 0;
}
Compilation message
snake_escaping.cpp: In lambda function:
snake_escaping.cpp:123:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | if(i == pos.size()){
| ~~^~~~~~~~~~~~~
snake_escaping.cpp: In lambda function:
snake_escaping.cpp:146:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
146 | if(i == pos.size()){
| ~~^~~~~~~~~~~~~
snake_escaping.cpp: In lambda function:
snake_escaping.cpp:172:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
172 | if(i == pos.size()){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 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 |
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 |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 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 |
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 |
293 ms |
4540 KB |
Output is correct |
12 |
Correct |
345 ms |
4232 KB |
Output is correct |
13 |
Correct |
389 ms |
3524 KB |
Output is correct |
14 |
Correct |
519 ms |
3636 KB |
Output is correct |
15 |
Correct |
344 ms |
4556 KB |
Output is correct |
16 |
Correct |
501 ms |
3704 KB |
Output is correct |
17 |
Correct |
441 ms |
3632 KB |
Output is correct |
18 |
Correct |
189 ms |
5660 KB |
Output is correct |
19 |
Correct |
262 ms |
2520 KB |
Output is correct |
20 |
Correct |
284 ms |
4296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 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 |
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 |
293 ms |
4540 KB |
Output is correct |
12 |
Correct |
345 ms |
4232 KB |
Output is correct |
13 |
Correct |
389 ms |
3524 KB |
Output is correct |
14 |
Correct |
519 ms |
3636 KB |
Output is correct |
15 |
Correct |
344 ms |
4556 KB |
Output is correct |
16 |
Correct |
501 ms |
3704 KB |
Output is correct |
17 |
Correct |
441 ms |
3632 KB |
Output is correct |
18 |
Correct |
189 ms |
5660 KB |
Output is correct |
19 |
Correct |
262 ms |
2520 KB |
Output is correct |
20 |
Correct |
284 ms |
4296 KB |
Output is correct |
21 |
Correct |
307 ms |
4616 KB |
Output is correct |
22 |
Correct |
338 ms |
4768 KB |
Output is correct |
23 |
Correct |
517 ms |
3852 KB |
Output is correct |
24 |
Correct |
716 ms |
3792 KB |
Output is correct |
25 |
Correct |
399 ms |
5632 KB |
Output is correct |
26 |
Correct |
654 ms |
4080 KB |
Output is correct |
27 |
Correct |
604 ms |
4032 KB |
Output is correct |
28 |
Correct |
205 ms |
6700 KB |
Output is correct |
29 |
Correct |
317 ms |
2592 KB |
Output is correct |
30 |
Correct |
328 ms |
4732 KB |
Output is correct |
31 |
Correct |
525 ms |
4652 KB |
Output is correct |
32 |
Correct |
757 ms |
4676 KB |
Output is correct |
33 |
Correct |
393 ms |
3476 KB |
Output is correct |
34 |
Correct |
628 ms |
3660 KB |
Output is correct |
35 |
Correct |
565 ms |
4176 KB |
Output is correct |
36 |
Correct |
229 ms |
2624 KB |
Output is correct |
37 |
Correct |
295 ms |
4608 KB |
Output is correct |
38 |
Correct |
340 ms |
2592 KB |
Output is correct |
39 |
Correct |
520 ms |
3904 KB |
Output is correct |
40 |
Correct |
715 ms |
3620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 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 |
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 |
159 ms |
14120 KB |
Output is correct |
12 |
Correct |
175 ms |
14188 KB |
Output is correct |
13 |
Correct |
194 ms |
14060 KB |
Output is correct |
14 |
Correct |
240 ms |
14120 KB |
Output is correct |
15 |
Correct |
176 ms |
14188 KB |
Output is correct |
16 |
Correct |
223 ms |
14052 KB |
Output is correct |
17 |
Correct |
244 ms |
14040 KB |
Output is correct |
18 |
Correct |
159 ms |
14308 KB |
Output is correct |
19 |
Correct |
157 ms |
13928 KB |
Output is correct |
20 |
Correct |
159 ms |
14160 KB |
Output is correct |
21 |
Correct |
214 ms |
14072 KB |
Output is correct |
22 |
Correct |
225 ms |
14052 KB |
Output is correct |
23 |
Correct |
166 ms |
14040 KB |
Output is correct |
24 |
Correct |
239 ms |
14040 KB |
Output is correct |
25 |
Correct |
246 ms |
14044 KB |
Output is correct |
26 |
Correct |
149 ms |
13984 KB |
Output is correct |
27 |
Correct |
163 ms |
14040 KB |
Output is correct |
28 |
Correct |
167 ms |
13912 KB |
Output is correct |
29 |
Correct |
187 ms |
14092 KB |
Output is correct |
30 |
Correct |
228 ms |
14052 KB |
Output is correct |
31 |
Correct |
177 ms |
14048 KB |
Output is correct |
32 |
Correct |
234 ms |
14120 KB |
Output is correct |
33 |
Correct |
230 ms |
14060 KB |
Output is correct |
34 |
Correct |
148 ms |
13936 KB |
Output is correct |
35 |
Correct |
201 ms |
14108 KB |
Output is correct |
36 |
Correct |
209 ms |
14216 KB |
Output is correct |
37 |
Correct |
225 ms |
14036 KB |
Output is correct |
38 |
Correct |
201 ms |
14060 KB |
Output is correct |
39 |
Correct |
204 ms |
14120 KB |
Output is correct |
40 |
Correct |
202 ms |
14196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 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 |
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 |
293 ms |
4540 KB |
Output is correct |
12 |
Correct |
345 ms |
4232 KB |
Output is correct |
13 |
Correct |
389 ms |
3524 KB |
Output is correct |
14 |
Correct |
519 ms |
3636 KB |
Output is correct |
15 |
Correct |
344 ms |
4556 KB |
Output is correct |
16 |
Correct |
501 ms |
3704 KB |
Output is correct |
17 |
Correct |
441 ms |
3632 KB |
Output is correct |
18 |
Correct |
189 ms |
5660 KB |
Output is correct |
19 |
Correct |
262 ms |
2520 KB |
Output is correct |
20 |
Correct |
284 ms |
4296 KB |
Output is correct |
21 |
Correct |
307 ms |
4616 KB |
Output is correct |
22 |
Correct |
338 ms |
4768 KB |
Output is correct |
23 |
Correct |
517 ms |
3852 KB |
Output is correct |
24 |
Correct |
716 ms |
3792 KB |
Output is correct |
25 |
Correct |
399 ms |
5632 KB |
Output is correct |
26 |
Correct |
654 ms |
4080 KB |
Output is correct |
27 |
Correct |
604 ms |
4032 KB |
Output is correct |
28 |
Correct |
205 ms |
6700 KB |
Output is correct |
29 |
Correct |
317 ms |
2592 KB |
Output is correct |
30 |
Correct |
328 ms |
4732 KB |
Output is correct |
31 |
Correct |
525 ms |
4652 KB |
Output is correct |
32 |
Correct |
757 ms |
4676 KB |
Output is correct |
33 |
Correct |
393 ms |
3476 KB |
Output is correct |
34 |
Correct |
628 ms |
3660 KB |
Output is correct |
35 |
Correct |
565 ms |
4176 KB |
Output is correct |
36 |
Correct |
229 ms |
2624 KB |
Output is correct |
37 |
Correct |
295 ms |
4608 KB |
Output is correct |
38 |
Correct |
340 ms |
2592 KB |
Output is correct |
39 |
Correct |
520 ms |
3904 KB |
Output is correct |
40 |
Correct |
715 ms |
3620 KB |
Output is correct |
41 |
Correct |
159 ms |
14120 KB |
Output is correct |
42 |
Correct |
175 ms |
14188 KB |
Output is correct |
43 |
Correct |
194 ms |
14060 KB |
Output is correct |
44 |
Correct |
240 ms |
14120 KB |
Output is correct |
45 |
Correct |
176 ms |
14188 KB |
Output is correct |
46 |
Correct |
223 ms |
14052 KB |
Output is correct |
47 |
Correct |
244 ms |
14040 KB |
Output is correct |
48 |
Correct |
159 ms |
14308 KB |
Output is correct |
49 |
Correct |
157 ms |
13928 KB |
Output is correct |
50 |
Correct |
159 ms |
14160 KB |
Output is correct |
51 |
Correct |
214 ms |
14072 KB |
Output is correct |
52 |
Correct |
225 ms |
14052 KB |
Output is correct |
53 |
Correct |
166 ms |
14040 KB |
Output is correct |
54 |
Correct |
239 ms |
14040 KB |
Output is correct |
55 |
Correct |
246 ms |
14044 KB |
Output is correct |
56 |
Correct |
149 ms |
13984 KB |
Output is correct |
57 |
Correct |
163 ms |
14040 KB |
Output is correct |
58 |
Correct |
167 ms |
13912 KB |
Output is correct |
59 |
Correct |
187 ms |
14092 KB |
Output is correct |
60 |
Correct |
228 ms |
14052 KB |
Output is correct |
61 |
Correct |
177 ms |
14048 KB |
Output is correct |
62 |
Correct |
234 ms |
14120 KB |
Output is correct |
63 |
Correct |
230 ms |
14060 KB |
Output is correct |
64 |
Correct |
148 ms |
13936 KB |
Output is correct |
65 |
Correct |
201 ms |
14108 KB |
Output is correct |
66 |
Correct |
209 ms |
14216 KB |
Output is correct |
67 |
Correct |
225 ms |
14036 KB |
Output is correct |
68 |
Correct |
201 ms |
14060 KB |
Output is correct |
69 |
Correct |
204 ms |
14120 KB |
Output is correct |
70 |
Correct |
202 ms |
14196 KB |
Output is correct |
71 |
Correct |
634 ms |
18992 KB |
Output is correct |
72 |
Correct |
664 ms |
18960 KB |
Output is correct |
73 |
Correct |
1235 ms |
20376 KB |
Output is correct |
74 |
Correct |
1963 ms |
18572 KB |
Output is correct |
75 |
Correct |
801 ms |
41368 KB |
Output is correct |
76 |
Execution timed out |
2072 ms |
38944 KB |
Time limit exceeded |
77 |
Halted |
0 ms |
0 KB |
- |