#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int readInt () {
bool minus = false;
int result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-') minus = true; else result = ch-'0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result*10 + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
int n, q;
const int MAXN = 20;
string s;
int dp[(1 << MAXN) + 7];
int dp2[(1 << MAXN) + 7];
int z(int x){
if(__builtin_popcount(x) % 2 == 0){
return 1;
}
return -1;
}
int z2(int a, int x){
if(((int)__builtin_popcount(a) - (int)__builtin_popcount(x)) % 2 == 0){
return 1;
}
return -1;
}
void solve(){
cin >> n >> q;
cin >> s;
for(int i = 0; i < (1 << n); i++){
dp[i] = s[i] - '0';
dp2[i] = s[i] - '0';
}
for(int i = 0; i < n; i++){
for(int msk = 0; msk < (1 << n); msk++){
if((msk & (1 << i))){
dp[msk] += dp[(msk ^ (1 << i))];
}else{
dp2[msk] += dp2[(msk ^ (1 << i))];
}
}
}
while(q--){
string t;
cin >> t;
reverse(t.begin(), t.end());
int a = 0, b = 0, c = 0;
for(int i = 0; i < n; i++){
if(t[i] == '0'){
a += (1 << i);
}else if(t[i] == '1'){
b += (1 << i);
}else{
c += (1 << i);
}
}
int ans = 0;
if(__builtin_popcount(c) <= 6){
for(int x = c; x > 0; x = (x - 1) & c){
ans += (s[x | b] - '0');
}
ans += (s[b] - '0');
}else if(__builtin_popcount(a) <= 6){
for(int x = a; x > 0; x = (x - 1) & a){
ans += (dp2[x | b] * z(x));
}
ans += dp2[b];
}else{
for(int x = b; x > 0; x = (x - 1) & b){
ans += (dp[x | c] * z2(b, x));
}
ans += (dp[c] * z(b));
}
cout << ans << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
203 ms |
17236 KB |
Output is correct |
12 |
Correct |
210 ms |
17244 KB |
Output is correct |
13 |
Correct |
174 ms |
16160 KB |
Output is correct |
14 |
Correct |
166 ms |
16072 KB |
Output is correct |
15 |
Correct |
205 ms |
17236 KB |
Output is correct |
16 |
Correct |
187 ms |
16384 KB |
Output is correct |
17 |
Correct |
200 ms |
16248 KB |
Output is correct |
18 |
Correct |
110 ms |
18068 KB |
Output is correct |
19 |
Correct |
157 ms |
15072 KB |
Output is correct |
20 |
Correct |
175 ms |
16724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
203 ms |
17236 KB |
Output is correct |
12 |
Correct |
210 ms |
17244 KB |
Output is correct |
13 |
Correct |
174 ms |
16160 KB |
Output is correct |
14 |
Correct |
166 ms |
16072 KB |
Output is correct |
15 |
Correct |
205 ms |
17236 KB |
Output is correct |
16 |
Correct |
187 ms |
16384 KB |
Output is correct |
17 |
Correct |
200 ms |
16248 KB |
Output is correct |
18 |
Correct |
110 ms |
18068 KB |
Output is correct |
19 |
Correct |
157 ms |
15072 KB |
Output is correct |
20 |
Correct |
175 ms |
16724 KB |
Output is correct |
21 |
Correct |
169 ms |
20164 KB |
Output is correct |
22 |
Correct |
249 ms |
20324 KB |
Output is correct |
23 |
Correct |
211 ms |
19284 KB |
Output is correct |
24 |
Correct |
205 ms |
19140 KB |
Output is correct |
25 |
Correct |
195 ms |
21064 KB |
Output is correct |
26 |
Correct |
234 ms |
19796 KB |
Output is correct |
27 |
Correct |
218 ms |
19540 KB |
Output is correct |
28 |
Correct |
125 ms |
22100 KB |
Output is correct |
29 |
Correct |
172 ms |
18036 KB |
Output is correct |
30 |
Correct |
220 ms |
20308 KB |
Output is correct |
31 |
Correct |
250 ms |
20128 KB |
Output is correct |
32 |
Correct |
224 ms |
20096 KB |
Output is correct |
33 |
Correct |
180 ms |
19028 KB |
Output is correct |
34 |
Correct |
204 ms |
19192 KB |
Output is correct |
35 |
Correct |
220 ms |
19616 KB |
Output is correct |
36 |
Correct |
113 ms |
18260 KB |
Output is correct |
37 |
Correct |
213 ms |
20176 KB |
Output is correct |
38 |
Correct |
176 ms |
18212 KB |
Output is correct |
39 |
Correct |
200 ms |
19284 KB |
Output is correct |
40 |
Correct |
202 ms |
19088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
40 ms |
11972 KB |
Output is correct |
12 |
Correct |
34 ms |
12028 KB |
Output is correct |
13 |
Correct |
39 ms |
12048 KB |
Output is correct |
14 |
Correct |
48 ms |
12020 KB |
Output is correct |
15 |
Correct |
36 ms |
12032 KB |
Output is correct |
16 |
Correct |
47 ms |
12104 KB |
Output is correct |
17 |
Correct |
40 ms |
11864 KB |
Output is correct |
18 |
Correct |
31 ms |
12024 KB |
Output is correct |
19 |
Correct |
33 ms |
11776 KB |
Output is correct |
20 |
Correct |
34 ms |
12120 KB |
Output is correct |
21 |
Correct |
42 ms |
12080 KB |
Output is correct |
22 |
Correct |
58 ms |
11964 KB |
Output is correct |
23 |
Correct |
33 ms |
12020 KB |
Output is correct |
24 |
Correct |
40 ms |
12064 KB |
Output is correct |
25 |
Correct |
41 ms |
12016 KB |
Output is correct |
26 |
Correct |
29 ms |
11768 KB |
Output is correct |
27 |
Correct |
40 ms |
12012 KB |
Output is correct |
28 |
Correct |
34 ms |
11764 KB |
Output is correct |
29 |
Correct |
37 ms |
12020 KB |
Output is correct |
30 |
Correct |
40 ms |
11880 KB |
Output is correct |
31 |
Correct |
35 ms |
12020 KB |
Output is correct |
32 |
Correct |
43 ms |
11864 KB |
Output is correct |
33 |
Correct |
40 ms |
11952 KB |
Output is correct |
34 |
Correct |
28 ms |
11768 KB |
Output is correct |
35 |
Correct |
38 ms |
12020 KB |
Output is correct |
36 |
Correct |
38 ms |
12020 KB |
Output is correct |
37 |
Correct |
39 ms |
12020 KB |
Output is correct |
38 |
Correct |
37 ms |
12020 KB |
Output is correct |
39 |
Correct |
38 ms |
12020 KB |
Output is correct |
40 |
Correct |
37 ms |
11864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
203 ms |
17236 KB |
Output is correct |
12 |
Correct |
210 ms |
17244 KB |
Output is correct |
13 |
Correct |
174 ms |
16160 KB |
Output is correct |
14 |
Correct |
166 ms |
16072 KB |
Output is correct |
15 |
Correct |
205 ms |
17236 KB |
Output is correct |
16 |
Correct |
187 ms |
16384 KB |
Output is correct |
17 |
Correct |
200 ms |
16248 KB |
Output is correct |
18 |
Correct |
110 ms |
18068 KB |
Output is correct |
19 |
Correct |
157 ms |
15072 KB |
Output is correct |
20 |
Correct |
175 ms |
16724 KB |
Output is correct |
21 |
Correct |
169 ms |
20164 KB |
Output is correct |
22 |
Correct |
249 ms |
20324 KB |
Output is correct |
23 |
Correct |
211 ms |
19284 KB |
Output is correct |
24 |
Correct |
205 ms |
19140 KB |
Output is correct |
25 |
Correct |
195 ms |
21064 KB |
Output is correct |
26 |
Correct |
234 ms |
19796 KB |
Output is correct |
27 |
Correct |
218 ms |
19540 KB |
Output is correct |
28 |
Correct |
125 ms |
22100 KB |
Output is correct |
29 |
Correct |
172 ms |
18036 KB |
Output is correct |
30 |
Correct |
220 ms |
20308 KB |
Output is correct |
31 |
Correct |
250 ms |
20128 KB |
Output is correct |
32 |
Correct |
224 ms |
20096 KB |
Output is correct |
33 |
Correct |
180 ms |
19028 KB |
Output is correct |
34 |
Correct |
204 ms |
19192 KB |
Output is correct |
35 |
Correct |
220 ms |
19616 KB |
Output is correct |
36 |
Correct |
113 ms |
18260 KB |
Output is correct |
37 |
Correct |
213 ms |
20176 KB |
Output is correct |
38 |
Correct |
176 ms |
18212 KB |
Output is correct |
39 |
Correct |
200 ms |
19284 KB |
Output is correct |
40 |
Correct |
202 ms |
19088 KB |
Output is correct |
41 |
Correct |
40 ms |
11972 KB |
Output is correct |
42 |
Correct |
34 ms |
12028 KB |
Output is correct |
43 |
Correct |
39 ms |
12048 KB |
Output is correct |
44 |
Correct |
48 ms |
12020 KB |
Output is correct |
45 |
Correct |
36 ms |
12032 KB |
Output is correct |
46 |
Correct |
47 ms |
12104 KB |
Output is correct |
47 |
Correct |
40 ms |
11864 KB |
Output is correct |
48 |
Correct |
31 ms |
12024 KB |
Output is correct |
49 |
Correct |
33 ms |
11776 KB |
Output is correct |
50 |
Correct |
34 ms |
12120 KB |
Output is correct |
51 |
Correct |
42 ms |
12080 KB |
Output is correct |
52 |
Correct |
58 ms |
11964 KB |
Output is correct |
53 |
Correct |
33 ms |
12020 KB |
Output is correct |
54 |
Correct |
40 ms |
12064 KB |
Output is correct |
55 |
Correct |
41 ms |
12016 KB |
Output is correct |
56 |
Correct |
29 ms |
11768 KB |
Output is correct |
57 |
Correct |
40 ms |
12012 KB |
Output is correct |
58 |
Correct |
34 ms |
11764 KB |
Output is correct |
59 |
Correct |
37 ms |
12020 KB |
Output is correct |
60 |
Correct |
40 ms |
11880 KB |
Output is correct |
61 |
Correct |
35 ms |
12020 KB |
Output is correct |
62 |
Correct |
43 ms |
11864 KB |
Output is correct |
63 |
Correct |
40 ms |
11952 KB |
Output is correct |
64 |
Correct |
28 ms |
11768 KB |
Output is correct |
65 |
Correct |
38 ms |
12020 KB |
Output is correct |
66 |
Correct |
38 ms |
12020 KB |
Output is correct |
67 |
Correct |
39 ms |
12020 KB |
Output is correct |
68 |
Correct |
37 ms |
12020 KB |
Output is correct |
69 |
Correct |
38 ms |
12020 KB |
Output is correct |
70 |
Correct |
37 ms |
11864 KB |
Output is correct |
71 |
Correct |
256 ms |
36340 KB |
Output is correct |
72 |
Correct |
316 ms |
36424 KB |
Output is correct |
73 |
Correct |
323 ms |
35064 KB |
Output is correct |
74 |
Correct |
540 ms |
35316 KB |
Output is correct |
75 |
Correct |
303 ms |
37240 KB |
Output is correct |
76 |
Correct |
517 ms |
36604 KB |
Output is correct |
77 |
Correct |
376 ms |
35572 KB |
Output is correct |
78 |
Correct |
188 ms |
39340 KB |
Output is correct |
79 |
Correct |
262 ms |
33268 KB |
Output is correct |
80 |
Correct |
273 ms |
36440 KB |
Output is correct |
81 |
Correct |
387 ms |
36340 KB |
Output is correct |
82 |
Correct |
526 ms |
35368 KB |
Output is correct |
83 |
Correct |
270 ms |
34540 KB |
Output is correct |
84 |
Correct |
383 ms |
35376 KB |
Output is correct |
85 |
Correct |
392 ms |
35572 KB |
Output is correct |
86 |
Correct |
163 ms |
33264 KB |
Output is correct |
87 |
Correct |
244 ms |
36188 KB |
Output is correct |
88 |
Correct |
249 ms |
33268 KB |
Output is correct |
89 |
Correct |
319 ms |
34920 KB |
Output is correct |
90 |
Correct |
358 ms |
35320 KB |
Output is correct |
91 |
Correct |
268 ms |
34548 KB |
Output is correct |
92 |
Correct |
457 ms |
35580 KB |
Output is correct |
93 |
Correct |
397 ms |
35372 KB |
Output is correct |
94 |
Correct |
153 ms |
33300 KB |
Output is correct |
95 |
Correct |
336 ms |
35312 KB |
Output is correct |
96 |
Correct |
359 ms |
35304 KB |
Output is correct |
97 |
Correct |
363 ms |
35316 KB |
Output is correct |
98 |
Correct |
335 ms |
35476 KB |
Output is correct |
99 |
Correct |
365 ms |
35360 KB |
Output is correct |
100 |
Correct |
364 ms |
35412 KB |
Output is correct |