#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
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 T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
int l, q; cin >> l >> q;
string s; cin >> s;
int sum1[MASK(l)], sum2[MASK(l)];
memset(sum1, 0, sizeof sum1);
for(int i= 0; i<MASK(l); ++i){
sum1[i] = s[i] - '0';
sum2[i] = s[i] - '0';
}
for(int j = 0; j < l; ++j) for(int i = 0; i<MASK(l); ++i) if (GETBIT(i, j)){
sum1[i] += sum1[i - MASK(j)];
}
for(int j = 0; j < l; ++j) for(int i = 0; i<MASK(l); ++i) if (!GETBIT(i, j)){
sum2[i] += sum2[i + MASK(j)];
}
while(q--){
string _s; cin >> _s;
reverse(ALL(_s));
int mask1 = 0, mask2 = 0;
for(int i = 0; i<l; ++i) {
if (_s[i] == '1') mask1 += MASK(i);
else if (_s[i] == '?') mask2 += MASK(i);
}
if (pop_cnt(mask2) <= 6){
int ans = 0;
for(int mask = mask2; mask >= 0; mask = (mask - 1) & mask2){
ans += s[mask + mask1] - '0';
if (mask == 0) break;
}
cout << ans << "\n";
}
else if (pop_cnt(mask1) <= 6){
int ans = 0;
int cur_pop = pop_cnt(mask1);
for(int mask = mask1; mask >= 0; mask = (mask - 1) & mask1){
if ((pop_cnt(mask) & 1) == (cur_pop & 1)) ans += sum1[mask + mask2];
else ans -= sum1[mask + mask2];
if (mask == 0) break;
}
cout << ans << "\n";
}
else{
int ans = 0;
int mask3 = (MASK(l) - 1) ^ mask1 ^ mask2;
for(int mask = mask3; mask >= 0; mask = (mask - 1) & mask3){
if (pop_cnt(mask) & 1) ans -= sum2[mask1 + mask];
else ans += sum2[mask1 + mask];
if (mask == 0) break;
}
cout << ans << "\n";
}
}
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
169 ms |
4388 KB |
Output is correct |
12 |
Correct |
137 ms |
3924 KB |
Output is correct |
13 |
Correct |
138 ms |
3156 KB |
Output is correct |
14 |
Correct |
145 ms |
3376 KB |
Output is correct |
15 |
Correct |
177 ms |
4432 KB |
Output is correct |
16 |
Correct |
163 ms |
3484 KB |
Output is correct |
17 |
Correct |
163 ms |
3496 KB |
Output is correct |
18 |
Correct |
107 ms |
5456 KB |
Output is correct |
19 |
Correct |
131 ms |
2216 KB |
Output is correct |
20 |
Correct |
166 ms |
3924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
169 ms |
4388 KB |
Output is correct |
12 |
Correct |
137 ms |
3924 KB |
Output is correct |
13 |
Correct |
138 ms |
3156 KB |
Output is correct |
14 |
Correct |
145 ms |
3376 KB |
Output is correct |
15 |
Correct |
177 ms |
4432 KB |
Output is correct |
16 |
Correct |
163 ms |
3484 KB |
Output is correct |
17 |
Correct |
163 ms |
3496 KB |
Output is correct |
18 |
Correct |
107 ms |
5456 KB |
Output is correct |
19 |
Correct |
131 ms |
2216 KB |
Output is correct |
20 |
Correct |
166 ms |
3924 KB |
Output is correct |
21 |
Correct |
325 ms |
4520 KB |
Output is correct |
22 |
Correct |
137 ms |
4480 KB |
Output is correct |
23 |
Correct |
165 ms |
3668 KB |
Output is correct |
24 |
Correct |
152 ms |
3408 KB |
Output is correct |
25 |
Correct |
156 ms |
5488 KB |
Output is correct |
26 |
Correct |
197 ms |
3920 KB |
Output is correct |
27 |
Correct |
189 ms |
3920 KB |
Output is correct |
28 |
Correct |
122 ms |
6536 KB |
Output is correct |
29 |
Correct |
145 ms |
2388 KB |
Output is correct |
30 |
Correct |
243 ms |
4632 KB |
Output is correct |
31 |
Correct |
191 ms |
4436 KB |
Output is correct |
32 |
Correct |
161 ms |
4436 KB |
Output is correct |
33 |
Correct |
155 ms |
3412 KB |
Output is correct |
34 |
Correct |
160 ms |
3436 KB |
Output is correct |
35 |
Correct |
179 ms |
3776 KB |
Output is correct |
36 |
Correct |
97 ms |
2388 KB |
Output is correct |
37 |
Correct |
158 ms |
4340 KB |
Output is correct |
38 |
Correct |
167 ms |
2392 KB |
Output is correct |
39 |
Correct |
168 ms |
3500 KB |
Output is correct |
40 |
Correct |
154 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
40 ms |
9968 KB |
Output is correct |
12 |
Correct |
39 ms |
12016 KB |
Output is correct |
13 |
Correct |
43 ms |
11896 KB |
Output is correct |
14 |
Correct |
52 ms |
12016 KB |
Output is correct |
15 |
Correct |
44 ms |
12016 KB |
Output is correct |
16 |
Correct |
52 ms |
11848 KB |
Output is correct |
17 |
Correct |
49 ms |
11852 KB |
Output is correct |
18 |
Correct |
38 ms |
12272 KB |
Output is correct |
19 |
Correct |
41 ms |
11764 KB |
Output is correct |
20 |
Correct |
43 ms |
12024 KB |
Output is correct |
21 |
Correct |
45 ms |
12024 KB |
Output is correct |
22 |
Correct |
54 ms |
12040 KB |
Output is correct |
23 |
Correct |
42 ms |
11856 KB |
Output is correct |
24 |
Correct |
46 ms |
12016 KB |
Output is correct |
25 |
Correct |
47 ms |
12020 KB |
Output is correct |
26 |
Correct |
37 ms |
11848 KB |
Output is correct |
27 |
Correct |
37 ms |
12016 KB |
Output is correct |
28 |
Correct |
41 ms |
11764 KB |
Output is correct |
29 |
Correct |
44 ms |
12016 KB |
Output is correct |
30 |
Correct |
45 ms |
12008 KB |
Output is correct |
31 |
Correct |
43 ms |
12020 KB |
Output is correct |
32 |
Correct |
50 ms |
12272 KB |
Output is correct |
33 |
Correct |
47 ms |
11844 KB |
Output is correct |
34 |
Correct |
37 ms |
11760 KB |
Output is correct |
35 |
Correct |
45 ms |
12024 KB |
Output is correct |
36 |
Correct |
44 ms |
12024 KB |
Output is correct |
37 |
Correct |
48 ms |
11828 KB |
Output is correct |
38 |
Correct |
46 ms |
11852 KB |
Output is correct |
39 |
Correct |
44 ms |
12020 KB |
Output is correct |
40 |
Correct |
47 ms |
12020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
169 ms |
4388 KB |
Output is correct |
12 |
Correct |
137 ms |
3924 KB |
Output is correct |
13 |
Correct |
138 ms |
3156 KB |
Output is correct |
14 |
Correct |
145 ms |
3376 KB |
Output is correct |
15 |
Correct |
177 ms |
4432 KB |
Output is correct |
16 |
Correct |
163 ms |
3484 KB |
Output is correct |
17 |
Correct |
163 ms |
3496 KB |
Output is correct |
18 |
Correct |
107 ms |
5456 KB |
Output is correct |
19 |
Correct |
131 ms |
2216 KB |
Output is correct |
20 |
Correct |
166 ms |
3924 KB |
Output is correct |
21 |
Correct |
325 ms |
4520 KB |
Output is correct |
22 |
Correct |
137 ms |
4480 KB |
Output is correct |
23 |
Correct |
165 ms |
3668 KB |
Output is correct |
24 |
Correct |
152 ms |
3408 KB |
Output is correct |
25 |
Correct |
156 ms |
5488 KB |
Output is correct |
26 |
Correct |
197 ms |
3920 KB |
Output is correct |
27 |
Correct |
189 ms |
3920 KB |
Output is correct |
28 |
Correct |
122 ms |
6536 KB |
Output is correct |
29 |
Correct |
145 ms |
2388 KB |
Output is correct |
30 |
Correct |
243 ms |
4632 KB |
Output is correct |
31 |
Correct |
191 ms |
4436 KB |
Output is correct |
32 |
Correct |
161 ms |
4436 KB |
Output is correct |
33 |
Correct |
155 ms |
3412 KB |
Output is correct |
34 |
Correct |
160 ms |
3436 KB |
Output is correct |
35 |
Correct |
179 ms |
3776 KB |
Output is correct |
36 |
Correct |
97 ms |
2388 KB |
Output is correct |
37 |
Correct |
158 ms |
4340 KB |
Output is correct |
38 |
Correct |
167 ms |
2392 KB |
Output is correct |
39 |
Correct |
168 ms |
3500 KB |
Output is correct |
40 |
Correct |
154 ms |
3412 KB |
Output is correct |
41 |
Correct |
40 ms |
9968 KB |
Output is correct |
42 |
Correct |
39 ms |
12016 KB |
Output is correct |
43 |
Correct |
43 ms |
11896 KB |
Output is correct |
44 |
Correct |
52 ms |
12016 KB |
Output is correct |
45 |
Correct |
44 ms |
12016 KB |
Output is correct |
46 |
Correct |
52 ms |
11848 KB |
Output is correct |
47 |
Correct |
49 ms |
11852 KB |
Output is correct |
48 |
Correct |
38 ms |
12272 KB |
Output is correct |
49 |
Correct |
41 ms |
11764 KB |
Output is correct |
50 |
Correct |
43 ms |
12024 KB |
Output is correct |
51 |
Correct |
45 ms |
12024 KB |
Output is correct |
52 |
Correct |
54 ms |
12040 KB |
Output is correct |
53 |
Correct |
42 ms |
11856 KB |
Output is correct |
54 |
Correct |
46 ms |
12016 KB |
Output is correct |
55 |
Correct |
47 ms |
12020 KB |
Output is correct |
56 |
Correct |
37 ms |
11848 KB |
Output is correct |
57 |
Correct |
37 ms |
12016 KB |
Output is correct |
58 |
Correct |
41 ms |
11764 KB |
Output is correct |
59 |
Correct |
44 ms |
12016 KB |
Output is correct |
60 |
Correct |
45 ms |
12008 KB |
Output is correct |
61 |
Correct |
43 ms |
12020 KB |
Output is correct |
62 |
Correct |
50 ms |
12272 KB |
Output is correct |
63 |
Correct |
47 ms |
11844 KB |
Output is correct |
64 |
Correct |
37 ms |
11760 KB |
Output is correct |
65 |
Correct |
45 ms |
12024 KB |
Output is correct |
66 |
Correct |
44 ms |
12024 KB |
Output is correct |
67 |
Correct |
48 ms |
11828 KB |
Output is correct |
68 |
Correct |
46 ms |
11852 KB |
Output is correct |
69 |
Correct |
44 ms |
12020 KB |
Output is correct |
70 |
Correct |
47 ms |
12020 KB |
Output is correct |
71 |
Correct |
267 ms |
36280 KB |
Output is correct |
72 |
Correct |
201 ms |
36340 KB |
Output is correct |
73 |
Correct |
273 ms |
34884 KB |
Output is correct |
74 |
Correct |
506 ms |
35124 KB |
Output is correct |
75 |
Correct |
288 ms |
37292 KB |
Output is correct |
76 |
Correct |
448 ms |
35832 KB |
Output is correct |
77 |
Correct |
389 ms |
35312 KB |
Output is correct |
78 |
Correct |
196 ms |
39144 KB |
Output is correct |
79 |
Correct |
263 ms |
33220 KB |
Output is correct |
80 |
Correct |
290 ms |
36336 KB |
Output is correct |
81 |
Correct |
345 ms |
36336 KB |
Output is correct |
82 |
Correct |
522 ms |
35316 KB |
Output is correct |
83 |
Correct |
260 ms |
34296 KB |
Output is correct |
84 |
Correct |
334 ms |
35316 KB |
Output is correct |
85 |
Correct |
355 ms |
35312 KB |
Output is correct |
86 |
Correct |
175 ms |
33336 KB |
Output is correct |
87 |
Correct |
188 ms |
36168 KB |
Output is correct |
88 |
Correct |
268 ms |
33112 KB |
Output is correct |
89 |
Correct |
335 ms |
34804 KB |
Output is correct |
90 |
Correct |
369 ms |
35296 KB |
Output is correct |
91 |
Correct |
237 ms |
34352 KB |
Output is correct |
92 |
Correct |
419 ms |
35640 KB |
Output is correct |
93 |
Correct |
359 ms |
35564 KB |
Output is correct |
94 |
Correct |
167 ms |
33264 KB |
Output is correct |
95 |
Correct |
373 ms |
35300 KB |
Output is correct |
96 |
Correct |
351 ms |
35316 KB |
Output is correct |
97 |
Correct |
334 ms |
35324 KB |
Output is correct |
98 |
Correct |
351 ms |
35404 KB |
Output is correct |
99 |
Correct |
360 ms |
35316 KB |
Output is correct |
100 |
Correct |
371 ms |
35312 KB |
Output is correct |