#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 23;
int N, Q;
string cost;
int sum[2][MASK(20) + 10];
void solve(){
cin >> N >> Q >> cost;
for(int i = 0; i < MASK(N); i++){
int c = cost[i] - '0';
sum[0][i] = sum[1][i] = c;
}
for(int j = 0; j < N; j++){
for(int i = 0; i < MASK(N); i++){
if(BIT(i, j)){
sum[0][i] += sum[0][i - MASK(j)];
}
}
for(int i = MASK(N) - 1; i >= 0; i--){
if(!BIT(i, j)){
sum[1][i] += sum[1][i + MASK(j)];
}
}
}
while(Q--){
string s; cin >> s;
reverse(ALL(s));
int mask0 = 0, mask1 = 0, mask2 = 0;
for(int i = 0; i < N; i++){
if(s[i] == '0') mask0 += MASK(i);
if(s[i] == '1') mask1 += MASK(i);
if(s[i] == '?') mask2 += MASK(i);
}
if(__builtin_popcount(mask0) <= 6){
int ans = 0;
for(int mask = mask0; mask >= 0; mask = (mask - 1) & mask0){
if(__builtin_popcount(mask) & 1) ans -= sum[1][mask1 + mask];
else ans += sum[1][mask1 + mask];
if(mask == 0) break;
}
cout << ans << '\n';
}
else if(__builtin_popcount(mask1) <= 6){
int ans = 0;
for(int mask = mask1; mask >= 0; mask = (mask - 1) & mask1){
if(__builtin_popcount(mask) & 1) ans -= sum[0][MASK(N) - 1 - mask0 - mask];
else ans += sum[0][MASK(N) - 1 - mask0 - mask];
if(mask == 0) break;
}
cout << ans << '\n';
}
else{
int ans = 0;
for(int mask = mask2; mask >= 0; mask = (mask - 1) & mask2){
ans += cost[mask1 + mask] - '0';
if(mask == 0) break;
}
cout << ans << '\n';
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("btower.inp","r",stdin);
// freopen("btower.out","w",stdout);
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
149 ms |
15000 KB |
Output is correct |
12 |
Correct |
222 ms |
14672 KB |
Output is correct |
13 |
Correct |
271 ms |
13908 KB |
Output is correct |
14 |
Correct |
204 ms |
14160 KB |
Output is correct |
15 |
Correct |
178 ms |
14992 KB |
Output is correct |
16 |
Correct |
188 ms |
14188 KB |
Output is correct |
17 |
Correct |
214 ms |
14268 KB |
Output is correct |
18 |
Correct |
103 ms |
16436 KB |
Output is correct |
19 |
Correct |
211 ms |
13184 KB |
Output is correct |
20 |
Correct |
154 ms |
14980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
149 ms |
15000 KB |
Output is correct |
12 |
Correct |
222 ms |
14672 KB |
Output is correct |
13 |
Correct |
271 ms |
13908 KB |
Output is correct |
14 |
Correct |
204 ms |
14160 KB |
Output is correct |
15 |
Correct |
178 ms |
14992 KB |
Output is correct |
16 |
Correct |
188 ms |
14188 KB |
Output is correct |
17 |
Correct |
214 ms |
14268 KB |
Output is correct |
18 |
Correct |
103 ms |
16436 KB |
Output is correct |
19 |
Correct |
211 ms |
13184 KB |
Output is correct |
20 |
Correct |
154 ms |
14980 KB |
Output is correct |
21 |
Correct |
160 ms |
18052 KB |
Output is correct |
22 |
Correct |
234 ms |
18252 KB |
Output is correct |
23 |
Correct |
358 ms |
17236 KB |
Output is correct |
24 |
Correct |
266 ms |
17020 KB |
Output is correct |
25 |
Correct |
192 ms |
19028 KB |
Output is correct |
26 |
Correct |
265 ms |
17592 KB |
Output is correct |
27 |
Correct |
257 ms |
17748 KB |
Output is correct |
28 |
Correct |
118 ms |
20052 KB |
Output is correct |
29 |
Correct |
325 ms |
16212 KB |
Output is correct |
30 |
Correct |
200 ms |
18256 KB |
Output is correct |
31 |
Correct |
221 ms |
18028 KB |
Output is correct |
32 |
Correct |
223 ms |
18004 KB |
Output is correct |
33 |
Correct |
207 ms |
17252 KB |
Output is correct |
34 |
Correct |
247 ms |
17232 KB |
Output is correct |
35 |
Correct |
249 ms |
17572 KB |
Output is correct |
36 |
Correct |
113 ms |
16024 KB |
Output is correct |
37 |
Correct |
155 ms |
18056 KB |
Output is correct |
38 |
Correct |
267 ms |
15996 KB |
Output is correct |
39 |
Correct |
220 ms |
17236 KB |
Output is correct |
40 |
Correct |
219 ms |
17032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
43 ms |
12092 KB |
Output is correct |
12 |
Correct |
45 ms |
12016 KB |
Output is correct |
13 |
Correct |
50 ms |
11860 KB |
Output is correct |
14 |
Correct |
60 ms |
12016 KB |
Output is correct |
15 |
Correct |
45 ms |
12112 KB |
Output is correct |
16 |
Correct |
61 ms |
12016 KB |
Output is correct |
17 |
Correct |
54 ms |
11844 KB |
Output is correct |
18 |
Correct |
43 ms |
12116 KB |
Output is correct |
19 |
Correct |
45 ms |
11764 KB |
Output is correct |
20 |
Correct |
49 ms |
12008 KB |
Output is correct |
21 |
Correct |
50 ms |
12024 KB |
Output is correct |
22 |
Correct |
60 ms |
11856 KB |
Output is correct |
23 |
Correct |
47 ms |
11760 KB |
Output is correct |
24 |
Correct |
54 ms |
11852 KB |
Output is correct |
25 |
Correct |
53 ms |
11888 KB |
Output is correct |
26 |
Correct |
49 ms |
11840 KB |
Output is correct |
27 |
Correct |
43 ms |
12108 KB |
Output is correct |
28 |
Correct |
44 ms |
11772 KB |
Output is correct |
29 |
Correct |
49 ms |
12016 KB |
Output is correct |
30 |
Correct |
60 ms |
12016 KB |
Output is correct |
31 |
Correct |
46 ms |
12016 KB |
Output is correct |
32 |
Correct |
57 ms |
12028 KB |
Output is correct |
33 |
Correct |
74 ms |
12020 KB |
Output is correct |
34 |
Correct |
39 ms |
11868 KB |
Output is correct |
35 |
Correct |
49 ms |
11824 KB |
Output is correct |
36 |
Correct |
50 ms |
11856 KB |
Output is correct |
37 |
Correct |
50 ms |
12024 KB |
Output is correct |
38 |
Correct |
49 ms |
12020 KB |
Output is correct |
39 |
Correct |
50 ms |
11848 KB |
Output is correct |
40 |
Correct |
49 ms |
11856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
149 ms |
15000 KB |
Output is correct |
12 |
Correct |
222 ms |
14672 KB |
Output is correct |
13 |
Correct |
271 ms |
13908 KB |
Output is correct |
14 |
Correct |
204 ms |
14160 KB |
Output is correct |
15 |
Correct |
178 ms |
14992 KB |
Output is correct |
16 |
Correct |
188 ms |
14188 KB |
Output is correct |
17 |
Correct |
214 ms |
14268 KB |
Output is correct |
18 |
Correct |
103 ms |
16436 KB |
Output is correct |
19 |
Correct |
211 ms |
13184 KB |
Output is correct |
20 |
Correct |
154 ms |
14980 KB |
Output is correct |
21 |
Correct |
160 ms |
18052 KB |
Output is correct |
22 |
Correct |
234 ms |
18252 KB |
Output is correct |
23 |
Correct |
358 ms |
17236 KB |
Output is correct |
24 |
Correct |
266 ms |
17020 KB |
Output is correct |
25 |
Correct |
192 ms |
19028 KB |
Output is correct |
26 |
Correct |
265 ms |
17592 KB |
Output is correct |
27 |
Correct |
257 ms |
17748 KB |
Output is correct |
28 |
Correct |
118 ms |
20052 KB |
Output is correct |
29 |
Correct |
325 ms |
16212 KB |
Output is correct |
30 |
Correct |
200 ms |
18256 KB |
Output is correct |
31 |
Correct |
221 ms |
18028 KB |
Output is correct |
32 |
Correct |
223 ms |
18004 KB |
Output is correct |
33 |
Correct |
207 ms |
17252 KB |
Output is correct |
34 |
Correct |
247 ms |
17232 KB |
Output is correct |
35 |
Correct |
249 ms |
17572 KB |
Output is correct |
36 |
Correct |
113 ms |
16024 KB |
Output is correct |
37 |
Correct |
155 ms |
18056 KB |
Output is correct |
38 |
Correct |
267 ms |
15996 KB |
Output is correct |
39 |
Correct |
220 ms |
17236 KB |
Output is correct |
40 |
Correct |
219 ms |
17032 KB |
Output is correct |
41 |
Correct |
43 ms |
12092 KB |
Output is correct |
42 |
Correct |
45 ms |
12016 KB |
Output is correct |
43 |
Correct |
50 ms |
11860 KB |
Output is correct |
44 |
Correct |
60 ms |
12016 KB |
Output is correct |
45 |
Correct |
45 ms |
12112 KB |
Output is correct |
46 |
Correct |
61 ms |
12016 KB |
Output is correct |
47 |
Correct |
54 ms |
11844 KB |
Output is correct |
48 |
Correct |
43 ms |
12116 KB |
Output is correct |
49 |
Correct |
45 ms |
11764 KB |
Output is correct |
50 |
Correct |
49 ms |
12008 KB |
Output is correct |
51 |
Correct |
50 ms |
12024 KB |
Output is correct |
52 |
Correct |
60 ms |
11856 KB |
Output is correct |
53 |
Correct |
47 ms |
11760 KB |
Output is correct |
54 |
Correct |
54 ms |
11852 KB |
Output is correct |
55 |
Correct |
53 ms |
11888 KB |
Output is correct |
56 |
Correct |
49 ms |
11840 KB |
Output is correct |
57 |
Correct |
43 ms |
12108 KB |
Output is correct |
58 |
Correct |
44 ms |
11772 KB |
Output is correct |
59 |
Correct |
49 ms |
12016 KB |
Output is correct |
60 |
Correct |
60 ms |
12016 KB |
Output is correct |
61 |
Correct |
46 ms |
12016 KB |
Output is correct |
62 |
Correct |
57 ms |
12028 KB |
Output is correct |
63 |
Correct |
74 ms |
12020 KB |
Output is correct |
64 |
Correct |
39 ms |
11868 KB |
Output is correct |
65 |
Correct |
49 ms |
11824 KB |
Output is correct |
66 |
Correct |
50 ms |
11856 KB |
Output is correct |
67 |
Correct |
50 ms |
12024 KB |
Output is correct |
68 |
Correct |
49 ms |
12020 KB |
Output is correct |
69 |
Correct |
50 ms |
11848 KB |
Output is correct |
70 |
Correct |
49 ms |
11856 KB |
Output is correct |
71 |
Correct |
277 ms |
36324 KB |
Output is correct |
72 |
Correct |
311 ms |
36432 KB |
Output is correct |
73 |
Correct |
399 ms |
35056 KB |
Output is correct |
74 |
Correct |
550 ms |
35316 KB |
Output is correct |
75 |
Correct |
328 ms |
37360 KB |
Output is correct |
76 |
Correct |
594 ms |
35568 KB |
Output is correct |
77 |
Correct |
478 ms |
35368 KB |
Output is correct |
78 |
Correct |
216 ms |
39412 KB |
Output is correct |
79 |
Correct |
308 ms |
33356 KB |
Output is correct |
80 |
Correct |
282 ms |
36340 KB |
Output is correct |
81 |
Correct |
390 ms |
36336 KB |
Output is correct |
82 |
Correct |
601 ms |
35316 KB |
Output is correct |
83 |
Correct |
294 ms |
34804 KB |
Output is correct |
84 |
Correct |
483 ms |
35312 KB |
Output is correct |
85 |
Correct |
448 ms |
35572 KB |
Output is correct |
86 |
Correct |
171 ms |
33360 KB |
Output is correct |
87 |
Correct |
251 ms |
36336 KB |
Output is correct |
88 |
Correct |
297 ms |
33348 KB |
Output is correct |
89 |
Correct |
401 ms |
35056 KB |
Output is correct |
90 |
Correct |
415 ms |
35372 KB |
Output is correct |
91 |
Correct |
361 ms |
34500 KB |
Output is correct |
92 |
Correct |
537 ms |
35660 KB |
Output is correct |
93 |
Correct |
461 ms |
35568 KB |
Output is correct |
94 |
Correct |
177 ms |
33232 KB |
Output is correct |
95 |
Correct |
417 ms |
35316 KB |
Output is correct |
96 |
Correct |
399 ms |
35404 KB |
Output is correct |
97 |
Correct |
452 ms |
35316 KB |
Output is correct |
98 |
Correct |
435 ms |
35384 KB |
Output is correct |
99 |
Correct |
384 ms |
35404 KB |
Output is correct |
100 |
Correct |
419 ms |
35408 KB |
Output is correct |