#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = (1<<20)+5;
int n, q, arr[N], dp[N];
int getAns(string &s) {
int num = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '?') {num = -1; break;}
else num += (1<<(n-i-1))*(s[i]-'0');
}
if (num != -1) {
return arr[num];
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '?') {
s[i] = '1';
ans += getAns(s);
s[i] = '0';
ans += getAns(s);
s[i] = '?';
break;
}
}
return ans;
}
void sol() {
cin >> n >> q;
for (int i = 0; i < (1<<n); i++) {char x; cin >> x; arr[i] = x-'0';}
// for (int mask = 0; mask < (1<<n); mask++) {
// dp[mask] = arr[mask];
// for (int i = 0; i < n; i++) {
// if (mask&(1<<i)) {
// dp[mask] = dp[mask] + dp[mask^(1<<i)];
// }
// }
// }
for(int i = 0; i<(1<<n); ++i)
dp[i] = arr[i];
for(int i = 0;i < n; ++i) for(int mask = 0; mask < (1<<n); ++mask){
if(mask & (1<<i))
dp[mask] += dp[mask^(1<<i)];
}
while (q--) {
string s; cin >> s;
int one = 0, zero = 0, question = 0;
for (int i = 0; i < n; i++) {
one += s[i]=='1';
zero += s[i]=='0';
question += s[i]=='?';
}
if (one <= 10) {
vi temp;
int num = 0;
reverse(all(s));
for (int i = 0; i < n; i++) {
if (s[i] == '1') temp.pb(i);
if (s[i] == '1' || s[i] == '?') num += (1<<i);
}
int ans = 0;
for (int i = 0; i < (1<<temp.size()); i++) {
int tempNum = num, ct = 0;
for (int j = 0; j < temp.size(); j++) {
if (i&(1<<j)) {
tempNum ^= (1<<temp[j]);
ct++;
}
}
if (ct%2 != 0) ans -= dp[tempNum];
else ans += dp[tempNum];
}
cout << ans << endl;
}
else {
cout << getAns(s) << endl;
}
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'void sol()':
snake_escaping.cpp:103:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int j = 0; j < temp.size(); j++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
504 ms |
6296 KB |
Output is correct |
12 |
Correct |
206 ms |
6228 KB |
Output is correct |
13 |
Correct |
303 ms |
5204 KB |
Output is correct |
14 |
Correct |
307 ms |
5456 KB |
Output is correct |
15 |
Correct |
284 ms |
6324 KB |
Output is correct |
16 |
Correct |
418 ms |
5456 KB |
Output is correct |
17 |
Correct |
452 ms |
5528 KB |
Output is correct |
18 |
Correct |
152 ms |
7384 KB |
Output is correct |
19 |
Correct |
484 ms |
4936 KB |
Output is correct |
20 |
Correct |
1004 ms |
6232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
504 ms |
6296 KB |
Output is correct |
12 |
Correct |
206 ms |
6228 KB |
Output is correct |
13 |
Correct |
303 ms |
5204 KB |
Output is correct |
14 |
Correct |
307 ms |
5456 KB |
Output is correct |
15 |
Correct |
284 ms |
6324 KB |
Output is correct |
16 |
Correct |
418 ms |
5456 KB |
Output is correct |
17 |
Correct |
452 ms |
5528 KB |
Output is correct |
18 |
Correct |
152 ms |
7384 KB |
Output is correct |
19 |
Correct |
484 ms |
4936 KB |
Output is correct |
20 |
Correct |
1004 ms |
6232 KB |
Output is correct |
21 |
Correct |
867 ms |
6484 KB |
Output is correct |
22 |
Correct |
235 ms |
6488 KB |
Output is correct |
23 |
Correct |
442 ms |
5660 KB |
Output is correct |
24 |
Correct |
393 ms |
5480 KB |
Output is correct |
25 |
Correct |
320 ms |
7580 KB |
Output is correct |
26 |
Correct |
732 ms |
5904 KB |
Output is correct |
27 |
Correct |
906 ms |
6096 KB |
Output is correct |
28 |
Correct |
181 ms |
8532 KB |
Output is correct |
29 |
Correct |
1478 ms |
4504 KB |
Output is correct |
30 |
Execution timed out |
2061 ms |
5200 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
1861 ms |
8840 KB |
Output is correct |
12 |
Correct |
46 ms |
8788 KB |
Output is correct |
13 |
Correct |
89 ms |
8720 KB |
Output is correct |
14 |
Correct |
75 ms |
8724 KB |
Output is correct |
15 |
Correct |
54 ms |
8900 KB |
Output is correct |
16 |
Correct |
385 ms |
8924 KB |
Output is correct |
17 |
Correct |
328 ms |
8792 KB |
Output is correct |
18 |
Correct |
36 ms |
9040 KB |
Output is correct |
19 |
Correct |
1871 ms |
8904 KB |
Output is correct |
20 |
Correct |
882 ms |
8788 KB |
Output is correct |
21 |
Correct |
87 ms |
10804 KB |
Output is correct |
22 |
Correct |
110 ms |
10832 KB |
Output is correct |
23 |
Correct |
75 ms |
10692 KB |
Output is correct |
24 |
Correct |
384 ms |
10896 KB |
Output is correct |
25 |
Correct |
344 ms |
10880 KB |
Output is correct |
26 |
Correct |
34 ms |
10584 KB |
Output is correct |
27 |
Correct |
43 ms |
10832 KB |
Output is correct |
28 |
Correct |
609 ms |
10792 KB |
Output is correct |
29 |
Correct |
1908 ms |
10880 KB |
Output is correct |
30 |
Correct |
108 ms |
10688 KB |
Output is correct |
31 |
Correct |
51 ms |
10836 KB |
Output is correct |
32 |
Correct |
80 ms |
10692 KB |
Output is correct |
33 |
Correct |
336 ms |
11108 KB |
Output is correct |
34 |
Correct |
37 ms |
10636 KB |
Output is correct |
35 |
Correct |
455 ms |
10836 KB |
Output is correct |
36 |
Correct |
461 ms |
10940 KB |
Output is correct |
37 |
Correct |
496 ms |
10960 KB |
Output is correct |
38 |
Correct |
452 ms |
10828 KB |
Output is correct |
39 |
Correct |
462 ms |
11004 KB |
Output is correct |
40 |
Correct |
474 ms |
10684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
504 ms |
6296 KB |
Output is correct |
12 |
Correct |
206 ms |
6228 KB |
Output is correct |
13 |
Correct |
303 ms |
5204 KB |
Output is correct |
14 |
Correct |
307 ms |
5456 KB |
Output is correct |
15 |
Correct |
284 ms |
6324 KB |
Output is correct |
16 |
Correct |
418 ms |
5456 KB |
Output is correct |
17 |
Correct |
452 ms |
5528 KB |
Output is correct |
18 |
Correct |
152 ms |
7384 KB |
Output is correct |
19 |
Correct |
484 ms |
4936 KB |
Output is correct |
20 |
Correct |
1004 ms |
6232 KB |
Output is correct |
21 |
Correct |
867 ms |
6484 KB |
Output is correct |
22 |
Correct |
235 ms |
6488 KB |
Output is correct |
23 |
Correct |
442 ms |
5660 KB |
Output is correct |
24 |
Correct |
393 ms |
5480 KB |
Output is correct |
25 |
Correct |
320 ms |
7580 KB |
Output is correct |
26 |
Correct |
732 ms |
5904 KB |
Output is correct |
27 |
Correct |
906 ms |
6096 KB |
Output is correct |
28 |
Correct |
181 ms |
8532 KB |
Output is correct |
29 |
Correct |
1478 ms |
4504 KB |
Output is correct |
30 |
Execution timed out |
2061 ms |
5200 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |