#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int H = 7;
char S[(1 << 20)+1],T[21];
struct query { int m1,m2,i; };
query queries[1000000];
int pow3[21];
int ans[1000000];
int p[1594323],sum[1594323];
int main() {
int i,j;
int L,Q;
scanf("%d %d %s",&L,&Q,S);
H = min(H,L);
pow3[0] = 1;
for (i = 1; i <= L; i++) pow3[i] = pow3[i-1]*3;
for (i = 0; i < Q; i++) {
scanf("%s",T),queries[i].i = i;
reverse(T,T+H),reverse(T+H,T+L);
for (j = 0; j < H; j++) {
if (T[j] == '0') queries[i].m1 |= (1 << (2*j));
else if (T[j] == '1') queries[i].m1 |= (1 << (2*j+1));
else queries[i].m1 |= (1 << (2*j)) | (1 << (2*j+1));
}
for (j = H; j < L; j++) {
if (T[j] == '1') queries[i].m2 += pow3[j-H];
else if (T[j] == '?') queries[i].m2 += 2*pow3[j-H];
}
}
int k;
for (i = 0; i < pow3[L-H]; i++) {
int n = i;
p[i] = -1;
for (j = 0; j < L-H; j++) {
if ((n % 3) == 2) p[i] = j;
n /= 3;
}
}
for (i = 0; i < (1 << H); i++) {
for (j = 0; j < (1 << (L-H)); j++) {
int c = 0;
for (k = 0; k < L-H; k++) {
if (j & (1 << k)) c += pow3[k];
}
sum[c] = S[j+i*(1 << (L-H))]-'0';
}
for (j = 0; j < pow3[L-H]; j++) {
if (p[j] != -1) sum[j] = sum[j-pow3[p[j]]]+sum[j-2*pow3[p[j]]];
}
int m = 0;
for (j = 0; j < H; j++) {
if (i & (1 << j)) m |= (1 << (2*j+1));
else m |= (1 << (2*j));
}
for (j = 0; j < Q; j++) {
if ((queries[j].m1 & m) == m) ans[queries[j].i] += sum[queries[j].m2];
}
}
for (i = 0; i < Q; i++) printf("%d\n",ans[i]);
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %s",&L,&Q,S);
~~~~~^~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:73:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",T),queries[i].i = i;
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
424 KB |
Output is correct |
11 |
Correct |
600 ms |
30804 KB |
Output is correct |
12 |
Correct |
688 ms |
30456 KB |
Output is correct |
13 |
Correct |
554 ms |
29668 KB |
Output is correct |
14 |
Correct |
501 ms |
29788 KB |
Output is correct |
15 |
Correct |
706 ms |
30712 KB |
Output is correct |
16 |
Correct |
544 ms |
29948 KB |
Output is correct |
17 |
Correct |
549 ms |
30072 KB |
Output is correct |
18 |
Correct |
637 ms |
31864 KB |
Output is correct |
19 |
Correct |
424 ms |
28752 KB |
Output is correct |
20 |
Correct |
635 ms |
30360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
424 KB |
Output is correct |
11 |
Correct |
600 ms |
30804 KB |
Output is correct |
12 |
Correct |
688 ms |
30456 KB |
Output is correct |
13 |
Correct |
554 ms |
29668 KB |
Output is correct |
14 |
Correct |
501 ms |
29788 KB |
Output is correct |
15 |
Correct |
706 ms |
30712 KB |
Output is correct |
16 |
Correct |
544 ms |
29948 KB |
Output is correct |
17 |
Correct |
549 ms |
30072 KB |
Output is correct |
18 |
Correct |
637 ms |
31864 KB |
Output is correct |
19 |
Correct |
424 ms |
28752 KB |
Output is correct |
20 |
Correct |
635 ms |
30360 KB |
Output is correct |
21 |
Correct |
665 ms |
33708 KB |
Output is correct |
22 |
Correct |
711 ms |
33948 KB |
Output is correct |
23 |
Correct |
631 ms |
32892 KB |
Output is correct |
24 |
Correct |
555 ms |
32760 KB |
Output is correct |
25 |
Correct |
767 ms |
34656 KB |
Output is correct |
26 |
Correct |
588 ms |
33212 KB |
Output is correct |
27 |
Correct |
614 ms |
33264 KB |
Output is correct |
28 |
Correct |
738 ms |
35648 KB |
Output is correct |
29 |
Correct |
510 ms |
31652 KB |
Output is correct |
30 |
Correct |
759 ms |
33832 KB |
Output is correct |
31 |
Correct |
660 ms |
33704 KB |
Output is correct |
32 |
Correct |
765 ms |
33816 KB |
Output is correct |
33 |
Correct |
538 ms |
32700 KB |
Output is correct |
34 |
Correct |
656 ms |
32684 KB |
Output is correct |
35 |
Correct |
635 ms |
33184 KB |
Output is correct |
36 |
Correct |
430 ms |
31812 KB |
Output is correct |
37 |
Correct |
623 ms |
33704 KB |
Output is correct |
38 |
Correct |
529 ms |
31656 KB |
Output is correct |
39 |
Correct |
582 ms |
32928 KB |
Output is correct |
40 |
Correct |
581 ms |
32812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
424 KB |
Output is correct |
11 |
Correct |
586 ms |
16968 KB |
Output is correct |
12 |
Correct |
556 ms |
17016 KB |
Output is correct |
13 |
Correct |
551 ms |
16888 KB |
Output is correct |
14 |
Correct |
554 ms |
16952 KB |
Output is correct |
15 |
Correct |
565 ms |
17016 KB |
Output is correct |
16 |
Correct |
557 ms |
16888 KB |
Output is correct |
17 |
Correct |
556 ms |
16916 KB |
Output is correct |
18 |
Correct |
556 ms |
17144 KB |
Output is correct |
19 |
Correct |
553 ms |
16748 KB |
Output is correct |
20 |
Correct |
556 ms |
17004 KB |
Output is correct |
21 |
Correct |
572 ms |
17032 KB |
Output is correct |
22 |
Correct |
577 ms |
16880 KB |
Output is correct |
23 |
Correct |
552 ms |
16776 KB |
Output is correct |
24 |
Correct |
554 ms |
16932 KB |
Output is correct |
25 |
Correct |
566 ms |
16840 KB |
Output is correct |
26 |
Correct |
544 ms |
16768 KB |
Output is correct |
27 |
Correct |
576 ms |
17012 KB |
Output is correct |
28 |
Correct |
546 ms |
16752 KB |
Output is correct |
29 |
Correct |
558 ms |
16880 KB |
Output is correct |
30 |
Correct |
553 ms |
16848 KB |
Output is correct |
31 |
Correct |
549 ms |
16908 KB |
Output is correct |
32 |
Correct |
583 ms |
16972 KB |
Output is correct |
33 |
Correct |
560 ms |
16848 KB |
Output is correct |
34 |
Correct |
542 ms |
16724 KB |
Output is correct |
35 |
Correct |
579 ms |
16880 KB |
Output is correct |
36 |
Correct |
587 ms |
16852 KB |
Output is correct |
37 |
Correct |
558 ms |
16876 KB |
Output is correct |
38 |
Correct |
571 ms |
16880 KB |
Output is correct |
39 |
Correct |
569 ms |
16880 KB |
Output is correct |
40 |
Correct |
559 ms |
16884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
424 KB |
Output is correct |
11 |
Correct |
600 ms |
30804 KB |
Output is correct |
12 |
Correct |
688 ms |
30456 KB |
Output is correct |
13 |
Correct |
554 ms |
29668 KB |
Output is correct |
14 |
Correct |
501 ms |
29788 KB |
Output is correct |
15 |
Correct |
706 ms |
30712 KB |
Output is correct |
16 |
Correct |
544 ms |
29948 KB |
Output is correct |
17 |
Correct |
549 ms |
30072 KB |
Output is correct |
18 |
Correct |
637 ms |
31864 KB |
Output is correct |
19 |
Correct |
424 ms |
28752 KB |
Output is correct |
20 |
Correct |
635 ms |
30360 KB |
Output is correct |
21 |
Correct |
665 ms |
33708 KB |
Output is correct |
22 |
Correct |
711 ms |
33948 KB |
Output is correct |
23 |
Correct |
631 ms |
32892 KB |
Output is correct |
24 |
Correct |
555 ms |
32760 KB |
Output is correct |
25 |
Correct |
767 ms |
34656 KB |
Output is correct |
26 |
Correct |
588 ms |
33212 KB |
Output is correct |
27 |
Correct |
614 ms |
33264 KB |
Output is correct |
28 |
Correct |
738 ms |
35648 KB |
Output is correct |
29 |
Correct |
510 ms |
31652 KB |
Output is correct |
30 |
Correct |
759 ms |
33832 KB |
Output is correct |
31 |
Correct |
660 ms |
33704 KB |
Output is correct |
32 |
Correct |
765 ms |
33816 KB |
Output is correct |
33 |
Correct |
538 ms |
32700 KB |
Output is correct |
34 |
Correct |
656 ms |
32684 KB |
Output is correct |
35 |
Correct |
635 ms |
33184 KB |
Output is correct |
36 |
Correct |
430 ms |
31812 KB |
Output is correct |
37 |
Correct |
623 ms |
33704 KB |
Output is correct |
38 |
Correct |
529 ms |
31656 KB |
Output is correct |
39 |
Correct |
582 ms |
32928 KB |
Output is correct |
40 |
Correct |
581 ms |
32812 KB |
Output is correct |
41 |
Correct |
586 ms |
16968 KB |
Output is correct |
42 |
Correct |
556 ms |
17016 KB |
Output is correct |
43 |
Correct |
551 ms |
16888 KB |
Output is correct |
44 |
Correct |
554 ms |
16952 KB |
Output is correct |
45 |
Correct |
565 ms |
17016 KB |
Output is correct |
46 |
Correct |
557 ms |
16888 KB |
Output is correct |
47 |
Correct |
556 ms |
16916 KB |
Output is correct |
48 |
Correct |
556 ms |
17144 KB |
Output is correct |
49 |
Correct |
553 ms |
16748 KB |
Output is correct |
50 |
Correct |
556 ms |
17004 KB |
Output is correct |
51 |
Correct |
572 ms |
17032 KB |
Output is correct |
52 |
Correct |
577 ms |
16880 KB |
Output is correct |
53 |
Correct |
552 ms |
16776 KB |
Output is correct |
54 |
Correct |
554 ms |
16932 KB |
Output is correct |
55 |
Correct |
566 ms |
16840 KB |
Output is correct |
56 |
Correct |
544 ms |
16768 KB |
Output is correct |
57 |
Correct |
576 ms |
17012 KB |
Output is correct |
58 |
Correct |
546 ms |
16752 KB |
Output is correct |
59 |
Correct |
558 ms |
16880 KB |
Output is correct |
60 |
Correct |
553 ms |
16848 KB |
Output is correct |
61 |
Correct |
549 ms |
16908 KB |
Output is correct |
62 |
Correct |
583 ms |
16972 KB |
Output is correct |
63 |
Correct |
560 ms |
16848 KB |
Output is correct |
64 |
Correct |
542 ms |
16724 KB |
Output is correct |
65 |
Correct |
579 ms |
16880 KB |
Output is correct |
66 |
Correct |
587 ms |
16852 KB |
Output is correct |
67 |
Correct |
558 ms |
16876 KB |
Output is correct |
68 |
Correct |
571 ms |
16880 KB |
Output is correct |
69 |
Correct |
569 ms |
16880 KB |
Output is correct |
70 |
Correct |
559 ms |
16884 KB |
Output is correct |
71 |
Correct |
1348 ms |
35456 KB |
Output is correct |
72 |
Correct |
1433 ms |
36472 KB |
Output is correct |
73 |
Correct |
1317 ms |
34856 KB |
Output is correct |
74 |
Correct |
1455 ms |
35228 KB |
Output is correct |
75 |
Correct |
1599 ms |
37216 KB |
Output is correct |
76 |
Correct |
1439 ms |
35672 KB |
Output is correct |
77 |
Correct |
1450 ms |
35344 KB |
Output is correct |
78 |
Correct |
1298 ms |
39268 KB |
Output is correct |
79 |
Correct |
1175 ms |
33060 KB |
Output is correct |
80 |
Correct |
1583 ms |
36244 KB |
Output is correct |
81 |
Correct |
1486 ms |
36140 KB |
Output is correct |
82 |
Correct |
1496 ms |
35172 KB |
Output is correct |
83 |
Correct |
1215 ms |
34432 KB |
Output is correct |
84 |
Correct |
1388 ms |
35136 KB |
Output is correct |
85 |
Correct |
1519 ms |
35312 KB |
Output is correct |
86 |
Correct |
983 ms |
33068 KB |
Output is correct |
87 |
Correct |
1359 ms |
36084 KB |
Output is correct |
88 |
Correct |
1151 ms |
32996 KB |
Output is correct |
89 |
Correct |
1317 ms |
34828 KB |
Output is correct |
90 |
Correct |
1387 ms |
35084 KB |
Output is correct |
91 |
Correct |
1189 ms |
34276 KB |
Output is correct |
92 |
Correct |
1420 ms |
35416 KB |
Output is correct |
93 |
Correct |
1411 ms |
35220 KB |
Output is correct |
94 |
Correct |
984 ms |
33032 KB |
Output is correct |
95 |
Correct |
1445 ms |
35032 KB |
Output is correct |
96 |
Correct |
1432 ms |
35064 KB |
Output is correct |
97 |
Correct |
1424 ms |
34936 KB |
Output is correct |
98 |
Correct |
1416 ms |
35088 KB |
Output is correct |
99 |
Correct |
1423 ms |
35056 KB |
Output is correct |
100 |
Correct |
1495 ms |
35084 KB |
Output is correct |