// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int INF = 1e9+500;
const int N = 20;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,q;
array<int, (1 << N)> a, dp, dpr;
int RVSR;
inline void solve() {
cin>>n>>q;
RVSR = (1 << n) - 1;
for(int i = 0; i < (1 << n); i++) {
char tmp;
cin >> tmp;
a[i] = dp[i] = dpr[i ^ RVSR] = tmp - '0';
}
for(int k = 0; k < n; k++) {
for(int i = 0; i < (1 << n); i++) {
if(i & (1 << k)) {
dp[i] += dp[i ^ (1 << k)];
dpr[i] += dpr[i ^ (1 << k)];
}
}
}
// for(int i = 0; i < (1 << n); i++) {
// cout << "i:" << i <<" dp:" << dp[i] << " dpr:" << dpr[i] << "\n";
// }
REP(z, q) {
string s;
cin >> s;
reverse(all(s));
int cnt0 = 0, cnt1 = 0;
for(char c : s) {
if(c == '0') cnt0++;
else if(c == '1') cnt1++;
}
int ans = 0;
if(cnt1 + cnt0 >= 14) {
int x = 0;
int y = 0;
for(int i = 0; i < n; i++) {
if(s[i] == '?') y += (1 << i);
else if(s[i] == '1') x += (1 << i);
}
for(int i = y; ; i = (i - 1) & y) {
ans += a[x + i];
if(i == 0) break;
}
}
else if(cnt1 > cnt0) { // use reverse
int x = 0;
int y = 0;
for(int i = 0; i < n; i++) {
if(s[i] != '1') x += (1 << i);
if(s[i] == '0') {
y += (1 << i);
}
}
for(int i = y; ; i = (i - 1) & y) {
if(__popcount(i) & 1) {
ans -= dpr[x ^ i];
}
else {
ans += dpr[x ^ i];
}
if(i == 0) break;
}
}
else {
int x = 0;
int y = 0;
for(int i = 0; i < n; i++) {
if(s[i] != '0') x += (1 << i);
if(s[i] == '1') {
y += (1 << i);
}
}
for(int i = y; ; i = (i - 1) & y) {
if(__popcount(i) & 1) {
ans -= dp[x ^ i];
}
else {
ans += dp[x ^ i];
}
if(i == 0) break;
}
}
cout << ans << endl;
}
}
signed main() {
fastio();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4696 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4696 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1223 ms |
8488 KB |
Output is correct |
12 |
Correct |
1290 ms |
8144 KB |
Output is correct |
13 |
Correct |
1256 ms |
7448 KB |
Output is correct |
14 |
Correct |
1320 ms |
7636 KB |
Output is correct |
15 |
Correct |
1290 ms |
8572 KB |
Output is correct |
16 |
Correct |
1270 ms |
7784 KB |
Output is correct |
17 |
Correct |
1308 ms |
7636 KB |
Output is correct |
18 |
Correct |
1202 ms |
9520 KB |
Output is correct |
19 |
Correct |
1324 ms |
6312 KB |
Output is correct |
20 |
Correct |
1246 ms |
8028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4696 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1223 ms |
8488 KB |
Output is correct |
12 |
Correct |
1290 ms |
8144 KB |
Output is correct |
13 |
Correct |
1256 ms |
7448 KB |
Output is correct |
14 |
Correct |
1320 ms |
7636 KB |
Output is correct |
15 |
Correct |
1290 ms |
8572 KB |
Output is correct |
16 |
Correct |
1270 ms |
7784 KB |
Output is correct |
17 |
Correct |
1308 ms |
7636 KB |
Output is correct |
18 |
Correct |
1202 ms |
9520 KB |
Output is correct |
19 |
Correct |
1324 ms |
6312 KB |
Output is correct |
20 |
Correct |
1246 ms |
8028 KB |
Output is correct |
21 |
Correct |
1257 ms |
8540 KB |
Output is correct |
22 |
Correct |
1333 ms |
8708 KB |
Output is correct |
23 |
Correct |
1355 ms |
7780 KB |
Output is correct |
24 |
Correct |
1362 ms |
7716 KB |
Output is correct |
25 |
Correct |
1284 ms |
9672 KB |
Output is correct |
26 |
Correct |
1322 ms |
8020 KB |
Output is correct |
27 |
Correct |
1337 ms |
8124 KB |
Output is correct |
28 |
Correct |
1230 ms |
10412 KB |
Output is correct |
29 |
Correct |
1471 ms |
6592 KB |
Output is correct |
30 |
Correct |
1254 ms |
8848 KB |
Output is correct |
31 |
Correct |
1332 ms |
8760 KB |
Output is correct |
32 |
Correct |
1319 ms |
8456 KB |
Output is correct |
33 |
Correct |
1296 ms |
7632 KB |
Output is correct |
34 |
Correct |
1346 ms |
7620 KB |
Output is correct |
35 |
Correct |
1338 ms |
7800 KB |
Output is correct |
36 |
Correct |
1214 ms |
6592 KB |
Output is correct |
37 |
Correct |
1300 ms |
8424 KB |
Output is correct |
38 |
Correct |
1427 ms |
6392 KB |
Output is correct |
39 |
Correct |
1341 ms |
7724 KB |
Output is correct |
40 |
Correct |
1334 ms |
7376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4696 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
101 ms |
12944 KB |
Output is correct |
12 |
Correct |
102 ms |
12880 KB |
Output is correct |
13 |
Correct |
109 ms |
12740 KB |
Output is correct |
14 |
Correct |
112 ms |
13276 KB |
Output is correct |
15 |
Correct |
98 ms |
12880 KB |
Output is correct |
16 |
Correct |
115 ms |
12816 KB |
Output is correct |
17 |
Correct |
115 ms |
13268 KB |
Output is correct |
18 |
Correct |
105 ms |
13200 KB |
Output is correct |
19 |
Correct |
99 ms |
12804 KB |
Output is correct |
20 |
Correct |
99 ms |
12756 KB |
Output is correct |
21 |
Correct |
102 ms |
12884 KB |
Output is correct |
22 |
Correct |
126 ms |
13112 KB |
Output is correct |
23 |
Correct |
98 ms |
12784 KB |
Output is correct |
24 |
Correct |
105 ms |
12760 KB |
Output is correct |
25 |
Correct |
107 ms |
12880 KB |
Output is correct |
26 |
Correct |
92 ms |
12632 KB |
Output is correct |
27 |
Correct |
105 ms |
12884 KB |
Output is correct |
28 |
Correct |
100 ms |
12632 KB |
Output is correct |
29 |
Correct |
102 ms |
12884 KB |
Output is correct |
30 |
Correct |
105 ms |
12792 KB |
Output is correct |
31 |
Correct |
101 ms |
12760 KB |
Output is correct |
32 |
Correct |
111 ms |
12884 KB |
Output is correct |
33 |
Correct |
112 ms |
12764 KB |
Output is correct |
34 |
Correct |
93 ms |
12760 KB |
Output is correct |
35 |
Correct |
107 ms |
12848 KB |
Output is correct |
36 |
Correct |
106 ms |
12856 KB |
Output is correct |
37 |
Correct |
106 ms |
12880 KB |
Output is correct |
38 |
Correct |
114 ms |
13292 KB |
Output is correct |
39 |
Correct |
104 ms |
12800 KB |
Output is correct |
40 |
Correct |
108 ms |
12784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4696 KB |
Output is correct |
6 |
Correct |
2 ms |
4440 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1223 ms |
8488 KB |
Output is correct |
12 |
Correct |
1290 ms |
8144 KB |
Output is correct |
13 |
Correct |
1256 ms |
7448 KB |
Output is correct |
14 |
Correct |
1320 ms |
7636 KB |
Output is correct |
15 |
Correct |
1290 ms |
8572 KB |
Output is correct |
16 |
Correct |
1270 ms |
7784 KB |
Output is correct |
17 |
Correct |
1308 ms |
7636 KB |
Output is correct |
18 |
Correct |
1202 ms |
9520 KB |
Output is correct |
19 |
Correct |
1324 ms |
6312 KB |
Output is correct |
20 |
Correct |
1246 ms |
8028 KB |
Output is correct |
21 |
Correct |
1257 ms |
8540 KB |
Output is correct |
22 |
Correct |
1333 ms |
8708 KB |
Output is correct |
23 |
Correct |
1355 ms |
7780 KB |
Output is correct |
24 |
Correct |
1362 ms |
7716 KB |
Output is correct |
25 |
Correct |
1284 ms |
9672 KB |
Output is correct |
26 |
Correct |
1322 ms |
8020 KB |
Output is correct |
27 |
Correct |
1337 ms |
8124 KB |
Output is correct |
28 |
Correct |
1230 ms |
10412 KB |
Output is correct |
29 |
Correct |
1471 ms |
6592 KB |
Output is correct |
30 |
Correct |
1254 ms |
8848 KB |
Output is correct |
31 |
Correct |
1332 ms |
8760 KB |
Output is correct |
32 |
Correct |
1319 ms |
8456 KB |
Output is correct |
33 |
Correct |
1296 ms |
7632 KB |
Output is correct |
34 |
Correct |
1346 ms |
7620 KB |
Output is correct |
35 |
Correct |
1338 ms |
7800 KB |
Output is correct |
36 |
Correct |
1214 ms |
6592 KB |
Output is correct |
37 |
Correct |
1300 ms |
8424 KB |
Output is correct |
38 |
Correct |
1427 ms |
6392 KB |
Output is correct |
39 |
Correct |
1341 ms |
7724 KB |
Output is correct |
40 |
Correct |
1334 ms |
7376 KB |
Output is correct |
41 |
Correct |
101 ms |
12944 KB |
Output is correct |
42 |
Correct |
102 ms |
12880 KB |
Output is correct |
43 |
Correct |
109 ms |
12740 KB |
Output is correct |
44 |
Correct |
112 ms |
13276 KB |
Output is correct |
45 |
Correct |
98 ms |
12880 KB |
Output is correct |
46 |
Correct |
115 ms |
12816 KB |
Output is correct |
47 |
Correct |
115 ms |
13268 KB |
Output is correct |
48 |
Correct |
105 ms |
13200 KB |
Output is correct |
49 |
Correct |
99 ms |
12804 KB |
Output is correct |
50 |
Correct |
99 ms |
12756 KB |
Output is correct |
51 |
Correct |
102 ms |
12884 KB |
Output is correct |
52 |
Correct |
126 ms |
13112 KB |
Output is correct |
53 |
Correct |
98 ms |
12784 KB |
Output is correct |
54 |
Correct |
105 ms |
12760 KB |
Output is correct |
55 |
Correct |
107 ms |
12880 KB |
Output is correct |
56 |
Correct |
92 ms |
12632 KB |
Output is correct |
57 |
Correct |
105 ms |
12884 KB |
Output is correct |
58 |
Correct |
100 ms |
12632 KB |
Output is correct |
59 |
Correct |
102 ms |
12884 KB |
Output is correct |
60 |
Correct |
105 ms |
12792 KB |
Output is correct |
61 |
Correct |
101 ms |
12760 KB |
Output is correct |
62 |
Correct |
111 ms |
12884 KB |
Output is correct |
63 |
Correct |
112 ms |
12764 KB |
Output is correct |
64 |
Correct |
93 ms |
12760 KB |
Output is correct |
65 |
Correct |
107 ms |
12848 KB |
Output is correct |
66 |
Correct |
106 ms |
12856 KB |
Output is correct |
67 |
Correct |
106 ms |
12880 KB |
Output is correct |
68 |
Correct |
114 ms |
13292 KB |
Output is correct |
69 |
Correct |
104 ms |
12800 KB |
Output is correct |
70 |
Correct |
108 ms |
12784 KB |
Output is correct |
71 |
Correct |
1400 ms |
17744 KB |
Output is correct |
72 |
Correct |
1499 ms |
17720 KB |
Output is correct |
73 |
Correct |
1489 ms |
20356 KB |
Output is correct |
74 |
Correct |
1681 ms |
38524 KB |
Output is correct |
75 |
Correct |
1456 ms |
40320 KB |
Output is correct |
76 |
Correct |
1721 ms |
38716 KB |
Output is correct |
77 |
Correct |
1712 ms |
38728 KB |
Output is correct |
78 |
Correct |
1321 ms |
42292 KB |
Output is correct |
79 |
Correct |
1439 ms |
36208 KB |
Output is correct |
80 |
Correct |
1458 ms |
39520 KB |
Output is correct |
81 |
Correct |
1563 ms |
39556 KB |
Output is correct |
82 |
Correct |
1724 ms |
38148 KB |
Output is correct |
83 |
Correct |
1435 ms |
37476 KB |
Output is correct |
84 |
Correct |
1542 ms |
38140 KB |
Output is correct |
85 |
Correct |
1707 ms |
38424 KB |
Output is correct |
86 |
Correct |
1335 ms |
36464 KB |
Output is correct |
87 |
Correct |
1506 ms |
39464 KB |
Output is correct |
88 |
Correct |
1442 ms |
36484 KB |
Output is correct |
89 |
Correct |
1505 ms |
37764 KB |
Output is correct |
90 |
Correct |
1568 ms |
38228 KB |
Output is correct |
91 |
Correct |
1458 ms |
37312 KB |
Output is correct |
92 |
Correct |
1722 ms |
38736 KB |
Output is correct |
93 |
Correct |
1694 ms |
38580 KB |
Output is correct |
94 |
Correct |
1326 ms |
36184 KB |
Output is correct |
95 |
Correct |
1588 ms |
38380 KB |
Output is correct |
96 |
Correct |
1639 ms |
38420 KB |
Output is correct |
97 |
Correct |
1668 ms |
38296 KB |
Output is correct |
98 |
Correct |
1604 ms |
39024 KB |
Output is correct |
99 |
Correct |
1698 ms |
38220 KB |
Output is correct |
100 |
Correct |
1621 ms |
38664 KB |
Output is correct |