//khodaya khodet komak kon
# include <bits/stdc++.h>
/*
// ordered_set
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
# define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
*/
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <pii, pii> ppp;
typedef pair <ll, ll> pll;
# define A first
# define B second
# define endl '\n'
# define sep ' '
# define lc id << 1
# define rc lc | 1
# define all(x) x.begin(), x.end()
# define kill(x) return cout << x << endl, 0
# define SZ(x) int(x.size())
# define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
const int xn = 21;
const int xm = - 20 + 10;
const int sq = 320;
const ll inf = 1e18 + 10;
const ll INF = 1e18 + 10;
const int mod = 998244353;
const int base = 257;
int n, q, a[1 << xn], cnt[3], ted[1 << xn];
pii dp[1 << xn];
string s;
void solve3(){
int mask = 0, tot = 0, ans = 0;
for (int i = 0; i < n; ++ i){
if (s[i] == '?')
mask += (1 << i);
else
tot += (s[i] - '0') * (1 << i);
}
for (int sub = mask; sub > 0; sub = ((sub - 1) & mask))
ans += a[tot + sub];
ans += a[tot];
cout << ans << endl;
}
void solve2(){
int tot = 0, ans = 0, mask = 0;
for (int i = 0; i < n; ++ i){
if (s[i] != '0')
tot += (1 << i);
if (s[i] == '1')
mask += (1 << i);
}
ans = dp[tot].A;
for (int sub = mask; sub > 0; sub = ((sub - 1) & mask))
ans += dp[tot - sub].A * ted[sub];
cout << ans << endl;
}
void solve1(){
int tot = 0, ans = 0, mask = 0;
for (int i = 0; i < n; ++ i){
if (s[i] == '1')
tot += (1 << i);
else if (s[i] == '0')
mask += (1 << i);
}
ans = dp[tot].B;
for (int sub = mask; sub > 0; sub = ((sub - 1) & mask))
ans += dp[tot + sub].B * ted[sub];
cout << ans << endl;
}
int main(){
InTheNameOfGod;
cin >> n >> q >> s;
for (int i = 0; i < (1 << n); ++ i)
a[i] = dp[i].A = dp[i].B = s[i] - '0';
for (int bit = 0; bit < n; ++ bit){
for (int mask = (1 << n) - 1; mask >= 0; -- mask)
if ((mask & (1 << bit)))
dp[mask].A += dp[mask - (1 << bit)].A;
for (int mask = 0; mask < (1 << n); ++ mask)
if (!(mask & (1 << bit)))
dp[mask].B += dp[mask + (1 << bit)].B;
}
ted[0] = 1;
for (int mask = 1; mask < (1 << n); ++ mask)
ted[mask] = ted[mask - (mask & - mask)] * (- 1);
while (q --){
cin >> s, reverse(all(s));
cnt[0] = cnt[1] = cnt[2] = 0;
for (char c : s){
if (c == '0')
++ cnt[0];
else if (c == '1')
++ cnt[1];
else
++ cnt[2];
}
if (cnt[0] <= n / 3)
solve1();
else if (cnt[1] <= n / 3)
solve2();
else
solve3();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
279 ms |
5484 KB |
Output is correct |
12 |
Correct |
287 ms |
5228 KB |
Output is correct |
13 |
Correct |
331 ms |
4332 KB |
Output is correct |
14 |
Correct |
335 ms |
4460 KB |
Output is correct |
15 |
Correct |
290 ms |
5484 KB |
Output is correct |
16 |
Correct |
364 ms |
4612 KB |
Output is correct |
17 |
Correct |
358 ms |
4716 KB |
Output is correct |
18 |
Correct |
238 ms |
6636 KB |
Output is correct |
19 |
Correct |
279 ms |
3404 KB |
Output is correct |
20 |
Correct |
312 ms |
4716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
279 ms |
5484 KB |
Output is correct |
12 |
Correct |
287 ms |
5228 KB |
Output is correct |
13 |
Correct |
331 ms |
4332 KB |
Output is correct |
14 |
Correct |
335 ms |
4460 KB |
Output is correct |
15 |
Correct |
290 ms |
5484 KB |
Output is correct |
16 |
Correct |
364 ms |
4612 KB |
Output is correct |
17 |
Correct |
358 ms |
4716 KB |
Output is correct |
18 |
Correct |
238 ms |
6636 KB |
Output is correct |
19 |
Correct |
279 ms |
3404 KB |
Output is correct |
20 |
Correct |
312 ms |
4716 KB |
Output is correct |
21 |
Correct |
329 ms |
5100 KB |
Output is correct |
22 |
Correct |
353 ms |
5072 KB |
Output is correct |
23 |
Correct |
415 ms |
4076 KB |
Output is correct |
24 |
Correct |
434 ms |
4000 KB |
Output is correct |
25 |
Correct |
343 ms |
5996 KB |
Output is correct |
26 |
Correct |
418 ms |
4332 KB |
Output is correct |
27 |
Correct |
424 ms |
4460 KB |
Output is correct |
28 |
Correct |
258 ms |
6968 KB |
Output is correct |
29 |
Correct |
296 ms |
3180 KB |
Output is correct |
30 |
Correct |
352 ms |
5228 KB |
Output is correct |
31 |
Correct |
389 ms |
4972 KB |
Output is correct |
32 |
Correct |
406 ms |
5228 KB |
Output is correct |
33 |
Correct |
365 ms |
3948 KB |
Output is correct |
34 |
Correct |
416 ms |
4076 KB |
Output is correct |
35 |
Correct |
420 ms |
4460 KB |
Output is correct |
36 |
Correct |
251 ms |
3052 KB |
Output is correct |
37 |
Correct |
325 ms |
4972 KB |
Output is correct |
38 |
Correct |
322 ms |
2924 KB |
Output is correct |
39 |
Correct |
405 ms |
4204 KB |
Output is correct |
40 |
Correct |
406 ms |
4204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
93 ms |
18448 KB |
Output is correct |
12 |
Correct |
91 ms |
18448 KB |
Output is correct |
13 |
Correct |
106 ms |
18448 KB |
Output is correct |
14 |
Correct |
129 ms |
18448 KB |
Output is correct |
15 |
Correct |
93 ms |
18704 KB |
Output is correct |
16 |
Correct |
123 ms |
18448 KB |
Output is correct |
17 |
Correct |
111 ms |
18448 KB |
Output is correct |
18 |
Correct |
80 ms |
18704 KB |
Output is correct |
19 |
Correct |
103 ms |
18320 KB |
Output is correct |
20 |
Correct |
92 ms |
18448 KB |
Output is correct |
21 |
Correct |
101 ms |
18448 KB |
Output is correct |
22 |
Correct |
122 ms |
18448 KB |
Output is correct |
23 |
Correct |
114 ms |
18448 KB |
Output is correct |
24 |
Correct |
117 ms |
18448 KB |
Output is correct |
25 |
Correct |
113 ms |
18448 KB |
Output is correct |
26 |
Correct |
79 ms |
18460 KB |
Output is correct |
27 |
Correct |
96 ms |
18448 KB |
Output is correct |
28 |
Correct |
91 ms |
18320 KB |
Output is correct |
29 |
Correct |
103 ms |
18448 KB |
Output is correct |
30 |
Correct |
104 ms |
18448 KB |
Output is correct |
31 |
Correct |
92 ms |
18468 KB |
Output is correct |
32 |
Correct |
120 ms |
18448 KB |
Output is correct |
33 |
Correct |
110 ms |
18576 KB |
Output is correct |
34 |
Correct |
93 ms |
18320 KB |
Output is correct |
35 |
Correct |
106 ms |
18476 KB |
Output is correct |
36 |
Correct |
102 ms |
18448 KB |
Output is correct |
37 |
Correct |
102 ms |
18448 KB |
Output is correct |
38 |
Correct |
111 ms |
18436 KB |
Output is correct |
39 |
Correct |
124 ms |
18624 KB |
Output is correct |
40 |
Correct |
104 ms |
18448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
279 ms |
5484 KB |
Output is correct |
12 |
Correct |
287 ms |
5228 KB |
Output is correct |
13 |
Correct |
331 ms |
4332 KB |
Output is correct |
14 |
Correct |
335 ms |
4460 KB |
Output is correct |
15 |
Correct |
290 ms |
5484 KB |
Output is correct |
16 |
Correct |
364 ms |
4612 KB |
Output is correct |
17 |
Correct |
358 ms |
4716 KB |
Output is correct |
18 |
Correct |
238 ms |
6636 KB |
Output is correct |
19 |
Correct |
279 ms |
3404 KB |
Output is correct |
20 |
Correct |
312 ms |
4716 KB |
Output is correct |
21 |
Correct |
329 ms |
5100 KB |
Output is correct |
22 |
Correct |
353 ms |
5072 KB |
Output is correct |
23 |
Correct |
415 ms |
4076 KB |
Output is correct |
24 |
Correct |
434 ms |
4000 KB |
Output is correct |
25 |
Correct |
343 ms |
5996 KB |
Output is correct |
26 |
Correct |
418 ms |
4332 KB |
Output is correct |
27 |
Correct |
424 ms |
4460 KB |
Output is correct |
28 |
Correct |
258 ms |
6968 KB |
Output is correct |
29 |
Correct |
296 ms |
3180 KB |
Output is correct |
30 |
Correct |
352 ms |
5228 KB |
Output is correct |
31 |
Correct |
389 ms |
4972 KB |
Output is correct |
32 |
Correct |
406 ms |
5228 KB |
Output is correct |
33 |
Correct |
365 ms |
3948 KB |
Output is correct |
34 |
Correct |
416 ms |
4076 KB |
Output is correct |
35 |
Correct |
420 ms |
4460 KB |
Output is correct |
36 |
Correct |
251 ms |
3052 KB |
Output is correct |
37 |
Correct |
325 ms |
4972 KB |
Output is correct |
38 |
Correct |
322 ms |
2924 KB |
Output is correct |
39 |
Correct |
405 ms |
4204 KB |
Output is correct |
40 |
Correct |
406 ms |
4204 KB |
Output is correct |
41 |
Correct |
93 ms |
18448 KB |
Output is correct |
42 |
Correct |
91 ms |
18448 KB |
Output is correct |
43 |
Correct |
106 ms |
18448 KB |
Output is correct |
44 |
Correct |
129 ms |
18448 KB |
Output is correct |
45 |
Correct |
93 ms |
18704 KB |
Output is correct |
46 |
Correct |
123 ms |
18448 KB |
Output is correct |
47 |
Correct |
111 ms |
18448 KB |
Output is correct |
48 |
Correct |
80 ms |
18704 KB |
Output is correct |
49 |
Correct |
103 ms |
18320 KB |
Output is correct |
50 |
Correct |
92 ms |
18448 KB |
Output is correct |
51 |
Correct |
101 ms |
18448 KB |
Output is correct |
52 |
Correct |
122 ms |
18448 KB |
Output is correct |
53 |
Correct |
114 ms |
18448 KB |
Output is correct |
54 |
Correct |
117 ms |
18448 KB |
Output is correct |
55 |
Correct |
113 ms |
18448 KB |
Output is correct |
56 |
Correct |
79 ms |
18460 KB |
Output is correct |
57 |
Correct |
96 ms |
18448 KB |
Output is correct |
58 |
Correct |
91 ms |
18320 KB |
Output is correct |
59 |
Correct |
103 ms |
18448 KB |
Output is correct |
60 |
Correct |
104 ms |
18448 KB |
Output is correct |
61 |
Correct |
92 ms |
18468 KB |
Output is correct |
62 |
Correct |
120 ms |
18448 KB |
Output is correct |
63 |
Correct |
110 ms |
18576 KB |
Output is correct |
64 |
Correct |
93 ms |
18320 KB |
Output is correct |
65 |
Correct |
106 ms |
18476 KB |
Output is correct |
66 |
Correct |
102 ms |
18448 KB |
Output is correct |
67 |
Correct |
102 ms |
18448 KB |
Output is correct |
68 |
Correct |
111 ms |
18436 KB |
Output is correct |
69 |
Correct |
124 ms |
18624 KB |
Output is correct |
70 |
Correct |
104 ms |
18448 KB |
Output is correct |
71 |
Correct |
637 ms |
23512 KB |
Output is correct |
72 |
Correct |
690 ms |
23756 KB |
Output is correct |
73 |
Correct |
999 ms |
22288 KB |
Output is correct |
74 |
Correct |
1328 ms |
22856 KB |
Output is correct |
75 |
Correct |
614 ms |
24380 KB |
Output is correct |
76 |
Correct |
1337 ms |
22668 KB |
Output is correct |
77 |
Correct |
1061 ms |
22420 KB |
Output is correct |
78 |
Correct |
391 ms |
26256 KB |
Output is correct |
79 |
Correct |
485 ms |
20240 KB |
Output is correct |
80 |
Correct |
639 ms |
23568 KB |
Output is correct |
81 |
Correct |
850 ms |
23384 KB |
Output is correct |
82 |
Correct |
1237 ms |
22208 KB |
Output is correct |
83 |
Correct |
635 ms |
21380 KB |
Output is correct |
84 |
Correct |
1136 ms |
22544 KB |
Output is correct |
85 |
Correct |
1022 ms |
22488 KB |
Output is correct |
86 |
Correct |
356 ms |
20372 KB |
Output is correct |
87 |
Correct |
615 ms |
23316 KB |
Output is correct |
88 |
Correct |
583 ms |
20496 KB |
Output is correct |
89 |
Correct |
843 ms |
22160 KB |
Output is correct |
90 |
Correct |
872 ms |
22416 KB |
Output is correct |
91 |
Correct |
683 ms |
21648 KB |
Output is correct |
92 |
Correct |
1236 ms |
22708 KB |
Output is correct |
93 |
Correct |
1044 ms |
22724 KB |
Output is correct |
94 |
Correct |
347 ms |
20768 KB |
Output is correct |
95 |
Correct |
898 ms |
22848 KB |
Output is correct |
96 |
Correct |
892 ms |
23184 KB |
Output is correct |
97 |
Correct |
897 ms |
22672 KB |
Output is correct |
98 |
Correct |
921 ms |
22624 KB |
Output is correct |
99 |
Correct |
901 ms |
22544 KB |
Output is correct |
100 |
Correct |
909 ms |
22636 KB |
Output is correct |