Submission #41156

# Submission time Handle Problem Language Result Execution time Memory
41156 2018-02-13T06:58:58 Z aome Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 34936 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1000005;
const int M = 1594323;

int n, q;
int to3[1 << 13];
int mask2[2][N]; // 0 check and, 1 check or
int mask3[N];
int res[N];
int pw3[12], f[M], g[M];
string a;

void prepare() {
	for (int i = 0; i < (1 << 13); ++i) {
		for (int j = 12; j >= 0; --j) {
			to3[i] *= 3;
			if (i >> j & 1) to3[i]++;
		}
	}
	pw3[0] = 1; 
	for (int i = 1; i < 13; ++i) pw3[i] = 3 * pw3[i - 1];
	for (int i = 0; i < M; ++i) {
		int cur = i;
		g[i] = -1;
		for (int j = 0; j < 13; ++j) {
			if (cur % 3 == 2) g[i] = j;
			cur /= 3;
		}
	}
}

void calc() {
	for (int i = 0; i < M; ++i) {
		if (g[i] != -1) {
			f[i] += f[i - 2 * pw3[g[i]]];
			f[i] += f[i - 1 * pw3[g[i]]];
		}
	}
}

inline bool check(int mask, int i) {
	return (mask & mask2[0][i]) == mask2[0][i] && (mask | mask2[1][i]) == mask2[1][i];
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> q >> a; 
	for (int i = 0; i < q; ++i) {
		string s; cin >> s;
		for (int j = max(0, n - 7); j < n; ++j) { // first 7 bits
			mask2[0][i] <<= 1, mask2[1][i] <<= 1;
			if (s[j] == '1') mask2[0][i]++;
			if (s[j] == '?' || s[j] == '1') mask2[1][i]++;
		}
		for (int j = 0; j < max(0, n - 7); ++j) { // last 13 bits
			mask3[i] *= 3;
			if (s[j] == '1') mask3[i]++;
			if (s[j] == '?') mask3[i] += 2;
		}
	}
	prepare();
	for (int i = 0; i < 128; ++i) { // first 7 bits
		memset(f, 0, sizeof f);
		for (int j = 0; j < (1 << max(0, n - 7)); ++j) {
			f[to3[j]] += a[j << 7 | i] - '0';
		}
		calc();
		for (int j = 0; j < q; ++j) {
			if (!check(i, j)) continue;
			res[j] += f[mask3[j]];
		}
	}
	for (int i = 0; i < q; ++i) printf("%d\n", res[i]);
}

Compilation message

snake_escaping.cpp: In function 'void prepare()':
snake_escaping.cpp:24:54: warning: iteration 11u invokes undefined behavior [-Waggressive-loop-optimizations]
  for (int i = 1; i < 13; ++i) pw3[i] = 3 * pw3[i - 1];
                                                      ^
snake_escaping.cpp:24:20: note: containing loop
  for (int i = 1; i < 13; ++i) pw3[i] = 3 * pw3[i - 1];
                    ^
# Verdict Execution time Memory Grader output
1 Correct 614 ms 12992 KB Output is correct
2 Correct 606 ms 12992 KB Output is correct
3 Correct 633 ms 12992 KB Output is correct
4 Correct 618 ms 13188 KB Output is correct
5 Correct 648 ms 13188 KB Output is correct
6 Correct 632 ms 13188 KB Output is correct
7 Correct 621 ms 13188 KB Output is correct
8 Correct 613 ms 13188 KB Output is correct
9 Correct 635 ms 13188 KB Output is correct
10 Correct 610 ms 13208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 614 ms 12992 KB Output is correct
2 Correct 606 ms 12992 KB Output is correct
3 Correct 633 ms 12992 KB Output is correct
4 Correct 618 ms 13188 KB Output is correct
5 Correct 648 ms 13188 KB Output is correct
6 Correct 632 ms 13188 KB Output is correct
7 Correct 621 ms 13188 KB Output is correct
8 Correct 613 ms 13188 KB Output is correct
9 Correct 635 ms 13188 KB Output is correct
10 Correct 610 ms 13208 KB Output is correct
11 Correct 1240 ms 32696 KB Output is correct
12 Correct 1355 ms 32696 KB Output is correct
13 Correct 1668 ms 32696 KB Output is correct
14 Correct 1549 ms 32696 KB Output is correct
15 Correct 1916 ms 32696 KB Output is correct
16 Correct 1546 ms 32696 KB Output is correct
17 Correct 1612 ms 32696 KB Output is correct
18 Correct 1062 ms 33708 KB Output is correct
19 Correct 1215 ms 33708 KB Output is correct
20 Correct 1359 ms 33708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 614 ms 12992 KB Output is correct
2 Correct 606 ms 12992 KB Output is correct
3 Correct 633 ms 12992 KB Output is correct
4 Correct 618 ms 13188 KB Output is correct
5 Correct 648 ms 13188 KB Output is correct
6 Correct 632 ms 13188 KB Output is correct
7 Correct 621 ms 13188 KB Output is correct
8 Correct 613 ms 13188 KB Output is correct
9 Correct 635 ms 13188 KB Output is correct
10 Correct 610 ms 13208 KB Output is correct
11 Correct 1240 ms 32696 KB Output is correct
12 Correct 1355 ms 32696 KB Output is correct
13 Correct 1668 ms 32696 KB Output is correct
14 Correct 1549 ms 32696 KB Output is correct
15 Correct 1916 ms 32696 KB Output is correct
16 Correct 1546 ms 32696 KB Output is correct
17 Correct 1612 ms 32696 KB Output is correct
18 Correct 1062 ms 33708 KB Output is correct
19 Correct 1215 ms 33708 KB Output is correct
20 Correct 1359 ms 33708 KB Output is correct
21 Correct 1290 ms 33708 KB Output is correct
22 Correct 1347 ms 33708 KB Output is correct
23 Correct 1712 ms 33708 KB Output is correct
24 Correct 1655 ms 33708 KB Output is correct
25 Correct 1946 ms 33764 KB Output is correct
26 Correct 1632 ms 33764 KB Output is correct
27 Correct 1658 ms 33764 KB Output is correct
28 Correct 1073 ms 34844 KB Output is correct
29 Correct 1332 ms 34844 KB Output is correct
30 Correct 1307 ms 34844 KB Output is correct
31 Correct 1774 ms 34844 KB Output is correct
32 Correct 1606 ms 34844 KB Output is correct
33 Correct 1248 ms 34844 KB Output is correct
34 Correct 1613 ms 34844 KB Output is correct
35 Correct 1710 ms 34844 KB Output is correct
36 Correct 933 ms 34844 KB Output is correct
37 Correct 1294 ms 34844 KB Output is correct
38 Correct 1353 ms 34844 KB Output is correct
39 Correct 1449 ms 34844 KB Output is correct
40 Correct 1557 ms 34844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 614 ms 12992 KB Output is correct
2 Correct 606 ms 12992 KB Output is correct
3 Correct 633 ms 12992 KB Output is correct
4 Correct 618 ms 13188 KB Output is correct
5 Correct 648 ms 13188 KB Output is correct
6 Correct 632 ms 13188 KB Output is correct
7 Correct 621 ms 13188 KB Output is correct
8 Correct 613 ms 13188 KB Output is correct
9 Correct 635 ms 13188 KB Output is correct
10 Correct 610 ms 13208 KB Output is correct
11 Correct 687 ms 34844 KB Output is correct
12 Correct 688 ms 34844 KB Output is correct
13 Correct 694 ms 34844 KB Output is correct
14 Correct 739 ms 34844 KB Output is correct
15 Correct 761 ms 34844 KB Output is correct
16 Correct 741 ms 34844 KB Output is correct
17 Correct 724 ms 34844 KB Output is correct
18 Correct 711 ms 34844 KB Output is correct
19 Correct 656 ms 34844 KB Output is correct
20 Correct 679 ms 34844 KB Output is correct
21 Correct 706 ms 34844 KB Output is correct
22 Correct 707 ms 34844 KB Output is correct
23 Correct 662 ms 34844 KB Output is correct
24 Correct 754 ms 34844 KB Output is correct
25 Correct 789 ms 34844 KB Output is correct
26 Correct 680 ms 34844 KB Output is correct
27 Correct 678 ms 34844 KB Output is correct
28 Correct 675 ms 34844 KB Output is correct
29 Correct 740 ms 34844 KB Output is correct
30 Correct 684 ms 34844 KB Output is correct
31 Correct 724 ms 34844 KB Output is correct
32 Correct 687 ms 34844 KB Output is correct
33 Correct 727 ms 34844 KB Output is correct
34 Correct 714 ms 34844 KB Output is correct
35 Correct 686 ms 34844 KB Output is correct
36 Correct 676 ms 34844 KB Output is correct
37 Correct 668 ms 34844 KB Output is correct
38 Correct 693 ms 34844 KB Output is correct
39 Correct 714 ms 34844 KB Output is correct
40 Correct 676 ms 34844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 614 ms 12992 KB Output is correct
2 Correct 606 ms 12992 KB Output is correct
3 Correct 633 ms 12992 KB Output is correct
4 Correct 618 ms 13188 KB Output is correct
5 Correct 648 ms 13188 KB Output is correct
6 Correct 632 ms 13188 KB Output is correct
7 Correct 621 ms 13188 KB Output is correct
8 Correct 613 ms 13188 KB Output is correct
9 Correct 635 ms 13188 KB Output is correct
10 Correct 610 ms 13208 KB Output is correct
11 Correct 1240 ms 32696 KB Output is correct
12 Correct 1355 ms 32696 KB Output is correct
13 Correct 1668 ms 32696 KB Output is correct
14 Correct 1549 ms 32696 KB Output is correct
15 Correct 1916 ms 32696 KB Output is correct
16 Correct 1546 ms 32696 KB Output is correct
17 Correct 1612 ms 32696 KB Output is correct
18 Correct 1062 ms 33708 KB Output is correct
19 Correct 1215 ms 33708 KB Output is correct
20 Correct 1359 ms 33708 KB Output is correct
21 Correct 1290 ms 33708 KB Output is correct
22 Correct 1347 ms 33708 KB Output is correct
23 Correct 1712 ms 33708 KB Output is correct
24 Correct 1655 ms 33708 KB Output is correct
25 Correct 1946 ms 33764 KB Output is correct
26 Correct 1632 ms 33764 KB Output is correct
27 Correct 1658 ms 33764 KB Output is correct
28 Correct 1073 ms 34844 KB Output is correct
29 Correct 1332 ms 34844 KB Output is correct
30 Correct 1307 ms 34844 KB Output is correct
31 Correct 1774 ms 34844 KB Output is correct
32 Correct 1606 ms 34844 KB Output is correct
33 Correct 1248 ms 34844 KB Output is correct
34 Correct 1613 ms 34844 KB Output is correct
35 Correct 1710 ms 34844 KB Output is correct
36 Correct 933 ms 34844 KB Output is correct
37 Correct 1294 ms 34844 KB Output is correct
38 Correct 1353 ms 34844 KB Output is correct
39 Correct 1449 ms 34844 KB Output is correct
40 Correct 1557 ms 34844 KB Output is correct
41 Correct 687 ms 34844 KB Output is correct
42 Correct 688 ms 34844 KB Output is correct
43 Correct 694 ms 34844 KB Output is correct
44 Correct 739 ms 34844 KB Output is correct
45 Correct 761 ms 34844 KB Output is correct
46 Correct 741 ms 34844 KB Output is correct
47 Correct 724 ms 34844 KB Output is correct
48 Correct 711 ms 34844 KB Output is correct
49 Correct 656 ms 34844 KB Output is correct
50 Correct 679 ms 34844 KB Output is correct
51 Correct 706 ms 34844 KB Output is correct
52 Correct 707 ms 34844 KB Output is correct
53 Correct 662 ms 34844 KB Output is correct
54 Correct 754 ms 34844 KB Output is correct
55 Correct 789 ms 34844 KB Output is correct
56 Correct 680 ms 34844 KB Output is correct
57 Correct 678 ms 34844 KB Output is correct
58 Correct 675 ms 34844 KB Output is correct
59 Correct 740 ms 34844 KB Output is correct
60 Correct 684 ms 34844 KB Output is correct
61 Correct 724 ms 34844 KB Output is correct
62 Correct 687 ms 34844 KB Output is correct
63 Correct 727 ms 34844 KB Output is correct
64 Correct 714 ms 34844 KB Output is correct
65 Correct 686 ms 34844 KB Output is correct
66 Correct 676 ms 34844 KB Output is correct
67 Correct 668 ms 34844 KB Output is correct
68 Correct 693 ms 34844 KB Output is correct
69 Correct 714 ms 34844 KB Output is correct
70 Correct 676 ms 34844 KB Output is correct
71 Correct 1613 ms 34876 KB Output is correct
72 Correct 1891 ms 34936 KB Output is correct
73 Execution timed out 2028 ms 34936 KB Time limit exceeded
74 Halted 0 ms 0 KB -