#include "bits/stdc++.h"
using namespace std;
#define ar array
const int N = 20;
int dp[2][1 << N];
int a[1 << N], n, m;
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin>>n>>m;
for(int i=0;i<(1 << n);i++){
char c; cin>>c;
a[i] = c - '0';
dp[0][i] = dp[1][i] = a[i];
}
for(int i=n-1;~i;i--){
for(int mask=0;mask<(1 << n);mask++){
if(mask >> i & 1) dp[1][mask] += dp[1][mask ^ (1 << i)];
}
for(int mask=(1 << n)-1;~mask;mask--){
if(!(mask >> i & 1)) dp[0][mask] += dp[0][mask | (1 << i)];
}
}
ar<int, 3> c {};
int res, in, t;
vector<int> p;
while(m--){
string s; cin>>s;
reverse(s.begin(), s.end());
c[0] = c[1] = c[2] = 0;
for(int i=0;i<n;i++){
if(s[i] == '0') c[0]++;
if(s[i] == '1') c[1]++;
if(s[i] == '?') c[2]++;
}
res = 0; p.clear();
if(c[2] <= 6){
in = 0;
for(int i=0;i<n;i++){
if(s[i] == '0' || s[i] == '1') in |= ((s[i] - '0') << i);
else p.push_back(i);
}
for(int mask=0;mask < (1 << c[2]);mask++){
int tot = in;
for(int j=0;j<c[2];j++){
if(mask >> j & 1) tot |= (1 << p[j]);
}
res += a[tot];
}
}
else if(c[0] <= 6) {
t = 0, in = 0;
for(int i=0;i<n;i++){
if(s[i] == '1') in |= (1 << i);
if(s[i] == '0') p.push_back(i);
}
for(int mask=0;mask < (1 << c[0]);mask++){
int tot = in;
for(int j=0;j<c[0];j++){
if(mask >> j & 1) tot |= (1 << p[j]);
}
if(__builtin_popcount(mask)&1) res -= dp[t][tot];
else res += dp[t][tot];
}
} else if(c[1] <= 6) {
t = 1, in = (1 << n) - 1;
for(int i=0;i<n;i++){
if(s[i] == '0') in ^= (1 << i);
if(s[i] == '1') p.push_back(i);
}
for(int mask=0;mask<(1 << c[1]);mask++){
int tot = in;
for(int j=0;j<c[1];j++){
if(mask >> j & 1) tot ^= (1 << p[j]);
}
if(__builtin_popcount(mask)&1) res -= dp[t][tot];
else res += dp[t][tot];
}
} else assert(false);
cout<<res<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
461 ms |
4288 KB |
Output is correct |
12 |
Correct |
452 ms |
4184 KB |
Output is correct |
13 |
Correct |
306 ms |
3224 KB |
Output is correct |
14 |
Correct |
287 ms |
3288 KB |
Output is correct |
15 |
Correct |
743 ms |
4356 KB |
Output is correct |
16 |
Correct |
395 ms |
3524 KB |
Output is correct |
17 |
Correct |
396 ms |
3548 KB |
Output is correct |
18 |
Correct |
170 ms |
5328 KB |
Output is correct |
19 |
Correct |
208 ms |
2316 KB |
Output is correct |
20 |
Correct |
490 ms |
4044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
461 ms |
4288 KB |
Output is correct |
12 |
Correct |
452 ms |
4184 KB |
Output is correct |
13 |
Correct |
306 ms |
3224 KB |
Output is correct |
14 |
Correct |
287 ms |
3288 KB |
Output is correct |
15 |
Correct |
743 ms |
4356 KB |
Output is correct |
16 |
Correct |
395 ms |
3524 KB |
Output is correct |
17 |
Correct |
396 ms |
3548 KB |
Output is correct |
18 |
Correct |
170 ms |
5328 KB |
Output is correct |
19 |
Correct |
208 ms |
2316 KB |
Output is correct |
20 |
Correct |
490 ms |
4044 KB |
Output is correct |
21 |
Correct |
277 ms |
4428 KB |
Output is correct |
22 |
Correct |
728 ms |
4596 KB |
Output is correct |
23 |
Correct |
461 ms |
3772 KB |
Output is correct |
24 |
Correct |
395 ms |
3508 KB |
Output is correct |
25 |
Correct |
374 ms |
5556 KB |
Output is correct |
26 |
Correct |
542 ms |
3976 KB |
Output is correct |
27 |
Correct |
520 ms |
3948 KB |
Output is correct |
28 |
Correct |
180 ms |
6376 KB |
Output is correct |
29 |
Correct |
232 ms |
2532 KB |
Output is correct |
30 |
Correct |
491 ms |
4600 KB |
Output is correct |
31 |
Correct |
852 ms |
4476 KB |
Output is correct |
32 |
Correct |
537 ms |
4380 KB |
Output is correct |
33 |
Correct |
331 ms |
3448 KB |
Output is correct |
34 |
Correct |
400 ms |
3524 KB |
Output is correct |
35 |
Correct |
530 ms |
4184 KB |
Output is correct |
36 |
Correct |
209 ms |
2456 KB |
Output is correct |
37 |
Correct |
788 ms |
4416 KB |
Output is correct |
38 |
Correct |
274 ms |
2516 KB |
Output is correct |
39 |
Correct |
443 ms |
3612 KB |
Output is correct |
40 |
Correct |
396 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
92 ms |
14828 KB |
Output is correct |
12 |
Correct |
107 ms |
14908 KB |
Output is correct |
13 |
Correct |
94 ms |
14840 KB |
Output is correct |
14 |
Correct |
136 ms |
14792 KB |
Output is correct |
15 |
Correct |
99 ms |
14900 KB |
Output is correct |
16 |
Correct |
122 ms |
14836 KB |
Output is correct |
17 |
Correct |
115 ms |
14768 KB |
Output is correct |
18 |
Correct |
68 ms |
15004 KB |
Output is correct |
19 |
Correct |
80 ms |
14668 KB |
Output is correct |
20 |
Correct |
84 ms |
14856 KB |
Output is correct |
21 |
Correct |
99 ms |
14924 KB |
Output is correct |
22 |
Correct |
147 ms |
14796 KB |
Output is correct |
23 |
Correct |
84 ms |
14772 KB |
Output is correct |
24 |
Correct |
108 ms |
14796 KB |
Output is correct |
25 |
Correct |
123 ms |
14800 KB |
Output is correct |
26 |
Correct |
76 ms |
14644 KB |
Output is correct |
27 |
Correct |
81 ms |
14916 KB |
Output is correct |
28 |
Correct |
76 ms |
14780 KB |
Output is correct |
29 |
Correct |
96 ms |
14772 KB |
Output is correct |
30 |
Correct |
125 ms |
14812 KB |
Output is correct |
31 |
Correct |
110 ms |
14796 KB |
Output is correct |
32 |
Correct |
127 ms |
14772 KB |
Output is correct |
33 |
Correct |
115 ms |
14780 KB |
Output is correct |
34 |
Correct |
74 ms |
14776 KB |
Output is correct |
35 |
Correct |
112 ms |
14776 KB |
Output is correct |
36 |
Correct |
104 ms |
14904 KB |
Output is correct |
37 |
Correct |
105 ms |
14788 KB |
Output is correct |
38 |
Correct |
124 ms |
14800 KB |
Output is correct |
39 |
Correct |
114 ms |
14848 KB |
Output is correct |
40 |
Correct |
106 ms |
14876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
461 ms |
4288 KB |
Output is correct |
12 |
Correct |
452 ms |
4184 KB |
Output is correct |
13 |
Correct |
306 ms |
3224 KB |
Output is correct |
14 |
Correct |
287 ms |
3288 KB |
Output is correct |
15 |
Correct |
743 ms |
4356 KB |
Output is correct |
16 |
Correct |
395 ms |
3524 KB |
Output is correct |
17 |
Correct |
396 ms |
3548 KB |
Output is correct |
18 |
Correct |
170 ms |
5328 KB |
Output is correct |
19 |
Correct |
208 ms |
2316 KB |
Output is correct |
20 |
Correct |
490 ms |
4044 KB |
Output is correct |
21 |
Correct |
277 ms |
4428 KB |
Output is correct |
22 |
Correct |
728 ms |
4596 KB |
Output is correct |
23 |
Correct |
461 ms |
3772 KB |
Output is correct |
24 |
Correct |
395 ms |
3508 KB |
Output is correct |
25 |
Correct |
374 ms |
5556 KB |
Output is correct |
26 |
Correct |
542 ms |
3976 KB |
Output is correct |
27 |
Correct |
520 ms |
3948 KB |
Output is correct |
28 |
Correct |
180 ms |
6376 KB |
Output is correct |
29 |
Correct |
232 ms |
2532 KB |
Output is correct |
30 |
Correct |
491 ms |
4600 KB |
Output is correct |
31 |
Correct |
852 ms |
4476 KB |
Output is correct |
32 |
Correct |
537 ms |
4380 KB |
Output is correct |
33 |
Correct |
331 ms |
3448 KB |
Output is correct |
34 |
Correct |
400 ms |
3524 KB |
Output is correct |
35 |
Correct |
530 ms |
4184 KB |
Output is correct |
36 |
Correct |
209 ms |
2456 KB |
Output is correct |
37 |
Correct |
788 ms |
4416 KB |
Output is correct |
38 |
Correct |
274 ms |
2516 KB |
Output is correct |
39 |
Correct |
443 ms |
3612 KB |
Output is correct |
40 |
Correct |
396 ms |
3540 KB |
Output is correct |
41 |
Correct |
92 ms |
14828 KB |
Output is correct |
42 |
Correct |
107 ms |
14908 KB |
Output is correct |
43 |
Correct |
94 ms |
14840 KB |
Output is correct |
44 |
Correct |
136 ms |
14792 KB |
Output is correct |
45 |
Correct |
99 ms |
14900 KB |
Output is correct |
46 |
Correct |
122 ms |
14836 KB |
Output is correct |
47 |
Correct |
115 ms |
14768 KB |
Output is correct |
48 |
Correct |
68 ms |
15004 KB |
Output is correct |
49 |
Correct |
80 ms |
14668 KB |
Output is correct |
50 |
Correct |
84 ms |
14856 KB |
Output is correct |
51 |
Correct |
99 ms |
14924 KB |
Output is correct |
52 |
Correct |
147 ms |
14796 KB |
Output is correct |
53 |
Correct |
84 ms |
14772 KB |
Output is correct |
54 |
Correct |
108 ms |
14796 KB |
Output is correct |
55 |
Correct |
123 ms |
14800 KB |
Output is correct |
56 |
Correct |
76 ms |
14644 KB |
Output is correct |
57 |
Correct |
81 ms |
14916 KB |
Output is correct |
58 |
Correct |
76 ms |
14780 KB |
Output is correct |
59 |
Correct |
96 ms |
14772 KB |
Output is correct |
60 |
Correct |
125 ms |
14812 KB |
Output is correct |
61 |
Correct |
110 ms |
14796 KB |
Output is correct |
62 |
Correct |
127 ms |
14772 KB |
Output is correct |
63 |
Correct |
115 ms |
14780 KB |
Output is correct |
64 |
Correct |
74 ms |
14776 KB |
Output is correct |
65 |
Correct |
112 ms |
14776 KB |
Output is correct |
66 |
Correct |
104 ms |
14904 KB |
Output is correct |
67 |
Correct |
105 ms |
14788 KB |
Output is correct |
68 |
Correct |
124 ms |
14800 KB |
Output is correct |
69 |
Correct |
114 ms |
14848 KB |
Output is correct |
70 |
Correct |
106 ms |
14876 KB |
Output is correct |
71 |
Correct |
584 ms |
39172 KB |
Output is correct |
72 |
Correct |
654 ms |
39256 KB |
Output is correct |
73 |
Correct |
855 ms |
37844 KB |
Output is correct |
74 |
Correct |
1318 ms |
38296 KB |
Output is correct |
75 |
Correct |
653 ms |
40180 KB |
Output is correct |
76 |
Correct |
1342 ms |
38592 KB |
Output is correct |
77 |
Correct |
1243 ms |
38580 KB |
Output is correct |
78 |
Correct |
308 ms |
42232 KB |
Output is correct |
79 |
Correct |
424 ms |
36152 KB |
Output is correct |
80 |
Correct |
554 ms |
39308 KB |
Output is correct |
81 |
Correct |
985 ms |
39128 KB |
Output is correct |
82 |
Correct |
1302 ms |
38180 KB |
Output is correct |
83 |
Correct |
588 ms |
37300 KB |
Output is correct |
84 |
Correct |
1066 ms |
38348 KB |
Output is correct |
85 |
Correct |
1234 ms |
38324 KB |
Output is correct |
86 |
Correct |
335 ms |
36104 KB |
Output is correct |
87 |
Correct |
500 ms |
39256 KB |
Output is correct |
88 |
Correct |
434 ms |
36140 KB |
Output is correct |
89 |
Correct |
776 ms |
37880 KB |
Output is correct |
90 |
Correct |
1104 ms |
38636 KB |
Output is correct |
91 |
Correct |
619 ms |
37428 KB |
Output is correct |
92 |
Correct |
1473 ms |
38512 KB |
Output is correct |
93 |
Correct |
1228 ms |
38460 KB |
Output is correct |
94 |
Correct |
303 ms |
36280 KB |
Output is correct |
95 |
Correct |
1006 ms |
38416 KB |
Output is correct |
96 |
Correct |
1056 ms |
38384 KB |
Output is correct |
97 |
Correct |
1010 ms |
38348 KB |
Output is correct |
98 |
Correct |
1008 ms |
38492 KB |
Output is correct |
99 |
Correct |
1012 ms |
38396 KB |
Output is correct |
100 |
Correct |
1004 ms |
38348 KB |
Output is correct |