#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL);
#define PI acos(-1.0)
#define eps 1e-9
#define FF first
#define SS second
// VECTOR (6)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define uniq(v) sort(all( (v) )), (v).resize( unique(all( (v) )) - (v).begin() );
// BIT (6)
#define CNTBIT(x) __builtin_popcountll(x)
#define ODDBIT(x) __builtin_parityll(x)
#define MASK(i) (1LL<<(i))
#define BIT(x, i) (((x)>>(i))&1)
#define SUBSET(big, small) (((big)&(small))==(small))
#define FIRSTBIT(x) __builtin_ctzll(x)
//typedef
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> ii;
/* */
template<class T>
bool minimize(T &a, const T &b) {
if(a > b) {a = b; return 1;}
return 0;
}
template<class T>
bool maximize(T &a, const T &b) {
if(a < b) {a = b; return 1;}
return 0;
}
/* */
/* CODE BELOW */
const int N = 1e9 + 7, M = 20;
const int oo = 1e9 + 7;
const int MOD = 1e9 + 7;
int l, q;
int sumPar[MASK(M)]; // sum of toxicity of parent mask
int sumSub[MASK(M)]; // sum of toxicity of submask
int a[MASK(M)];
signed main() {
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
//fastIO;
scanf("%d%d\n", &l, &q);
char ch[MASK(l)]; scanf("%s\n", &ch);
for(int i=0;i<MASK(l);i++) {
a[i] = ch[i]-'0';
sumSub[i] = a[i];
sumPar[i] = a[i];
}
for(int j=0;j<l;j++) for(int i=0;i<MASK(l);i++) {
if(BIT(i, j)) sumSub[i]+= sumSub[i^MASK(j)];
}
for(int j=0;j<l;j++) for(int i=0;i<MASK(l);i++) {
if(!BIT(i, j)) sumPar[i]+= sumPar[i|MASK(j)];
}
for(int i=1;i<=q;i++) {
scanf("%s\n", &ch);
// 0, 1, ?
int cnt[3]{0, 0, 0}, msk[3]{0, 0, 0};
for(int j=0;j<l;j++) {
if(ch[j]=='0') cnt[0]++, msk[0]|=MASK(l-j-1);
if(ch[j]=='1') cnt[1]++, msk[1]|=MASK(l-j-1);
if(ch[j]=='?') cnt[2]++, msk[2]|=MASK(l-j-1);
}
int mn = *min_element(cnt, cnt+3), ans = 0;
if(cnt[0] == mn) { // 0
for(int i=msk[0];i>=0;i=(i-1)&msk[0]) {
ans+= (ODDBIT(i)?-1:1)*sumPar[i|msk[1]];
if(i == 0) break;
}
} else if(cnt[1] == mn) {
int oddbase = ODDBIT(msk[1]);
for(int i=msk[1];i>=0;i=(i-1)&msk[1]) {
ans+= (oddbase==ODDBIT(i)?1:-1)*sumSub[i|msk[2]];
if(i == 0) break;
}
} else if(cnt[2] == mn) {
for(int i=msk[2];i>=0;i=(i-1)&msk[2]) {
ans+= a[i|msk[1]];
if(i == 0) break;
}
}
printf("%d\n", ans);
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:57:31: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[(1 << l)]' [-Wformat=]
57 | char ch[MASK(l)]; scanf("%s\n", &ch);
| ~^ ~~~
| | |
| char* char (*)[(1 << l)]
snake_escaping.cpp:72:17: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[(1 << l)]' [-Wformat=]
72 | scanf("%s\n", &ch);
| ~^ ~~~
| | |
| char* char (*)[(1 << l)]
snake_escaping.cpp:56:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d%d\n", &l, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~
snake_escaping.cpp:57:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | char ch[MASK(l)]; scanf("%s\n", &ch);
| ~~~~~^~~~~~~~~~~~~
snake_escaping.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%s\n", &ch);
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
243 ms |
15032 KB |
Output is correct |
12 |
Correct |
257 ms |
14768 KB |
Output is correct |
13 |
Correct |
275 ms |
13868 KB |
Output is correct |
14 |
Correct |
280 ms |
14040 KB |
Output is correct |
15 |
Correct |
260 ms |
15032 KB |
Output is correct |
16 |
Correct |
283 ms |
14156 KB |
Output is correct |
17 |
Correct |
300 ms |
14092 KB |
Output is correct |
18 |
Correct |
207 ms |
16076 KB |
Output is correct |
19 |
Correct |
243 ms |
12972 KB |
Output is correct |
20 |
Correct |
248 ms |
14660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
243 ms |
15032 KB |
Output is correct |
12 |
Correct |
257 ms |
14768 KB |
Output is correct |
13 |
Correct |
275 ms |
13868 KB |
Output is correct |
14 |
Correct |
280 ms |
14040 KB |
Output is correct |
15 |
Correct |
260 ms |
15032 KB |
Output is correct |
16 |
Correct |
283 ms |
14156 KB |
Output is correct |
17 |
Correct |
300 ms |
14092 KB |
Output is correct |
18 |
Correct |
207 ms |
16076 KB |
Output is correct |
19 |
Correct |
243 ms |
12972 KB |
Output is correct |
20 |
Correct |
248 ms |
14660 KB |
Output is correct |
21 |
Correct |
273 ms |
18024 KB |
Output is correct |
22 |
Correct |
280 ms |
18208 KB |
Output is correct |
23 |
Correct |
317 ms |
17308 KB |
Output is correct |
24 |
Correct |
348 ms |
17040 KB |
Output is correct |
25 |
Correct |
280 ms |
19080 KB |
Output is correct |
26 |
Correct |
334 ms |
17596 KB |
Output is correct |
27 |
Correct |
332 ms |
17460 KB |
Output is correct |
28 |
Correct |
225 ms |
20048 KB |
Output is correct |
29 |
Correct |
263 ms |
15972 KB |
Output is correct |
30 |
Correct |
277 ms |
18204 KB |
Output is correct |
31 |
Correct |
318 ms |
18028 KB |
Output is correct |
32 |
Correct |
348 ms |
18080 KB |
Output is correct |
33 |
Correct |
282 ms |
16948 KB |
Output is correct |
34 |
Correct |
338 ms |
17120 KB |
Output is correct |
35 |
Correct |
331 ms |
17536 KB |
Output is correct |
36 |
Correct |
235 ms |
15976 KB |
Output is correct |
37 |
Correct |
262 ms |
18084 KB |
Output is correct |
38 |
Correct |
276 ms |
16076 KB |
Output is correct |
39 |
Correct |
308 ms |
17244 KB |
Output is correct |
40 |
Correct |
342 ms |
17064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
67 ms |
15880 KB |
Output is correct |
12 |
Correct |
61 ms |
15880 KB |
Output is correct |
13 |
Correct |
68 ms |
15792 KB |
Output is correct |
14 |
Correct |
75 ms |
15764 KB |
Output is correct |
15 |
Correct |
62 ms |
15880 KB |
Output is correct |
16 |
Correct |
76 ms |
15844 KB |
Output is correct |
17 |
Correct |
81 ms |
15760 KB |
Output is correct |
18 |
Correct |
58 ms |
16004 KB |
Output is correct |
19 |
Correct |
62 ms |
15640 KB |
Output is correct |
20 |
Correct |
64 ms |
15956 KB |
Output is correct |
21 |
Correct |
68 ms |
15876 KB |
Output is correct |
22 |
Correct |
75 ms |
15760 KB |
Output is correct |
23 |
Correct |
62 ms |
15812 KB |
Output is correct |
24 |
Correct |
86 ms |
15764 KB |
Output is correct |
25 |
Correct |
77 ms |
15748 KB |
Output is correct |
26 |
Correct |
56 ms |
15684 KB |
Output is correct |
27 |
Correct |
68 ms |
15996 KB |
Output is correct |
28 |
Correct |
67 ms |
15636 KB |
Output is correct |
29 |
Correct |
67 ms |
15792 KB |
Output is correct |
30 |
Correct |
67 ms |
15792 KB |
Output is correct |
31 |
Correct |
66 ms |
15764 KB |
Output is correct |
32 |
Correct |
91 ms |
15812 KB |
Output is correct |
33 |
Correct |
77 ms |
15788 KB |
Output is correct |
34 |
Correct |
56 ms |
15660 KB |
Output is correct |
35 |
Correct |
78 ms |
15832 KB |
Output is correct |
36 |
Correct |
73 ms |
15788 KB |
Output is correct |
37 |
Correct |
73 ms |
15768 KB |
Output is correct |
38 |
Correct |
79 ms |
15744 KB |
Output is correct |
39 |
Correct |
75 ms |
15756 KB |
Output is correct |
40 |
Correct |
73 ms |
15812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
243 ms |
15032 KB |
Output is correct |
12 |
Correct |
257 ms |
14768 KB |
Output is correct |
13 |
Correct |
275 ms |
13868 KB |
Output is correct |
14 |
Correct |
280 ms |
14040 KB |
Output is correct |
15 |
Correct |
260 ms |
15032 KB |
Output is correct |
16 |
Correct |
283 ms |
14156 KB |
Output is correct |
17 |
Correct |
300 ms |
14092 KB |
Output is correct |
18 |
Correct |
207 ms |
16076 KB |
Output is correct |
19 |
Correct |
243 ms |
12972 KB |
Output is correct |
20 |
Correct |
248 ms |
14660 KB |
Output is correct |
21 |
Correct |
273 ms |
18024 KB |
Output is correct |
22 |
Correct |
280 ms |
18208 KB |
Output is correct |
23 |
Correct |
317 ms |
17308 KB |
Output is correct |
24 |
Correct |
348 ms |
17040 KB |
Output is correct |
25 |
Correct |
280 ms |
19080 KB |
Output is correct |
26 |
Correct |
334 ms |
17596 KB |
Output is correct |
27 |
Correct |
332 ms |
17460 KB |
Output is correct |
28 |
Correct |
225 ms |
20048 KB |
Output is correct |
29 |
Correct |
263 ms |
15972 KB |
Output is correct |
30 |
Correct |
277 ms |
18204 KB |
Output is correct |
31 |
Correct |
318 ms |
18028 KB |
Output is correct |
32 |
Correct |
348 ms |
18080 KB |
Output is correct |
33 |
Correct |
282 ms |
16948 KB |
Output is correct |
34 |
Correct |
338 ms |
17120 KB |
Output is correct |
35 |
Correct |
331 ms |
17536 KB |
Output is correct |
36 |
Correct |
235 ms |
15976 KB |
Output is correct |
37 |
Correct |
262 ms |
18084 KB |
Output is correct |
38 |
Correct |
276 ms |
16076 KB |
Output is correct |
39 |
Correct |
308 ms |
17244 KB |
Output is correct |
40 |
Correct |
342 ms |
17064 KB |
Output is correct |
41 |
Correct |
67 ms |
15880 KB |
Output is correct |
42 |
Correct |
61 ms |
15880 KB |
Output is correct |
43 |
Correct |
68 ms |
15792 KB |
Output is correct |
44 |
Correct |
75 ms |
15764 KB |
Output is correct |
45 |
Correct |
62 ms |
15880 KB |
Output is correct |
46 |
Correct |
76 ms |
15844 KB |
Output is correct |
47 |
Correct |
81 ms |
15760 KB |
Output is correct |
48 |
Correct |
58 ms |
16004 KB |
Output is correct |
49 |
Correct |
62 ms |
15640 KB |
Output is correct |
50 |
Correct |
64 ms |
15956 KB |
Output is correct |
51 |
Correct |
68 ms |
15876 KB |
Output is correct |
52 |
Correct |
75 ms |
15760 KB |
Output is correct |
53 |
Correct |
62 ms |
15812 KB |
Output is correct |
54 |
Correct |
86 ms |
15764 KB |
Output is correct |
55 |
Correct |
77 ms |
15748 KB |
Output is correct |
56 |
Correct |
56 ms |
15684 KB |
Output is correct |
57 |
Correct |
68 ms |
15996 KB |
Output is correct |
58 |
Correct |
67 ms |
15636 KB |
Output is correct |
59 |
Correct |
67 ms |
15792 KB |
Output is correct |
60 |
Correct |
67 ms |
15792 KB |
Output is correct |
61 |
Correct |
66 ms |
15764 KB |
Output is correct |
62 |
Correct |
91 ms |
15812 KB |
Output is correct |
63 |
Correct |
77 ms |
15788 KB |
Output is correct |
64 |
Correct |
56 ms |
15660 KB |
Output is correct |
65 |
Correct |
78 ms |
15832 KB |
Output is correct |
66 |
Correct |
73 ms |
15788 KB |
Output is correct |
67 |
Correct |
73 ms |
15768 KB |
Output is correct |
68 |
Correct |
79 ms |
15744 KB |
Output is correct |
69 |
Correct |
75 ms |
15756 KB |
Output is correct |
70 |
Correct |
73 ms |
15812 KB |
Output is correct |
71 |
Correct |
403 ms |
21904 KB |
Output is correct |
72 |
Correct |
408 ms |
21700 KB |
Output is correct |
73 |
Correct |
541 ms |
20164 KB |
Output is correct |
74 |
Correct |
631 ms |
20396 KB |
Output is correct |
75 |
Correct |
417 ms |
22492 KB |
Output is correct |
76 |
Correct |
750 ms |
20696 KB |
Output is correct |
77 |
Correct |
746 ms |
20760 KB |
Output is correct |
78 |
Correct |
310 ms |
24444 KB |
Output is correct |
79 |
Correct |
399 ms |
18532 KB |
Output is correct |
80 |
Correct |
427 ms |
21672 KB |
Output is correct |
81 |
Correct |
531 ms |
21528 KB |
Output is correct |
82 |
Correct |
635 ms |
20552 KB |
Output is correct |
83 |
Correct |
413 ms |
19836 KB |
Output is correct |
84 |
Correct |
835 ms |
20588 KB |
Output is correct |
85 |
Correct |
734 ms |
20664 KB |
Output is correct |
86 |
Correct |
317 ms |
18452 KB |
Output is correct |
87 |
Correct |
401 ms |
21564 KB |
Output is correct |
88 |
Correct |
423 ms |
18712 KB |
Output is correct |
89 |
Correct |
532 ms |
20100 KB |
Output is correct |
90 |
Correct |
523 ms |
20456 KB |
Output is correct |
91 |
Correct |
447 ms |
19608 KB |
Output is correct |
92 |
Correct |
840 ms |
20856 KB |
Output is correct |
93 |
Correct |
738 ms |
20580 KB |
Output is correct |
94 |
Correct |
296 ms |
18448 KB |
Output is correct |
95 |
Correct |
642 ms |
20656 KB |
Output is correct |
96 |
Correct |
666 ms |
20392 KB |
Output is correct |
97 |
Correct |
622 ms |
20448 KB |
Output is correct |
98 |
Correct |
657 ms |
20340 KB |
Output is correct |
99 |
Correct |
639 ms |
20400 KB |
Output is correct |
100 |
Correct |
638 ms |
20292 KB |
Output is correct |