#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];
unordered_map<string, int> mp;
int sol(string s) {
if (mp.find(s) != mp.end()) {
return mp[s];
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '?') {
s[i] = '0';
ans = (ans + sol(s));
s[i] = '1';
ans = (ans + sol(s));
s[i] = '?';
break;
}
}
return mp[s] = ans;
}
void sol() {
cin >> n >> q;
for (int i = 0; i < (1<<n); i++) {char x; cin >> x; arr[i] = x-'0';}
for (int i = 0; i < (1<<n); i++) {
string s(n,'0');
for (int j = 0; j < n; j++) {
if (i&(1<<j)) s[j] = '1';
}
reverse(all(s));
mp[s] = arr[i];
}
string s(n, '?');
sol(s);
while (q--) {
string s; cin >> s;
cout << sol(s) << endl;
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
920 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
4 ms |
1372 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
920 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
4 ms |
1372 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
174 ms |
4692 KB |
Output is correct |
12 |
Correct |
182 ms |
4440 KB |
Output is correct |
13 |
Correct |
211 ms |
4380 KB |
Output is correct |
14 |
Correct |
194 ms |
4232 KB |
Output is correct |
15 |
Correct |
215 ms |
6276 KB |
Output is correct |
16 |
Correct |
214 ms |
5592 KB |
Output is correct |
17 |
Correct |
237 ms |
7868 KB |
Output is correct |
18 |
Correct |
162 ms |
5592 KB |
Output is correct |
19 |
Correct |
181 ms |
2584 KB |
Output is correct |
20 |
Correct |
214 ms |
4484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
920 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
4 ms |
1372 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
174 ms |
4692 KB |
Output is correct |
12 |
Correct |
182 ms |
4440 KB |
Output is correct |
13 |
Correct |
211 ms |
4380 KB |
Output is correct |
14 |
Correct |
194 ms |
4232 KB |
Output is correct |
15 |
Correct |
215 ms |
6276 KB |
Output is correct |
16 |
Correct |
214 ms |
5592 KB |
Output is correct |
17 |
Correct |
237 ms |
7868 KB |
Output is correct |
18 |
Correct |
162 ms |
5592 KB |
Output is correct |
19 |
Correct |
181 ms |
2584 KB |
Output is correct |
20 |
Correct |
214 ms |
4484 KB |
Output is correct |
21 |
Correct |
212 ms |
7328 KB |
Output is correct |
22 |
Correct |
228 ms |
9052 KB |
Output is correct |
23 |
Correct |
597 ms |
27604 KB |
Output is correct |
24 |
Correct |
493 ms |
23360 KB |
Output is correct |
25 |
Correct |
577 ms |
36796 KB |
Output is correct |
26 |
Correct |
983 ms |
55332 KB |
Output is correct |
27 |
Runtime error |
885 ms |
65536 KB |
Execution killed with signal 9 |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
920 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
4 ms |
1372 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Runtime error |
275 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
920 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
4 ms |
1372 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
174 ms |
4692 KB |
Output is correct |
12 |
Correct |
182 ms |
4440 KB |
Output is correct |
13 |
Correct |
211 ms |
4380 KB |
Output is correct |
14 |
Correct |
194 ms |
4232 KB |
Output is correct |
15 |
Correct |
215 ms |
6276 KB |
Output is correct |
16 |
Correct |
214 ms |
5592 KB |
Output is correct |
17 |
Correct |
237 ms |
7868 KB |
Output is correct |
18 |
Correct |
162 ms |
5592 KB |
Output is correct |
19 |
Correct |
181 ms |
2584 KB |
Output is correct |
20 |
Correct |
214 ms |
4484 KB |
Output is correct |
21 |
Correct |
212 ms |
7328 KB |
Output is correct |
22 |
Correct |
228 ms |
9052 KB |
Output is correct |
23 |
Correct |
597 ms |
27604 KB |
Output is correct |
24 |
Correct |
493 ms |
23360 KB |
Output is correct |
25 |
Correct |
577 ms |
36796 KB |
Output is correct |
26 |
Correct |
983 ms |
55332 KB |
Output is correct |
27 |
Runtime error |
885 ms |
65536 KB |
Execution killed with signal 9 |
28 |
Halted |
0 ms |
0 KB |
- |