답안 #379384

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379384 2021-03-18T05:34:02 Z cheissmart Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1464 ms 42220 KB
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 20;

int a[1 << 20], sub[1 << N], sup[1 << N];

signed main()
{
	IO_OP;
	
	int l, q;
	cin >> l >> q;
	for(int i = 0; i < (1 << l); i++) {
		char c;
		cin >> c;
		a[i] = c - '0';
	}
	memcpy(sub, a, sizeof(a[0]) * (1 << l));
	memcpy(sup, a, sizeof(a[0]) * (1 << l));
	for(int j = 0; j < l; j++) {
		for(int i = 0; i < (1 << l); i++)
			if(i >> j & 1)
				sub[i] += sub[i ^ (1 << j)];
	}
	for(int j = 0; j < l; j++) {
		for(int i = 0; i < (1 << l); i++)
			if(i >> j & 1)
				sup[i ^ (1 << j)] += sup[i];
	}
	while(q--) {
		string s;
		cin >> s;
		reverse(ALL(s));
		int one = 0, zero = 0, any = 0;
		for(int i = 0; i < l; i++)
			if(s[i] == '1') one++;
			else if(s[i] == '0') zero++;
			else any++;
		int m1 = 0, m2 = 0;
		for(int i = 0; i < l; i++) {
			if(s[i] == '?') m2 |= 1 << i;
			else m1 |= (s[i] - '0') << i;
		}
		int mn = min({one, zero, any});
		if(one == mn) {
			int ans = 0;
			for(int m = m1; ; m = (m - 1) & m1) {
				int sign = (__builtin_popcount(m ^ m1) % 2 ? -1 : 1);
				ans += sign * sub[m | m2];
				if(m == 0) break;
			}
			cout << ans << '\n';			
		} else if(zero == mn) {
			int ans = 0;
			int all = ((1 << l) - 1) ^ m2;
			m1 ^= all;
			for(int m = m1; ; m = (m - 1) & m1) {
				int mm = all ^ m;
				int sign = (__builtin_popcount(m ^ m1) % 2 ? -1 : 1);
				ans += sign * sup[mm];
				if(m == 0) break;
			}
			cout << ans << '\n';
		} else {
			int ans = 0;
			for(int m = m2; ; m = (m - 1) & m2) {
				ans += a[m1 | m];
				if(m == 0) break;
			}
			cout << ans << '\n';
		}
	}

}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 269 ms 6636 KB Output is correct
12 Correct 307 ms 6324 KB Output is correct
13 Correct 306 ms 5612 KB Output is correct
14 Correct 335 ms 5612 KB Output is correct
15 Correct 297 ms 6636 KB Output is correct
16 Correct 338 ms 5784 KB Output is correct
17 Correct 341 ms 5868 KB Output is correct
18 Correct 207 ms 7660 KB Output is correct
19 Correct 252 ms 4460 KB Output is correct
20 Correct 283 ms 6124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 269 ms 6636 KB Output is correct
12 Correct 307 ms 6324 KB Output is correct
13 Correct 306 ms 5612 KB Output is correct
14 Correct 335 ms 5612 KB Output is correct
15 Correct 297 ms 6636 KB Output is correct
16 Correct 338 ms 5784 KB Output is correct
17 Correct 341 ms 5868 KB Output is correct
18 Correct 207 ms 7660 KB Output is correct
19 Correct 252 ms 4460 KB Output is correct
20 Correct 283 ms 6124 KB Output is correct
21 Correct 320 ms 6508 KB Output is correct
22 Correct 367 ms 6764 KB Output is correct
23 Correct 413 ms 5612 KB Output is correct
24 Correct 503 ms 5484 KB Output is correct
25 Correct 373 ms 7512 KB Output is correct
26 Correct 422 ms 5996 KB Output is correct
27 Correct 417 ms 5996 KB Output is correct
28 Correct 229 ms 8428 KB Output is correct
29 Correct 279 ms 4460 KB Output is correct
30 Correct 334 ms 6636 KB Output is correct
31 Correct 385 ms 6380 KB Output is correct
32 Correct 441 ms 6400 KB Output is correct
33 Correct 402 ms 5344 KB Output is correct
34 Correct 420 ms 5444 KB Output is correct
35 Correct 425 ms 5912 KB Output is correct
36 Correct 209 ms 4460 KB Output is correct
37 Correct 338 ms 6252 KB Output is correct
38 Correct 312 ms 4332 KB Output is correct
39 Correct 380 ms 5356 KB Output is correct
40 Correct 431 ms 5356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 97 ms 13676 KB Output is correct
12 Correct 98 ms 13676 KB Output is correct
13 Correct 112 ms 13548 KB Output is correct
14 Correct 124 ms 13676 KB Output is correct
15 Correct 94 ms 13676 KB Output is correct
16 Correct 136 ms 13644 KB Output is correct
17 Correct 132 ms 13676 KB Output is correct
18 Correct 86 ms 13804 KB Output is correct
19 Correct 89 ms 13548 KB Output is correct
20 Correct 93 ms 13676 KB Output is correct
21 Correct 110 ms 13676 KB Output is correct
22 Correct 120 ms 13548 KB Output is correct
23 Correct 98 ms 13640 KB Output is correct
24 Correct 152 ms 13676 KB Output is correct
25 Correct 125 ms 13676 KB Output is correct
26 Correct 81 ms 13548 KB Output is correct
27 Correct 94 ms 13676 KB Output is correct
28 Correct 90 ms 13548 KB Output is correct
29 Correct 126 ms 13420 KB Output is correct
30 Correct 103 ms 13460 KB Output is correct
31 Correct 92 ms 13420 KB Output is correct
32 Correct 168 ms 13548 KB Output is correct
33 Correct 124 ms 13548 KB Output is correct
34 Correct 86 ms 13420 KB Output is correct
35 Correct 116 ms 13548 KB Output is correct
36 Correct 122 ms 13548 KB Output is correct
37 Correct 115 ms 13548 KB Output is correct
38 Correct 114 ms 13548 KB Output is correct
39 Correct 113 ms 13548 KB Output is correct
40 Correct 129 ms 13548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 269 ms 6636 KB Output is correct
12 Correct 307 ms 6324 KB Output is correct
13 Correct 306 ms 5612 KB Output is correct
14 Correct 335 ms 5612 KB Output is correct
15 Correct 297 ms 6636 KB Output is correct
16 Correct 338 ms 5784 KB Output is correct
17 Correct 341 ms 5868 KB Output is correct
18 Correct 207 ms 7660 KB Output is correct
19 Correct 252 ms 4460 KB Output is correct
20 Correct 283 ms 6124 KB Output is correct
21 Correct 320 ms 6508 KB Output is correct
22 Correct 367 ms 6764 KB Output is correct
23 Correct 413 ms 5612 KB Output is correct
24 Correct 503 ms 5484 KB Output is correct
25 Correct 373 ms 7512 KB Output is correct
26 Correct 422 ms 5996 KB Output is correct
27 Correct 417 ms 5996 KB Output is correct
28 Correct 229 ms 8428 KB Output is correct
29 Correct 279 ms 4460 KB Output is correct
30 Correct 334 ms 6636 KB Output is correct
31 Correct 385 ms 6380 KB Output is correct
32 Correct 441 ms 6400 KB Output is correct
33 Correct 402 ms 5344 KB Output is correct
34 Correct 420 ms 5444 KB Output is correct
35 Correct 425 ms 5912 KB Output is correct
36 Correct 209 ms 4460 KB Output is correct
37 Correct 338 ms 6252 KB Output is correct
38 Correct 312 ms 4332 KB Output is correct
39 Correct 380 ms 5356 KB Output is correct
40 Correct 431 ms 5356 KB Output is correct
41 Correct 97 ms 13676 KB Output is correct
42 Correct 98 ms 13676 KB Output is correct
43 Correct 112 ms 13548 KB Output is correct
44 Correct 124 ms 13676 KB Output is correct
45 Correct 94 ms 13676 KB Output is correct
46 Correct 136 ms 13644 KB Output is correct
47 Correct 132 ms 13676 KB Output is correct
48 Correct 86 ms 13804 KB Output is correct
49 Correct 89 ms 13548 KB Output is correct
50 Correct 93 ms 13676 KB Output is correct
51 Correct 110 ms 13676 KB Output is correct
52 Correct 120 ms 13548 KB Output is correct
53 Correct 98 ms 13640 KB Output is correct
54 Correct 152 ms 13676 KB Output is correct
55 Correct 125 ms 13676 KB Output is correct
56 Correct 81 ms 13548 KB Output is correct
57 Correct 94 ms 13676 KB Output is correct
58 Correct 90 ms 13548 KB Output is correct
59 Correct 126 ms 13420 KB Output is correct
60 Correct 103 ms 13460 KB Output is correct
61 Correct 92 ms 13420 KB Output is correct
62 Correct 168 ms 13548 KB Output is correct
63 Correct 124 ms 13548 KB Output is correct
64 Correct 86 ms 13420 KB Output is correct
65 Correct 116 ms 13548 KB Output is correct
66 Correct 122 ms 13548 KB Output is correct
67 Correct 115 ms 13548 KB Output is correct
68 Correct 114 ms 13548 KB Output is correct
69 Correct 113 ms 13548 KB Output is correct
70 Correct 129 ms 13548 KB Output is correct
71 Correct 636 ms 17884 KB Output is correct
72 Correct 641 ms 17900 KB Output is correct
73 Correct 784 ms 16492 KB Output is correct
74 Correct 1281 ms 16888 KB Output is correct
75 Correct 642 ms 19044 KB Output is correct
76 Correct 1464 ms 18028 KB Output is correct
77 Correct 1254 ms 38492 KB Output is correct
78 Correct 387 ms 42220 KB Output is correct
79 Correct 507 ms 36204 KB Output is correct
80 Correct 625 ms 39532 KB Output is correct
81 Correct 873 ms 39324 KB Output is correct
82 Correct 1104 ms 38380 KB Output is correct
83 Correct 620 ms 37484 KB Output is correct
84 Correct 1393 ms 38552 KB Output is correct
85 Correct 1244 ms 38508 KB Output is correct
86 Correct 365 ms 36204 KB Output is correct
87 Correct 629 ms 39276 KB Output is correct
88 Correct 543 ms 36204 KB Output is correct
89 Correct 836 ms 37996 KB Output is correct
90 Correct 794 ms 38380 KB Output is correct
91 Correct 586 ms 37540 KB Output is correct
92 Correct 1361 ms 38632 KB Output is correct
93 Correct 1222 ms 38760 KB Output is correct
94 Correct 422 ms 36200 KB Output is correct
95 Correct 1048 ms 38560 KB Output is correct
96 Correct 1022 ms 38508 KB Output is correct
97 Correct 1002 ms 38640 KB Output is correct
98 Correct 1033 ms 38512 KB Output is correct
99 Correct 1014 ms 38388 KB Output is correct
100 Correct 1028 ms 38692 KB Output is correct