#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define ii pair<int, int>
#define iii pair<ii, int>
#define ll long long
#define pb push_back
#define pli pair<ll, int>
#define all(v) (v).begin(), (v).end()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define cntbit(mask) __builtin_popcount(mask)
#define getbit(x, i) ((x >> i) & 1)
#define MASK(i) (1 << i)
#define pli pair<ll, int>
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return 1;} return 0;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return 1;} return 0;}
const int N = 1e5 + 5;
const int V = 1e6 + 5;
const int MOD = 1e9 + 7;
int n, q, cost[MASK(20) + 5], p[MASK(20) + 5];
string s;
void solve() {
cin >> n >> q;
cin >> s;
for(int i = 0 ; i < (1 << n) ; i++) p[i] = cost[i] = (s[i] - '0');
for(int i = 0 ; i < n ; i++)
for(int mask = 0 ; mask < (1 << n) ; mask++)
if (getbit(mask, i)) cost[mask] += cost[mask ^ MASK(i)];
for(int i = 0 ; i < q ; i++) {
string t; cin >> t;
reverse(all(t));
int mask1, mask2;
mask1 = mask2 = 0;
for(int i = 0 ; i < n ; i++)
if (t[i] == '1') mask1 |= MASK(i);
else if (t[i] == '?') mask2 |= MASK(i);
int ans = 0;
if (cntbit(mask2) <= 10) {
for(int submask = mask2 ; submask >= 0 ; submask = (submask - 1) & mask2) {
ans += p[mask1 + submask];
if (submask == 0) break;
}
} else {
int c = cntbit(mask1);
for(int submask = mask1 ; submask >= 0 ; submask = (submask - 1) & mask1) {
if (cntbit(submask) == c) ans += cost[submask + mask2];
else ans -= cost[submask + mask2];
if (submask == 0) break;
}
}
cout << ans << '\n';
}
}
int main(void) {
//freopen("task.inp", "r", stdin);
//freopen("task.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1; while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
476 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
476 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
172 ms |
15444 KB |
Output is correct |
12 |
Correct |
164 ms |
14664 KB |
Output is correct |
13 |
Correct |
132 ms |
13904 KB |
Output is correct |
14 |
Correct |
143 ms |
14160 KB |
Output is correct |
15 |
Correct |
189 ms |
15188 KB |
Output is correct |
16 |
Correct |
157 ms |
14160 KB |
Output is correct |
17 |
Correct |
171 ms |
14164 KB |
Output is correct |
18 |
Correct |
1111 ms |
16204 KB |
Output is correct |
19 |
Correct |
140 ms |
12980 KB |
Output is correct |
20 |
Correct |
210 ms |
14672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
476 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
172 ms |
15444 KB |
Output is correct |
12 |
Correct |
164 ms |
14664 KB |
Output is correct |
13 |
Correct |
132 ms |
13904 KB |
Output is correct |
14 |
Correct |
143 ms |
14160 KB |
Output is correct |
15 |
Correct |
189 ms |
15188 KB |
Output is correct |
16 |
Correct |
157 ms |
14160 KB |
Output is correct |
17 |
Correct |
171 ms |
14164 KB |
Output is correct |
18 |
Correct |
1111 ms |
16204 KB |
Output is correct |
19 |
Correct |
140 ms |
12980 KB |
Output is correct |
20 |
Correct |
210 ms |
14672 KB |
Output is correct |
21 |
Correct |
276 ms |
18000 KB |
Output is correct |
22 |
Correct |
280 ms |
18256 KB |
Output is correct |
23 |
Correct |
159 ms |
17232 KB |
Output is correct |
24 |
Correct |
180 ms |
17028 KB |
Output is correct |
25 |
Correct |
391 ms |
19316 KB |
Output is correct |
26 |
Correct |
202 ms |
17492 KB |
Output is correct |
27 |
Incorrect |
216 ms |
17640 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
476 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
99 ms |
12016 KB |
Output is correct |
12 |
Correct |
53 ms |
12024 KB |
Output is correct |
13 |
Correct |
37 ms |
12024 KB |
Output is correct |
14 |
Correct |
48 ms |
12016 KB |
Output is correct |
15 |
Incorrect |
37 ms |
12272 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
476 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
172 ms |
15444 KB |
Output is correct |
12 |
Correct |
164 ms |
14664 KB |
Output is correct |
13 |
Correct |
132 ms |
13904 KB |
Output is correct |
14 |
Correct |
143 ms |
14160 KB |
Output is correct |
15 |
Correct |
189 ms |
15188 KB |
Output is correct |
16 |
Correct |
157 ms |
14160 KB |
Output is correct |
17 |
Correct |
171 ms |
14164 KB |
Output is correct |
18 |
Correct |
1111 ms |
16204 KB |
Output is correct |
19 |
Correct |
140 ms |
12980 KB |
Output is correct |
20 |
Correct |
210 ms |
14672 KB |
Output is correct |
21 |
Correct |
276 ms |
18000 KB |
Output is correct |
22 |
Correct |
280 ms |
18256 KB |
Output is correct |
23 |
Correct |
159 ms |
17232 KB |
Output is correct |
24 |
Correct |
180 ms |
17028 KB |
Output is correct |
25 |
Correct |
391 ms |
19316 KB |
Output is correct |
26 |
Correct |
202 ms |
17492 KB |
Output is correct |
27 |
Incorrect |
216 ms |
17640 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |