#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()){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 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 |
261 ms |
5008 KB |
Output is correct |
12 |
Correct |
280 ms |
4764 KB |
Output is correct |
13 |
Correct |
393 ms |
4004 KB |
Output is correct |
14 |
Correct |
517 ms |
4052 KB |
Output is correct |
15 |
Correct |
336 ms |
5036 KB |
Output is correct |
16 |
Correct |
471 ms |
4264 KB |
Output is correct |
17 |
Correct |
430 ms |
4296 KB |
Output is correct |
18 |
Correct |
190 ms |
6280 KB |
Output is correct |
19 |
Correct |
253 ms |
2996 KB |
Output is correct |
20 |
Correct |
289 ms |
4684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 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 |
261 ms |
5008 KB |
Output is correct |
12 |
Correct |
280 ms |
4764 KB |
Output is correct |
13 |
Correct |
393 ms |
4004 KB |
Output is correct |
14 |
Correct |
517 ms |
4052 KB |
Output is correct |
15 |
Correct |
336 ms |
5036 KB |
Output is correct |
16 |
Correct |
471 ms |
4264 KB |
Output is correct |
17 |
Correct |
430 ms |
4296 KB |
Output is correct |
18 |
Correct |
190 ms |
6280 KB |
Output is correct |
19 |
Correct |
253 ms |
2996 KB |
Output is correct |
20 |
Correct |
289 ms |
4684 KB |
Output is correct |
21 |
Correct |
303 ms |
4972 KB |
Output is correct |
22 |
Correct |
328 ms |
5196 KB |
Output is correct |
23 |
Correct |
530 ms |
4336 KB |
Output is correct |
24 |
Correct |
710 ms |
4108 KB |
Output is correct |
25 |
Correct |
411 ms |
6172 KB |
Output is correct |
26 |
Correct |
609 ms |
4472 KB |
Output is correct |
27 |
Correct |
552 ms |
4620 KB |
Output is correct |
28 |
Correct |
199 ms |
7092 KB |
Output is correct |
29 |
Correct |
302 ms |
3080 KB |
Output is correct |
30 |
Correct |
324 ms |
5128 KB |
Output is correct |
31 |
Correct |
500 ms |
5144 KB |
Output is correct |
32 |
Correct |
696 ms |
5000 KB |
Output is correct |
33 |
Correct |
397 ms |
3868 KB |
Output is correct |
34 |
Correct |
628 ms |
4040 KB |
Output is correct |
35 |
Correct |
549 ms |
4516 KB |
Output is correct |
36 |
Correct |
211 ms |
2996 KB |
Output is correct |
37 |
Correct |
302 ms |
5060 KB |
Output is correct |
38 |
Correct |
307 ms |
2956 KB |
Output is correct |
39 |
Correct |
493 ms |
4252 KB |
Output is correct |
40 |
Correct |
687 ms |
4400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 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 |
161 ms |
14320 KB |
Output is correct |
12 |
Correct |
168 ms |
14444 KB |
Output is correct |
13 |
Correct |
192 ms |
14272 KB |
Output is correct |
14 |
Correct |
223 ms |
14300 KB |
Output is correct |
15 |
Correct |
188 ms |
14472 KB |
Output is correct |
16 |
Correct |
242 ms |
14336 KB |
Output is correct |
17 |
Correct |
234 ms |
14320 KB |
Output is correct |
18 |
Correct |
167 ms |
14468 KB |
Output is correct |
19 |
Correct |
162 ms |
14176 KB |
Output is correct |
20 |
Correct |
162 ms |
14312 KB |
Output is correct |
21 |
Correct |
196 ms |
14444 KB |
Output is correct |
22 |
Correct |
233 ms |
14380 KB |
Output is correct |
23 |
Correct |
188 ms |
14440 KB |
Output is correct |
24 |
Correct |
249 ms |
14400 KB |
Output is correct |
25 |
Correct |
228 ms |
14488 KB |
Output is correct |
26 |
Correct |
163 ms |
14372 KB |
Output is correct |
27 |
Correct |
184 ms |
14540 KB |
Output is correct |
28 |
Correct |
176 ms |
14384 KB |
Output is correct |
29 |
Correct |
220 ms |
14540 KB |
Output is correct |
30 |
Correct |
233 ms |
14436 KB |
Output is correct |
31 |
Correct |
182 ms |
14380 KB |
Output is correct |
32 |
Correct |
258 ms |
14600 KB |
Output is correct |
33 |
Correct |
236 ms |
14428 KB |
Output is correct |
34 |
Correct |
155 ms |
14360 KB |
Output is correct |
35 |
Correct |
207 ms |
14440 KB |
Output is correct |
36 |
Correct |
207 ms |
14456 KB |
Output is correct |
37 |
Correct |
215 ms |
14468 KB |
Output is correct |
38 |
Correct |
207 ms |
14444 KB |
Output is correct |
39 |
Correct |
207 ms |
14496 KB |
Output is correct |
40 |
Correct |
212 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 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 |
261 ms |
5008 KB |
Output is correct |
12 |
Correct |
280 ms |
4764 KB |
Output is correct |
13 |
Correct |
393 ms |
4004 KB |
Output is correct |
14 |
Correct |
517 ms |
4052 KB |
Output is correct |
15 |
Correct |
336 ms |
5036 KB |
Output is correct |
16 |
Correct |
471 ms |
4264 KB |
Output is correct |
17 |
Correct |
430 ms |
4296 KB |
Output is correct |
18 |
Correct |
190 ms |
6280 KB |
Output is correct |
19 |
Correct |
253 ms |
2996 KB |
Output is correct |
20 |
Correct |
289 ms |
4684 KB |
Output is correct |
21 |
Correct |
303 ms |
4972 KB |
Output is correct |
22 |
Correct |
328 ms |
5196 KB |
Output is correct |
23 |
Correct |
530 ms |
4336 KB |
Output is correct |
24 |
Correct |
710 ms |
4108 KB |
Output is correct |
25 |
Correct |
411 ms |
6172 KB |
Output is correct |
26 |
Correct |
609 ms |
4472 KB |
Output is correct |
27 |
Correct |
552 ms |
4620 KB |
Output is correct |
28 |
Correct |
199 ms |
7092 KB |
Output is correct |
29 |
Correct |
302 ms |
3080 KB |
Output is correct |
30 |
Correct |
324 ms |
5128 KB |
Output is correct |
31 |
Correct |
500 ms |
5144 KB |
Output is correct |
32 |
Correct |
696 ms |
5000 KB |
Output is correct |
33 |
Correct |
397 ms |
3868 KB |
Output is correct |
34 |
Correct |
628 ms |
4040 KB |
Output is correct |
35 |
Correct |
549 ms |
4516 KB |
Output is correct |
36 |
Correct |
211 ms |
2996 KB |
Output is correct |
37 |
Correct |
302 ms |
5060 KB |
Output is correct |
38 |
Correct |
307 ms |
2956 KB |
Output is correct |
39 |
Correct |
493 ms |
4252 KB |
Output is correct |
40 |
Correct |
687 ms |
4400 KB |
Output is correct |
41 |
Correct |
161 ms |
14320 KB |
Output is correct |
42 |
Correct |
168 ms |
14444 KB |
Output is correct |
43 |
Correct |
192 ms |
14272 KB |
Output is correct |
44 |
Correct |
223 ms |
14300 KB |
Output is correct |
45 |
Correct |
188 ms |
14472 KB |
Output is correct |
46 |
Correct |
242 ms |
14336 KB |
Output is correct |
47 |
Correct |
234 ms |
14320 KB |
Output is correct |
48 |
Correct |
167 ms |
14468 KB |
Output is correct |
49 |
Correct |
162 ms |
14176 KB |
Output is correct |
50 |
Correct |
162 ms |
14312 KB |
Output is correct |
51 |
Correct |
196 ms |
14444 KB |
Output is correct |
52 |
Correct |
233 ms |
14380 KB |
Output is correct |
53 |
Correct |
188 ms |
14440 KB |
Output is correct |
54 |
Correct |
249 ms |
14400 KB |
Output is correct |
55 |
Correct |
228 ms |
14488 KB |
Output is correct |
56 |
Correct |
163 ms |
14372 KB |
Output is correct |
57 |
Correct |
184 ms |
14540 KB |
Output is correct |
58 |
Correct |
176 ms |
14384 KB |
Output is correct |
59 |
Correct |
220 ms |
14540 KB |
Output is correct |
60 |
Correct |
233 ms |
14436 KB |
Output is correct |
61 |
Correct |
182 ms |
14380 KB |
Output is correct |
62 |
Correct |
258 ms |
14600 KB |
Output is correct |
63 |
Correct |
236 ms |
14428 KB |
Output is correct |
64 |
Correct |
155 ms |
14360 KB |
Output is correct |
65 |
Correct |
207 ms |
14440 KB |
Output is correct |
66 |
Correct |
207 ms |
14456 KB |
Output is correct |
67 |
Correct |
215 ms |
14468 KB |
Output is correct |
68 |
Correct |
207 ms |
14444 KB |
Output is correct |
69 |
Correct |
207 ms |
14496 KB |
Output is correct |
70 |
Correct |
212 ms |
14444 KB |
Output is correct |
71 |
Correct |
618 ms |
19016 KB |
Output is correct |
72 |
Correct |
624 ms |
19236 KB |
Output is correct |
73 |
Correct |
1183 ms |
17968 KB |
Output is correct |
74 |
Execution timed out |
2019 ms |
17808 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |