/*Author: KawakiMeido*/
#include <bits/stdc++.h>
#define pb push_back
#define endl "\n"
#define ll long long
#define all(x) (x).begin(),(x).end()
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
/*Constants*/
const int N = 2e5+10;
const int INF = 1e9+7;
const long long LLINF = 1e18+3;
/*TestCases*/
int test=1;
void solve();
void TestCases(bool v){
if (v) cin >> test;
while(test--) solve();
}
/*Global Variables*/
int n,q;
int a[(1<<20)+10];
int f[(1<<20)+10];
int g[(1<<20)+10];
int dp[(1<<6)+10];
vector<int> pos;
int BasedOne(int m, int x, vector<int>& pos){
int res = 0;
for (int mask=0; mask<(1<<m); mask++){
int cnt = m-__builtin_popcount(mask);
int val = 0;
for (int i=0; i<m; i++){
if (mask&(1<<i)) val = (val^(1<<pos[i]));
}
if (cnt%2) res-=f[x|val];
else res+=f[x|val];
}
return res;
}
int BasedZero(int m, int x, vector<int>& pos){
int res = 0;
for (int mask=0; mask<(1<<m); mask++){
int cnt = __builtin_popcount(mask);
int val = 0;
for (int i=0; i<m; i++){
if ((mask&(1<<i))) val = (val^(1<<pos[i]));
}
if (cnt%2) res-=g[x|val];
else res+=g[x|val];
}
return res;
}
int BasedQuestion(int m, int x, vector<int>& pos){
int res = 0;
for (int mask=0; mask<(1<<m); mask++){
// int cnt = __builtin_popcount(mask);
int val = x;
for (int i=0; i<m; i++){
if (mask&(1<<i)) val = (val^(1<<pos[i]));
}
res+=a[val];
}
return res;
}
/*Solution*/
void solve(){
cin >> n >> q;
for (int i=0; i<(1<<n); i++){
char c; cin >> c;
f[i] = c-'0';
a[i] = c-'0';
g[i] = c-'0';
}
for (int i=0; i<n; i++){
for (int mask=0; mask<(1<<n); mask++){
if (mask&(1<<i)){
f[mask]+=f[mask^(1<<i)];
}
}
}
for (int i=0; i<n; i++){
for (int mask=(1<<n)-1; mask>=0; mask--){
if (!(mask&(1<<i))){
g[mask]+=g[mask^(1<<i)];
}
}
}
string s;
for (int i=1; i<=q; i++){
pos.clear();
cin >> s;
int x=0;
int mask0=0,mask1=0,maskq=0;
int zerocnt=0,onecnt=0,questioncnt=0;
for (int i=0; i<n; i++){
int idx = n-i-1;
if (s[i] == '?'){
++questioncnt;
maskq = maskq^(1<<idx);
}
if (s[i] == '1'){
++onecnt;
mask1 = mask1^(1<<idx);
}
if (s[i] == '0'){
++zerocnt;
mask0 = mask0^(1<<idx);
}
}
int mn = min({zerocnt,onecnt,questioncnt});
if (zerocnt == mn){
for (int i=n-1; i>=0; i--){
int idx = n-i-1;
if (s[i] == '0'){
pos.push_back(idx);
}
}
cout << BasedZero(zerocnt,mask1,pos) << endl;
}
else if (onecnt == mn){
for (int i=n-1; i>=0; i--){
int idx = n-i-1;
if (s[i] == '1'){
pos.push_back(idx);
}
if (s[i] == '?'){
x = x^(1<<idx);
}
}
cout << BasedOne(onecnt,maskq,pos) << endl;
}
else {
for (int i=n-1; i>=0; i--){
int idx = n-i-1;
if (s[i] == '?'){
pos.push_back(idx);
}
}
cout << BasedQuestion(questioncnt,mask1,pos) << endl;
}
// cout << "x: " << x << " " << f[x] << endl;
}
}
/*Driver Code*/
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
TestCases(0);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
2 ms |
4432 KB |
Output is correct |
5 |
Correct |
2 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4600 KB |
Output is correct |
7 |
Correct |
2 ms |
4432 KB |
Output is correct |
8 |
Correct |
2 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
2 ms |
4432 KB |
Output is correct |
5 |
Correct |
2 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4600 KB |
Output is correct |
7 |
Correct |
2 ms |
4432 KB |
Output is correct |
8 |
Correct |
2 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
168 ms |
19272 KB |
Output is correct |
12 |
Correct |
177 ms |
19016 KB |
Output is correct |
13 |
Correct |
217 ms |
18220 KB |
Output is correct |
14 |
Correct |
244 ms |
18248 KB |
Output is correct |
15 |
Correct |
194 ms |
19228 KB |
Output is correct |
16 |
Correct |
237 ms |
18504 KB |
Output is correct |
17 |
Correct |
226 ms |
18248 KB |
Output is correct |
18 |
Correct |
126 ms |
20296 KB |
Output is correct |
19 |
Correct |
150 ms |
17280 KB |
Output is correct |
20 |
Correct |
171 ms |
18904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
2 ms |
4432 KB |
Output is correct |
5 |
Correct |
2 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4600 KB |
Output is correct |
7 |
Correct |
2 ms |
4432 KB |
Output is correct |
8 |
Correct |
2 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
168 ms |
19272 KB |
Output is correct |
12 |
Correct |
177 ms |
19016 KB |
Output is correct |
13 |
Correct |
217 ms |
18220 KB |
Output is correct |
14 |
Correct |
244 ms |
18248 KB |
Output is correct |
15 |
Correct |
194 ms |
19228 KB |
Output is correct |
16 |
Correct |
237 ms |
18504 KB |
Output is correct |
17 |
Correct |
226 ms |
18248 KB |
Output is correct |
18 |
Correct |
126 ms |
20296 KB |
Output is correct |
19 |
Correct |
150 ms |
17280 KB |
Output is correct |
20 |
Correct |
171 ms |
18904 KB |
Output is correct |
21 |
Correct |
187 ms |
22104 KB |
Output is correct |
22 |
Correct |
224 ms |
22436 KB |
Output is correct |
23 |
Correct |
277 ms |
21300 KB |
Output is correct |
24 |
Correct |
335 ms |
21236 KB |
Output is correct |
25 |
Correct |
265 ms |
23112 KB |
Output is correct |
26 |
Correct |
310 ms |
21796 KB |
Output is correct |
27 |
Correct |
318 ms |
21576 KB |
Output is correct |
28 |
Correct |
157 ms |
24136 KB |
Output is correct |
29 |
Correct |
181 ms |
20296 KB |
Output is correct |
30 |
Correct |
230 ms |
22348 KB |
Output is correct |
31 |
Correct |
287 ms |
22296 KB |
Output is correct |
32 |
Correct |
362 ms |
22092 KB |
Output is correct |
33 |
Correct |
232 ms |
21064 KB |
Output is correct |
34 |
Correct |
327 ms |
21320 KB |
Output is correct |
35 |
Correct |
303 ms |
21576 KB |
Output is correct |
36 |
Correct |
131 ms |
20280 KB |
Output is correct |
37 |
Correct |
213 ms |
22196 KB |
Output is correct |
38 |
Correct |
190 ms |
20296 KB |
Output is correct |
39 |
Correct |
255 ms |
21344 KB |
Output is correct |
40 |
Correct |
336 ms |
21320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
2 ms |
4432 KB |
Output is correct |
5 |
Correct |
2 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4600 KB |
Output is correct |
7 |
Correct |
2 ms |
4432 KB |
Output is correct |
8 |
Correct |
2 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
61 ms |
14920 KB |
Output is correct |
12 |
Correct |
64 ms |
15052 KB |
Output is correct |
13 |
Correct |
66 ms |
14796 KB |
Output is correct |
14 |
Correct |
96 ms |
14796 KB |
Output is correct |
15 |
Correct |
63 ms |
14920 KB |
Output is correct |
16 |
Correct |
88 ms |
14920 KB |
Output is correct |
17 |
Correct |
77 ms |
14796 KB |
Output is correct |
18 |
Correct |
55 ms |
15052 KB |
Output is correct |
19 |
Correct |
54 ms |
14920 KB |
Output is correct |
20 |
Correct |
52 ms |
14920 KB |
Output is correct |
21 |
Correct |
62 ms |
15048 KB |
Output is correct |
22 |
Correct |
91 ms |
14920 KB |
Output is correct |
23 |
Correct |
59 ms |
14800 KB |
Output is correct |
24 |
Correct |
109 ms |
14816 KB |
Output is correct |
25 |
Correct |
84 ms |
14920 KB |
Output is correct |
26 |
Correct |
49 ms |
14920 KB |
Output is correct |
27 |
Correct |
60 ms |
15056 KB |
Output is correct |
28 |
Correct |
60 ms |
14800 KB |
Output is correct |
29 |
Correct |
69 ms |
14920 KB |
Output is correct |
30 |
Correct |
86 ms |
14840 KB |
Output is correct |
31 |
Correct |
60 ms |
14812 KB |
Output is correct |
32 |
Correct |
93 ms |
14920 KB |
Output is correct |
33 |
Correct |
81 ms |
14920 KB |
Output is correct |
34 |
Correct |
52 ms |
14928 KB |
Output is correct |
35 |
Correct |
76 ms |
14920 KB |
Output is correct |
36 |
Correct |
75 ms |
14972 KB |
Output is correct |
37 |
Correct |
70 ms |
14920 KB |
Output is correct |
38 |
Correct |
75 ms |
14920 KB |
Output is correct |
39 |
Correct |
73 ms |
14920 KB |
Output is correct |
40 |
Correct |
76 ms |
14936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
2 ms |
4432 KB |
Output is correct |
5 |
Correct |
2 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4600 KB |
Output is correct |
7 |
Correct |
2 ms |
4432 KB |
Output is correct |
8 |
Correct |
2 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
168 ms |
19272 KB |
Output is correct |
12 |
Correct |
177 ms |
19016 KB |
Output is correct |
13 |
Correct |
217 ms |
18220 KB |
Output is correct |
14 |
Correct |
244 ms |
18248 KB |
Output is correct |
15 |
Correct |
194 ms |
19228 KB |
Output is correct |
16 |
Correct |
237 ms |
18504 KB |
Output is correct |
17 |
Correct |
226 ms |
18248 KB |
Output is correct |
18 |
Correct |
126 ms |
20296 KB |
Output is correct |
19 |
Correct |
150 ms |
17280 KB |
Output is correct |
20 |
Correct |
171 ms |
18904 KB |
Output is correct |
21 |
Correct |
187 ms |
22104 KB |
Output is correct |
22 |
Correct |
224 ms |
22436 KB |
Output is correct |
23 |
Correct |
277 ms |
21300 KB |
Output is correct |
24 |
Correct |
335 ms |
21236 KB |
Output is correct |
25 |
Correct |
265 ms |
23112 KB |
Output is correct |
26 |
Correct |
310 ms |
21796 KB |
Output is correct |
27 |
Correct |
318 ms |
21576 KB |
Output is correct |
28 |
Correct |
157 ms |
24136 KB |
Output is correct |
29 |
Correct |
181 ms |
20296 KB |
Output is correct |
30 |
Correct |
230 ms |
22348 KB |
Output is correct |
31 |
Correct |
287 ms |
22296 KB |
Output is correct |
32 |
Correct |
362 ms |
22092 KB |
Output is correct |
33 |
Correct |
232 ms |
21064 KB |
Output is correct |
34 |
Correct |
327 ms |
21320 KB |
Output is correct |
35 |
Correct |
303 ms |
21576 KB |
Output is correct |
36 |
Correct |
131 ms |
20280 KB |
Output is correct |
37 |
Correct |
213 ms |
22196 KB |
Output is correct |
38 |
Correct |
190 ms |
20296 KB |
Output is correct |
39 |
Correct |
255 ms |
21344 KB |
Output is correct |
40 |
Correct |
336 ms |
21320 KB |
Output is correct |
41 |
Correct |
61 ms |
14920 KB |
Output is correct |
42 |
Correct |
64 ms |
15052 KB |
Output is correct |
43 |
Correct |
66 ms |
14796 KB |
Output is correct |
44 |
Correct |
96 ms |
14796 KB |
Output is correct |
45 |
Correct |
63 ms |
14920 KB |
Output is correct |
46 |
Correct |
88 ms |
14920 KB |
Output is correct |
47 |
Correct |
77 ms |
14796 KB |
Output is correct |
48 |
Correct |
55 ms |
15052 KB |
Output is correct |
49 |
Correct |
54 ms |
14920 KB |
Output is correct |
50 |
Correct |
52 ms |
14920 KB |
Output is correct |
51 |
Correct |
62 ms |
15048 KB |
Output is correct |
52 |
Correct |
91 ms |
14920 KB |
Output is correct |
53 |
Correct |
59 ms |
14800 KB |
Output is correct |
54 |
Correct |
109 ms |
14816 KB |
Output is correct |
55 |
Correct |
84 ms |
14920 KB |
Output is correct |
56 |
Correct |
49 ms |
14920 KB |
Output is correct |
57 |
Correct |
60 ms |
15056 KB |
Output is correct |
58 |
Correct |
60 ms |
14800 KB |
Output is correct |
59 |
Correct |
69 ms |
14920 KB |
Output is correct |
60 |
Correct |
86 ms |
14840 KB |
Output is correct |
61 |
Correct |
60 ms |
14812 KB |
Output is correct |
62 |
Correct |
93 ms |
14920 KB |
Output is correct |
63 |
Correct |
81 ms |
14920 KB |
Output is correct |
64 |
Correct |
52 ms |
14928 KB |
Output is correct |
65 |
Correct |
76 ms |
14920 KB |
Output is correct |
66 |
Correct |
75 ms |
14972 KB |
Output is correct |
67 |
Correct |
70 ms |
14920 KB |
Output is correct |
68 |
Correct |
75 ms |
14920 KB |
Output is correct |
69 |
Correct |
73 ms |
14920 KB |
Output is correct |
70 |
Correct |
76 ms |
14936 KB |
Output is correct |
71 |
Correct |
345 ms |
39268 KB |
Output is correct |
72 |
Correct |
312 ms |
39312 KB |
Output is correct |
73 |
Correct |
487 ms |
37832 KB |
Output is correct |
74 |
Correct |
1166 ms |
38216 KB |
Output is correct |
75 |
Correct |
417 ms |
40136 KB |
Output is correct |
76 |
Correct |
1022 ms |
38608 KB |
Output is correct |
77 |
Correct |
798 ms |
38472 KB |
Output is correct |
78 |
Correct |
199 ms |
42320 KB |
Output is correct |
79 |
Correct |
264 ms |
36168 KB |
Output is correct |
80 |
Correct |
303 ms |
39264 KB |
Output is correct |
81 |
Correct |
521 ms |
39368 KB |
Output is correct |
82 |
Correct |
1166 ms |
38344 KB |
Output is correct |
83 |
Correct |
332 ms |
37304 KB |
Output is correct |
84 |
Correct |
917 ms |
38200 KB |
Output is correct |
85 |
Correct |
722 ms |
38472 KB |
Output is correct |
86 |
Correct |
194 ms |
36168 KB |
Output is correct |
87 |
Correct |
292 ms |
39252 KB |
Output is correct |
88 |
Correct |
276 ms |
36300 KB |
Output is correct |
89 |
Correct |
496 ms |
37960 KB |
Output is correct |
90 |
Correct |
814 ms |
38344 KB |
Output is correct |
91 |
Correct |
345 ms |
37368 KB |
Output is correct |
92 |
Correct |
908 ms |
38472 KB |
Output is correct |
93 |
Correct |
747 ms |
38532 KB |
Output is correct |
94 |
Correct |
202 ms |
36168 KB |
Output is correct |
95 |
Correct |
584 ms |
38224 KB |
Output is correct |
96 |
Correct |
588 ms |
38436 KB |
Output is correct |
97 |
Correct |
646 ms |
38396 KB |
Output is correct |
98 |
Correct |
620 ms |
38360 KB |
Output is correct |
99 |
Correct |
618 ms |
38216 KB |
Output is correct |
100 |
Correct |
577 ms |
38216 KB |
Output is correct |