Submission #1114184

# Submission time Handle Problem Language Result Execution time Memory
1114184 2024-11-18T09:49:06 Z lyis Snake Escaping (JOI18_snake_escaping) Java 11
100 / 100
934 ms 65536 KB
import java.io.*;
import java.util.*;

public class snake_escaping {
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) { e.printStackTrace(); }
			}
			return st.nextToken();
		}

		int nextInt() { return Integer.parseInt(next()); }
	}

	public static class Pair implements Comparable<Pair> {
		int vtx;
		int val;
		public Pair(int a, int b) {
			this.vtx = a;
			this.val = b;
		}
		public int compareTo(Pair other) {
			if (this.val < other.val) return -1;
			if (this.val > other.val) return 1;
			if (this.vtx < other.vtx) return -1;
			return 1;
		}
	}
	static int MOD = 998244353;

	public static void main(String[] args) {
		FastReader br = new FastReader();
		int L = br.nextInt();
		int Q = br.nextInt();
		final int[] S = new int[1 << L];
		final int[] sup = new int[1 << L];
		final int[] sub = new int[1 << L];
		final int[] btcnt = new int[1 << L];  // Precompute bitcount to speed up
		String s = br.next();
		for (int i = 0; i < (1 << L); i++) {
			S[i] = (int)s.charAt(i) - '0';
			sup[i] = sub[i] = S[i];
			btcnt[i] = Integer.bitCount(i);
		}
		// SOS DP!!
		for (int b = 0; b < L; b++) {
			for (int m = 0; m < (1 << L); m++) {
				if (((m >>> b) & 1) == 0) {
					sup[m] += sup[m ^ (1 << b)];
				} else {
					sub[m] += sub[m ^ (1 << b)];
				}
			}
		}

		StringBuilder sb =
		    new StringBuilder();  // I'm using stringbuilder to reduce the
		                          // number of times I print because calling
		                          // System.out.println 10^6 times is slow

		int A;
		int B;
		int C;
		int ca;
		int cb;
		int cc;
		long ans;
		for (int i = 0; i < Q; i++) {
			s = br.next();
			A = 0;
			B = 0;
			C = 0;
			ca = 0;
			cb = 0;
			cc = 0;
			ans = 0;
			for (int j = 0; j < L; j++) {
				if (s.charAt(j) == '0') {
					A |= (1 << (L - j - 1));
					ca++;
				} else if (s.charAt(j) == '1') {
					B |= (1 << (L - j - 1));
					cb++;
				} else {
					C |= (1 << (L - j - 1));
					cc++;
				}
			}

			if (ca <= cb && ca <= cc) {
				for (int m = A; m != 0; m = (m - 1) & A) {
					ans += (1 - 2 * ((btcnt[m]) & 1)) * sup[B | m];
				}
				ans += sup[B];
			} else if (cb <= ca && cb <= cc) {
				for (int m = B; m != 0; m = (m - 1) & B) {

					ans += (1 - 2 * ((btcnt[m]) & 1)) * sub[C | (B ^ m)];
				}
				ans += sub[C | B];

			} else {
				for (int m = C; m != 0; m = (m - 1) & C) { ans += S[m | B]; }

				ans += S[B];
			}
			sb.append(ans).append("\n");
			if ((i & ((1 << 17) - 1)) == 0) {  // sb can have a lot of memory
				System.out.println(sb.toString());
				sb = new StringBuilder();
			}
		}
		System.out.println(sb.toString());
	}
}
# Verdict Execution time Memory Grader output
1 Correct 84 ms 15892 KB Output is correct
2 Correct 82 ms 15760 KB Output is correct
3 Correct 84 ms 15460 KB Output is correct
4 Correct 90 ms 15600 KB Output is correct
5 Correct 84 ms 15832 KB Output is correct
6 Correct 83 ms 15296 KB Output is correct
7 Correct 82 ms 15484 KB Output is correct
8 Correct 80 ms 15476 KB Output is correct
9 Correct 79 ms 15296 KB Output is correct
10 Correct 83 ms 15612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 15892 KB Output is correct
2 Correct 82 ms 15760 KB Output is correct
3 Correct 84 ms 15460 KB Output is correct
4 Correct 90 ms 15600 KB Output is correct
5 Correct 84 ms 15832 KB Output is correct
6 Correct 83 ms 15296 KB Output is correct
7 Correct 82 ms 15484 KB Output is correct
8 Correct 80 ms 15476 KB Output is correct
9 Correct 79 ms 15296 KB Output is correct
10 Correct 83 ms 15612 KB Output is correct
11 Correct 377 ms 37408 KB Output is correct
12 Correct 383 ms 32016 KB Output is correct
13 Correct 415 ms 35484 KB Output is correct
14 Correct 418 ms 31512 KB Output is correct
15 Correct 407 ms 37332 KB Output is correct
16 Correct 427 ms 36684 KB Output is correct
17 Correct 438 ms 34292 KB Output is correct
18 Correct 334 ms 38524 KB Output is correct
19 Correct 365 ms 31012 KB Output is correct
20 Correct 377 ms 36852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 15892 KB Output is correct
2 Correct 82 ms 15760 KB Output is correct
3 Correct 84 ms 15460 KB Output is correct
4 Correct 90 ms 15600 KB Output is correct
5 Correct 84 ms 15832 KB Output is correct
6 Correct 83 ms 15296 KB Output is correct
7 Correct 82 ms 15484 KB Output is correct
8 Correct 80 ms 15476 KB Output is correct
9 Correct 79 ms 15296 KB Output is correct
10 Correct 83 ms 15612 KB Output is correct
11 Correct 377 ms 37408 KB Output is correct
12 Correct 383 ms 32016 KB Output is correct
13 Correct 415 ms 35484 KB Output is correct
14 Correct 418 ms 31512 KB Output is correct
15 Correct 407 ms 37332 KB Output is correct
16 Correct 427 ms 36684 KB Output is correct
17 Correct 438 ms 34292 KB Output is correct
18 Correct 334 ms 38524 KB Output is correct
19 Correct 365 ms 31012 KB Output is correct
20 Correct 377 ms 36852 KB Output is correct
21 Correct 446 ms 39776 KB Output is correct
22 Correct 471 ms 37952 KB Output is correct
23 Correct 486 ms 33924 KB Output is correct
24 Correct 453 ms 40028 KB Output is correct
25 Correct 481 ms 41528 KB Output is correct
26 Correct 493 ms 39824 KB Output is correct
27 Correct 501 ms 39660 KB Output is correct
28 Correct 392 ms 42412 KB Output is correct
29 Correct 404 ms 38224 KB Output is correct
30 Correct 430 ms 40548 KB Output is correct
31 Correct 462 ms 40636 KB Output is correct
32 Correct 449 ms 40832 KB Output is correct
33 Correct 420 ms 39448 KB Output is correct
34 Correct 476 ms 39088 KB Output is correct
35 Correct 490 ms 40036 KB Output is correct
36 Correct 336 ms 38776 KB Output is correct
37 Correct 497 ms 40224 KB Output is correct
38 Correct 483 ms 38864 KB Output is correct
39 Correct 468 ms 39656 KB Output is correct
40 Correct 487 ms 39156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 15892 KB Output is correct
2 Correct 82 ms 15760 KB Output is correct
3 Correct 84 ms 15460 KB Output is correct
4 Correct 90 ms 15600 KB Output is correct
5 Correct 84 ms 15832 KB Output is correct
6 Correct 83 ms 15296 KB Output is correct
7 Correct 82 ms 15484 KB Output is correct
8 Correct 80 ms 15476 KB Output is correct
9 Correct 79 ms 15296 KB Output is correct
10 Correct 83 ms 15612 KB Output is correct
11 Correct 327 ms 47032 KB Output is correct
12 Correct 306 ms 47020 KB Output is correct
13 Correct 327 ms 47200 KB Output is correct
14 Correct 318 ms 46632 KB Output is correct
15 Correct 300 ms 47188 KB Output is correct
16 Correct 301 ms 47260 KB Output is correct
17 Correct 320 ms 47200 KB Output is correct
18 Correct 306 ms 47208 KB Output is correct
19 Correct 312 ms 44928 KB Output is correct
20 Correct 304 ms 46952 KB Output is correct
21 Correct 316 ms 47268 KB Output is correct
22 Correct 326 ms 46932 KB Output is correct
23 Correct 302 ms 47176 KB Output is correct
24 Correct 310 ms 47460 KB Output is correct
25 Correct 341 ms 46796 KB Output is correct
26 Correct 286 ms 45304 KB Output is correct
27 Correct 286 ms 47228 KB Output is correct
28 Correct 288 ms 45240 KB Output is correct
29 Correct 314 ms 47080 KB Output is correct
30 Correct 312 ms 46868 KB Output is correct
31 Correct 298 ms 47372 KB Output is correct
32 Correct 316 ms 46908 KB Output is correct
33 Correct 335 ms 47056 KB Output is correct
34 Correct 295 ms 45420 KB Output is correct
35 Correct 317 ms 47152 KB Output is correct
36 Correct 315 ms 47600 KB Output is correct
37 Correct 306 ms 47104 KB Output is correct
38 Correct 299 ms 47328 KB Output is correct
39 Correct 317 ms 47444 KB Output is correct
40 Correct 352 ms 47116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 15892 KB Output is correct
2 Correct 82 ms 15760 KB Output is correct
3 Correct 84 ms 15460 KB Output is correct
4 Correct 90 ms 15600 KB Output is correct
5 Correct 84 ms 15832 KB Output is correct
6 Correct 83 ms 15296 KB Output is correct
7 Correct 82 ms 15484 KB Output is correct
8 Correct 80 ms 15476 KB Output is correct
9 Correct 79 ms 15296 KB Output is correct
10 Correct 83 ms 15612 KB Output is correct
11 Correct 377 ms 37408 KB Output is correct
12 Correct 383 ms 32016 KB Output is correct
13 Correct 415 ms 35484 KB Output is correct
14 Correct 418 ms 31512 KB Output is correct
15 Correct 407 ms 37332 KB Output is correct
16 Correct 427 ms 36684 KB Output is correct
17 Correct 438 ms 34292 KB Output is correct
18 Correct 334 ms 38524 KB Output is correct
19 Correct 365 ms 31012 KB Output is correct
20 Correct 377 ms 36852 KB Output is correct
21 Correct 446 ms 39776 KB Output is correct
22 Correct 471 ms 37952 KB Output is correct
23 Correct 486 ms 33924 KB Output is correct
24 Correct 453 ms 40028 KB Output is correct
25 Correct 481 ms 41528 KB Output is correct
26 Correct 493 ms 39824 KB Output is correct
27 Correct 501 ms 39660 KB Output is correct
28 Correct 392 ms 42412 KB Output is correct
29 Correct 404 ms 38224 KB Output is correct
30 Correct 430 ms 40548 KB Output is correct
31 Correct 462 ms 40636 KB Output is correct
32 Correct 449 ms 40832 KB Output is correct
33 Correct 420 ms 39448 KB Output is correct
34 Correct 476 ms 39088 KB Output is correct
35 Correct 490 ms 40036 KB Output is correct
36 Correct 336 ms 38776 KB Output is correct
37 Correct 497 ms 40224 KB Output is correct
38 Correct 483 ms 38864 KB Output is correct
39 Correct 468 ms 39656 KB Output is correct
40 Correct 487 ms 39156 KB Output is correct
41 Correct 327 ms 47032 KB Output is correct
42 Correct 306 ms 47020 KB Output is correct
43 Correct 327 ms 47200 KB Output is correct
44 Correct 318 ms 46632 KB Output is correct
45 Correct 300 ms 47188 KB Output is correct
46 Correct 301 ms 47260 KB Output is correct
47 Correct 320 ms 47200 KB Output is correct
48 Correct 306 ms 47208 KB Output is correct
49 Correct 312 ms 44928 KB Output is correct
50 Correct 304 ms 46952 KB Output is correct
51 Correct 316 ms 47268 KB Output is correct
52 Correct 326 ms 46932 KB Output is correct
53 Correct 302 ms 47176 KB Output is correct
54 Correct 310 ms 47460 KB Output is correct
55 Correct 341 ms 46796 KB Output is correct
56 Correct 286 ms 45304 KB Output is correct
57 Correct 286 ms 47228 KB Output is correct
58 Correct 288 ms 45240 KB Output is correct
59 Correct 314 ms 47080 KB Output is correct
60 Correct 312 ms 46868 KB Output is correct
61 Correct 298 ms 47372 KB Output is correct
62 Correct 316 ms 46908 KB Output is correct
63 Correct 335 ms 47056 KB Output is correct
64 Correct 295 ms 45420 KB Output is correct
65 Correct 317 ms 47152 KB Output is correct
66 Correct 315 ms 47600 KB Output is correct
67 Correct 306 ms 47104 KB Output is correct
68 Correct 299 ms 47328 KB Output is correct
69 Correct 317 ms 47444 KB Output is correct
70 Correct 352 ms 47116 KB Output is correct
71 Correct 706 ms 65536 KB Output is correct
72 Correct 808 ms 65536 KB Output is correct
73 Correct 858 ms 65536 KB Output is correct
74 Correct 934 ms 65536 KB Output is correct
75 Correct 721 ms 65536 KB Output is correct
76 Correct 876 ms 65536 KB Output is correct
77 Correct 760 ms 65536 KB Output is correct
78 Correct 471 ms 65536 KB Output is correct
79 Correct 569 ms 65536 KB Output is correct
80 Correct 702 ms 65536 KB Output is correct
81 Correct 759 ms 65536 KB Output is correct
82 Correct 839 ms 65536 KB Output is correct
83 Correct 640 ms 65536 KB Output is correct
84 Correct 837 ms 65536 KB Output is correct
85 Correct 813 ms 65536 KB Output is correct
86 Correct 464 ms 65536 KB Output is correct
87 Correct 652 ms 65536 KB Output is correct
88 Correct 630 ms 65536 KB Output is correct
89 Correct 712 ms 65536 KB Output is correct
90 Correct 733 ms 65536 KB Output is correct
91 Correct 654 ms 65536 KB Output is correct
92 Correct 817 ms 65536 KB Output is correct
93 Correct 783 ms 65536 KB Output is correct
94 Correct 424 ms 65536 KB Output is correct
95 Correct 682 ms 65536 KB Output is correct
96 Correct 681 ms 65536 KB Output is correct
97 Correct 815 ms 65536 KB Output is correct
98 Correct 741 ms 65536 KB Output is correct
99 Correct 773 ms 65536 KB Output is correct
100 Correct 767 ms 65536 KB Output is correct