#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1050005;
const ll MOD = 1000000007;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int SQ = 300;
string s;
int sc[N];
int s1[N], s0[N], bc[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q;
cin>>n>>q;
cin>>s;
for(int i = 0; i < (1 << n); ++i)
sc[i] = s[i] - '0';
for(int i = 1; i < (1 << n); ++i)
bc[i] = bc[i - (i & -i)] + 1;
for(int i = 0; i < (1 << n); ++i)
s1[i] = sc[i];
for(int i = 0; i < n; ++i)
for(int j = 0; j < (1 << n); ++j)
if((j & (1 << i)) == 0)
s1[j] += s1[j | (1 << i)];
for(int i = 0; i < (1 << n); ++i)
s0[i] = sc[((1 << n) - 1) ^ i];
for(int i = 0; i < n; ++i)
for(int j = 0; j < (1 << n); ++j)
if((j & (1 << i)) == 0)
s0[j] += s0[j | (1 << i)];
while(q--){
cin>>s;
int t1 = 0, t0 = 0, t2 = 0;
for(int i = 0; i < n; ++i){
if(s[i] == '1') t1 |= (1 << (n - 1 - i));
if(s[i] == '0') t0 |= (1 << (n - 1 - i));
if(s[i] == '?') t2 |= (1 << (n - 1 - i));
}
int res = 0;
if(bc[t2] <= 7){
for(int i = t2; ; i = (i - 1) & t2){
res += sc[t1 | i];
if(i == 0) break;
}
cout<<res<<'\n';
} else
if(bc[t0] <= 7){
for(int i = t0; ; i = (i - 1) & t0){
res += s1[t1 | i] * (bc[i] & 1 ? -1 : 1);
if(i == 0) break;
}
cout<<res<<'\n';
} else
if(bc[t1] <= 7){
for(int i = t1; ; i = (i - 1) & t1){
res += s0[t0 | i] * (bc[i] & 1 ? -1 : 1);
if(i == 0) break;
}
cout<<res<<'\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
257 ms |
15116 KB |
Output is correct |
12 |
Correct |
301 ms |
14880 KB |
Output is correct |
13 |
Correct |
259 ms |
14116 KB |
Output is correct |
14 |
Correct |
255 ms |
14148 KB |
Output is correct |
15 |
Correct |
324 ms |
15036 KB |
Output is correct |
16 |
Correct |
290 ms |
14272 KB |
Output is correct |
17 |
Correct |
307 ms |
14240 KB |
Output is correct |
18 |
Correct |
202 ms |
16064 KB |
Output is correct |
19 |
Correct |
229 ms |
13248 KB |
Output is correct |
20 |
Correct |
291 ms |
14708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
257 ms |
15116 KB |
Output is correct |
12 |
Correct |
301 ms |
14880 KB |
Output is correct |
13 |
Correct |
259 ms |
14116 KB |
Output is correct |
14 |
Correct |
255 ms |
14148 KB |
Output is correct |
15 |
Correct |
324 ms |
15036 KB |
Output is correct |
16 |
Correct |
290 ms |
14272 KB |
Output is correct |
17 |
Correct |
307 ms |
14240 KB |
Output is correct |
18 |
Correct |
202 ms |
16064 KB |
Output is correct |
19 |
Correct |
229 ms |
13248 KB |
Output is correct |
20 |
Correct |
291 ms |
14708 KB |
Output is correct |
21 |
Correct |
414 ms |
18140 KB |
Output is correct |
22 |
Correct |
341 ms |
18332 KB |
Output is correct |
23 |
Correct |
312 ms |
17272 KB |
Output is correct |
24 |
Correct |
308 ms |
17256 KB |
Output is correct |
25 |
Correct |
295 ms |
19136 KB |
Output is correct |
26 |
Correct |
343 ms |
17816 KB |
Output is correct |
27 |
Correct |
352 ms |
17672 KB |
Output is correct |
28 |
Correct |
209 ms |
20188 KB |
Output is correct |
29 |
Correct |
252 ms |
16276 KB |
Output is correct |
30 |
Correct |
330 ms |
18368 KB |
Output is correct |
31 |
Correct |
377 ms |
18156 KB |
Output is correct |
32 |
Correct |
332 ms |
18208 KB |
Output is correct |
33 |
Correct |
299 ms |
17092 KB |
Output is correct |
34 |
Correct |
315 ms |
17220 KB |
Output is correct |
35 |
Correct |
342 ms |
17548 KB |
Output is correct |
36 |
Correct |
202 ms |
16096 KB |
Output is correct |
37 |
Correct |
355 ms |
18100 KB |
Output is correct |
38 |
Correct |
270 ms |
16124 KB |
Output is correct |
39 |
Correct |
324 ms |
17380 KB |
Output is correct |
40 |
Correct |
313 ms |
17164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
76 ms |
20152 KB |
Output is correct |
12 |
Correct |
81 ms |
20192 KB |
Output is correct |
13 |
Correct |
79 ms |
20068 KB |
Output is correct |
14 |
Correct |
90 ms |
20068 KB |
Output is correct |
15 |
Correct |
85 ms |
20296 KB |
Output is correct |
16 |
Correct |
117 ms |
20168 KB |
Output is correct |
17 |
Correct |
120 ms |
20104 KB |
Output is correct |
18 |
Correct |
68 ms |
20300 KB |
Output is correct |
19 |
Correct |
82 ms |
20060 KB |
Output is correct |
20 |
Correct |
85 ms |
20204 KB |
Output is correct |
21 |
Correct |
105 ms |
20180 KB |
Output is correct |
22 |
Correct |
97 ms |
20140 KB |
Output is correct |
23 |
Correct |
83 ms |
20152 KB |
Output is correct |
24 |
Correct |
99 ms |
20136 KB |
Output is correct |
25 |
Correct |
101 ms |
20220 KB |
Output is correct |
26 |
Correct |
70 ms |
19928 KB |
Output is correct |
27 |
Correct |
74 ms |
20180 KB |
Output is correct |
28 |
Correct |
92 ms |
20016 KB |
Output is correct |
29 |
Correct |
88 ms |
20028 KB |
Output is correct |
30 |
Correct |
91 ms |
20076 KB |
Output is correct |
31 |
Correct |
84 ms |
20072 KB |
Output is correct |
32 |
Correct |
109 ms |
20144 KB |
Output is correct |
33 |
Correct |
103 ms |
20168 KB |
Output is correct |
34 |
Correct |
65 ms |
19936 KB |
Output is correct |
35 |
Correct |
93 ms |
20112 KB |
Output is correct |
36 |
Correct |
100 ms |
20132 KB |
Output is correct |
37 |
Correct |
88 ms |
20096 KB |
Output is correct |
38 |
Correct |
97 ms |
20068 KB |
Output is correct |
39 |
Correct |
98 ms |
20068 KB |
Output is correct |
40 |
Correct |
90 ms |
20064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
257 ms |
15116 KB |
Output is correct |
12 |
Correct |
301 ms |
14880 KB |
Output is correct |
13 |
Correct |
259 ms |
14116 KB |
Output is correct |
14 |
Correct |
255 ms |
14148 KB |
Output is correct |
15 |
Correct |
324 ms |
15036 KB |
Output is correct |
16 |
Correct |
290 ms |
14272 KB |
Output is correct |
17 |
Correct |
307 ms |
14240 KB |
Output is correct |
18 |
Correct |
202 ms |
16064 KB |
Output is correct |
19 |
Correct |
229 ms |
13248 KB |
Output is correct |
20 |
Correct |
291 ms |
14708 KB |
Output is correct |
21 |
Correct |
414 ms |
18140 KB |
Output is correct |
22 |
Correct |
341 ms |
18332 KB |
Output is correct |
23 |
Correct |
312 ms |
17272 KB |
Output is correct |
24 |
Correct |
308 ms |
17256 KB |
Output is correct |
25 |
Correct |
295 ms |
19136 KB |
Output is correct |
26 |
Correct |
343 ms |
17816 KB |
Output is correct |
27 |
Correct |
352 ms |
17672 KB |
Output is correct |
28 |
Correct |
209 ms |
20188 KB |
Output is correct |
29 |
Correct |
252 ms |
16276 KB |
Output is correct |
30 |
Correct |
330 ms |
18368 KB |
Output is correct |
31 |
Correct |
377 ms |
18156 KB |
Output is correct |
32 |
Correct |
332 ms |
18208 KB |
Output is correct |
33 |
Correct |
299 ms |
17092 KB |
Output is correct |
34 |
Correct |
315 ms |
17220 KB |
Output is correct |
35 |
Correct |
342 ms |
17548 KB |
Output is correct |
36 |
Correct |
202 ms |
16096 KB |
Output is correct |
37 |
Correct |
355 ms |
18100 KB |
Output is correct |
38 |
Correct |
270 ms |
16124 KB |
Output is correct |
39 |
Correct |
324 ms |
17380 KB |
Output is correct |
40 |
Correct |
313 ms |
17164 KB |
Output is correct |
41 |
Correct |
76 ms |
20152 KB |
Output is correct |
42 |
Correct |
81 ms |
20192 KB |
Output is correct |
43 |
Correct |
79 ms |
20068 KB |
Output is correct |
44 |
Correct |
90 ms |
20068 KB |
Output is correct |
45 |
Correct |
85 ms |
20296 KB |
Output is correct |
46 |
Correct |
117 ms |
20168 KB |
Output is correct |
47 |
Correct |
120 ms |
20104 KB |
Output is correct |
48 |
Correct |
68 ms |
20300 KB |
Output is correct |
49 |
Correct |
82 ms |
20060 KB |
Output is correct |
50 |
Correct |
85 ms |
20204 KB |
Output is correct |
51 |
Correct |
105 ms |
20180 KB |
Output is correct |
52 |
Correct |
97 ms |
20140 KB |
Output is correct |
53 |
Correct |
83 ms |
20152 KB |
Output is correct |
54 |
Correct |
99 ms |
20136 KB |
Output is correct |
55 |
Correct |
101 ms |
20220 KB |
Output is correct |
56 |
Correct |
70 ms |
19928 KB |
Output is correct |
57 |
Correct |
74 ms |
20180 KB |
Output is correct |
58 |
Correct |
92 ms |
20016 KB |
Output is correct |
59 |
Correct |
88 ms |
20028 KB |
Output is correct |
60 |
Correct |
91 ms |
20076 KB |
Output is correct |
61 |
Correct |
84 ms |
20072 KB |
Output is correct |
62 |
Correct |
109 ms |
20144 KB |
Output is correct |
63 |
Correct |
103 ms |
20168 KB |
Output is correct |
64 |
Correct |
65 ms |
19936 KB |
Output is correct |
65 |
Correct |
93 ms |
20112 KB |
Output is correct |
66 |
Correct |
100 ms |
20132 KB |
Output is correct |
67 |
Correct |
88 ms |
20096 KB |
Output is correct |
68 |
Correct |
97 ms |
20068 KB |
Output is correct |
69 |
Correct |
98 ms |
20068 KB |
Output is correct |
70 |
Correct |
90 ms |
20064 KB |
Output is correct |
71 |
Correct |
501 ms |
44408 KB |
Output is correct |
72 |
Correct |
616 ms |
44760 KB |
Output is correct |
73 |
Correct |
590 ms |
43100 KB |
Output is correct |
74 |
Correct |
811 ms |
43376 KB |
Output is correct |
75 |
Correct |
534 ms |
45404 KB |
Output is correct |
76 |
Correct |
1293 ms |
43748 KB |
Output is correct |
77 |
Correct |
942 ms |
43600 KB |
Output is correct |
78 |
Correct |
289 ms |
47392 KB |
Output is correct |
79 |
Correct |
408 ms |
41440 KB |
Output is correct |
80 |
Correct |
524 ms |
44640 KB |
Output is correct |
81 |
Correct |
804 ms |
44448 KB |
Output is correct |
82 |
Correct |
855 ms |
43756 KB |
Output is correct |
83 |
Correct |
464 ms |
42792 KB |
Output is correct |
84 |
Correct |
724 ms |
43452 KB |
Output is correct |
85 |
Correct |
1031 ms |
43696 KB |
Output is correct |
86 |
Correct |
285 ms |
41424 KB |
Output is correct |
87 |
Correct |
474 ms |
44480 KB |
Output is correct |
88 |
Correct |
409 ms |
41400 KB |
Output is correct |
89 |
Correct |
633 ms |
43092 KB |
Output is correct |
90 |
Correct |
701 ms |
43440 KB |
Output is correct |
91 |
Correct |
430 ms |
42708 KB |
Output is correct |
92 |
Correct |
964 ms |
43828 KB |
Output is correct |
93 |
Correct |
1000 ms |
43724 KB |
Output is correct |
94 |
Correct |
284 ms |
41388 KB |
Output is correct |
95 |
Correct |
754 ms |
43608 KB |
Output is correct |
96 |
Correct |
711 ms |
43620 KB |
Output is correct |
97 |
Correct |
783 ms |
43656 KB |
Output is correct |
98 |
Correct |
720 ms |
43576 KB |
Output is correct |
99 |
Correct |
851 ms |
43500 KB |
Output is correct |
100 |
Correct |
840 ms |
43516 KB |
Output is correct |