답안 #539327

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
539327 2022-03-18T17:42:45 Z pnm1384 Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1516 ms 39272 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 21;
int dp[1 << N], dp2[1 << N], cnt[3], bads[N];
string ss;

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int len, q;
	cin >> len >> q >> ss;
	for (int i = 0; i < (1 << len); i++)
	{
		dp[i] = ss[i] - '0';
		dp2[i] = ss[i] - '0';
	}
	for (int i = 0; i < len; i++)
	{
		for (int mask = 0; mask < (1 << len); mask++)
		{
			if (!((mask >> i) & 1)) dp[mask] += dp[mask ^ (1 << i)];
			else dp2[mask] += dp2[mask ^ (1 << i)];
		}
	}
	for (int jj = 0; jj < q; jj++)
	{
		string s2;
		cin >> s2;
		reverse(s2.begin(), s2.end());
		for (int j = 0; j < 3; j++) cnt[j] = 0;
		for (int i = 0; i < len; i++)
		{
			if (s2[i] == '?') cnt[0]++;
			else cnt[s2[i] - '0' + 1]++;
		}
		int ans = 0;
		if (cnt[0] <= 6)
		{
			int msk = 0;
			for (int i = 0; i < len; i++)
			{
				if (s2[i] == '1') msk ^= (1 << i);
			}
			int ttt = 0;
			for (int i = 0; i < len; i++)
			{
				if (s2[i] == '?') bads[ttt++] = i;
			}
			for (int mask = 0; mask < (1 << ttt); mask++)
			{
				int msk2 = msk;
				for (int i = 0; i < ttt; i++)
				{
					if ((mask >> i) & 1) msk2 |= (1 << bads[i]);
				}
				ans += ss[msk2] - '0';
			}
			cout << ans << '\n';
			continue;
		}
		if (cnt[1] <= 6)
		{
			int msk = 0;
			for (int i = 0; i < len; i++)
			{
				if (s2[i] == '1') msk ^= (1 << i);
			}
			int ttt = 0;
			for (int i = 0; i < len; i++)
			{
				if (s2[i] == '0') bads[ttt++] = i;
			}
			for (int mask = 0; mask < (1 << ttt); mask++)
			{
				int msk2 = msk;
				for (int i = 0; i < ttt; i++)
				{
					if ((mask >> i) & 1) msk2 |= (1 << bads[i]);
				}
				int f = 1;
				if (__builtin_parity(mask)) f = -1;
				ans += f * dp[msk2];
			}
			cout << ans << '\n';
			continue;
		}
		int msk = 0;
		for (int i = 0; i < len; i++)
		{
			if (s2[i] == '?' || s2[i] == '1') msk ^= (1 << i);
		}
		int ttt = 0;
		for (int i = 0; i < len; i++)
		{
			if (s2[i] == '1') bads[ttt++] = i;
		}
		for (int mask = 0; mask < (1 << ttt); mask++)
		{
			int msk2 = msk;
			for (int i = 0; i < ttt; i++)
			{
				if ((mask >> i) & 1) msk2 ^= (1 << bads[i]);
			}
			int f = 1;
			if (__builtin_parity(mask)) f = -1;
			ans += f * dp2[msk2];
		}
		cout << ans << '\n';
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 336 KB Output is correct
11 Correct 520 ms 6224 KB Output is correct
12 Correct 531 ms 5768 KB Output is correct
13 Correct 348 ms 5080 KB Output is correct
14 Correct 328 ms 5116 KB Output is correct
15 Correct 995 ms 6184 KB Output is correct
16 Correct 474 ms 5312 KB Output is correct
17 Correct 450 ms 5296 KB Output is correct
18 Correct 179 ms 7316 KB Output is correct
19 Correct 257 ms 4116 KB Output is correct
20 Correct 627 ms 5768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 336 KB Output is correct
11 Correct 520 ms 6224 KB Output is correct
12 Correct 531 ms 5768 KB Output is correct
13 Correct 348 ms 5080 KB Output is correct
14 Correct 328 ms 5116 KB Output is correct
15 Correct 995 ms 6184 KB Output is correct
16 Correct 474 ms 5312 KB Output is correct
17 Correct 450 ms 5296 KB Output is correct
18 Correct 179 ms 7316 KB Output is correct
19 Correct 257 ms 4116 KB Output is correct
20 Correct 627 ms 5768 KB Output is correct
21 Correct 367 ms 6272 KB Output is correct
22 Correct 756 ms 6536 KB Output is correct
23 Correct 519 ms 5416 KB Output is correct
24 Correct 447 ms 5192 KB Output is correct
25 Correct 394 ms 7276 KB Output is correct
26 Correct 636 ms 5876 KB Output is correct
27 Correct 585 ms 5716 KB Output is correct
28 Correct 238 ms 8232 KB Output is correct
29 Correct 280 ms 4172 KB Output is correct
30 Correct 602 ms 6332 KB Output is correct
31 Correct 1031 ms 6312 KB Output is correct
32 Correct 628 ms 6236 KB Output is correct
33 Correct 391 ms 5176 KB Output is correct
34 Correct 459 ms 5428 KB Output is correct
35 Correct 563 ms 5684 KB Output is correct
36 Correct 224 ms 4172 KB Output is correct
37 Correct 1020 ms 6296 KB Output is correct
38 Correct 290 ms 4348 KB Output is correct
39 Correct 572 ms 5496 KB Output is correct
40 Correct 435 ms 5176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 336 KB Output is correct
11 Correct 55 ms 11848 KB Output is correct
12 Correct 56 ms 11884 KB Output is correct
13 Correct 63 ms 11724 KB Output is correct
14 Correct 111 ms 11824 KB Output is correct
15 Correct 66 ms 12016 KB Output is correct
16 Correct 84 ms 12076 KB Output is correct
17 Correct 83 ms 11884 KB Output is correct
18 Correct 44 ms 12108 KB Output is correct
19 Correct 52 ms 11804 KB Output is correct
20 Correct 58 ms 11920 KB Output is correct
21 Correct 67 ms 11932 KB Output is correct
22 Correct 82 ms 11848 KB Output is correct
23 Correct 54 ms 11856 KB Output is correct
24 Correct 110 ms 11928 KB Output is correct
25 Correct 87 ms 11852 KB Output is correct
26 Correct 41 ms 11756 KB Output is correct
27 Correct 51 ms 12112 KB Output is correct
28 Correct 47 ms 11756 KB Output is correct
29 Correct 75 ms 11864 KB Output is correct
30 Correct 83 ms 11988 KB Output is correct
31 Correct 53 ms 11992 KB Output is correct
32 Correct 102 ms 11892 KB Output is correct
33 Correct 105 ms 11896 KB Output is correct
34 Correct 40 ms 11820 KB Output is correct
35 Correct 74 ms 11996 KB Output is correct
36 Correct 75 ms 11884 KB Output is correct
37 Correct 84 ms 11928 KB Output is correct
38 Correct 72 ms 11940 KB Output is correct
39 Correct 76 ms 11880 KB Output is correct
40 Correct 86 ms 11932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 336 KB Output is correct
11 Correct 520 ms 6224 KB Output is correct
12 Correct 531 ms 5768 KB Output is correct
13 Correct 348 ms 5080 KB Output is correct
14 Correct 328 ms 5116 KB Output is correct
15 Correct 995 ms 6184 KB Output is correct
16 Correct 474 ms 5312 KB Output is correct
17 Correct 450 ms 5296 KB Output is correct
18 Correct 179 ms 7316 KB Output is correct
19 Correct 257 ms 4116 KB Output is correct
20 Correct 627 ms 5768 KB Output is correct
21 Correct 367 ms 6272 KB Output is correct
22 Correct 756 ms 6536 KB Output is correct
23 Correct 519 ms 5416 KB Output is correct
24 Correct 447 ms 5192 KB Output is correct
25 Correct 394 ms 7276 KB Output is correct
26 Correct 636 ms 5876 KB Output is correct
27 Correct 585 ms 5716 KB Output is correct
28 Correct 238 ms 8232 KB Output is correct
29 Correct 280 ms 4172 KB Output is correct
30 Correct 602 ms 6332 KB Output is correct
31 Correct 1031 ms 6312 KB Output is correct
32 Correct 628 ms 6236 KB Output is correct
33 Correct 391 ms 5176 KB Output is correct
34 Correct 459 ms 5428 KB Output is correct
35 Correct 563 ms 5684 KB Output is correct
36 Correct 224 ms 4172 KB Output is correct
37 Correct 1020 ms 6296 KB Output is correct
38 Correct 290 ms 4348 KB Output is correct
39 Correct 572 ms 5496 KB Output is correct
40 Correct 435 ms 5176 KB Output is correct
41 Correct 55 ms 11848 KB Output is correct
42 Correct 56 ms 11884 KB Output is correct
43 Correct 63 ms 11724 KB Output is correct
44 Correct 111 ms 11824 KB Output is correct
45 Correct 66 ms 12016 KB Output is correct
46 Correct 84 ms 12076 KB Output is correct
47 Correct 83 ms 11884 KB Output is correct
48 Correct 44 ms 12108 KB Output is correct
49 Correct 52 ms 11804 KB Output is correct
50 Correct 58 ms 11920 KB Output is correct
51 Correct 67 ms 11932 KB Output is correct
52 Correct 82 ms 11848 KB Output is correct
53 Correct 54 ms 11856 KB Output is correct
54 Correct 110 ms 11928 KB Output is correct
55 Correct 87 ms 11852 KB Output is correct
56 Correct 41 ms 11756 KB Output is correct
57 Correct 51 ms 12112 KB Output is correct
58 Correct 47 ms 11756 KB Output is correct
59 Correct 75 ms 11864 KB Output is correct
60 Correct 83 ms 11988 KB Output is correct
61 Correct 53 ms 11992 KB Output is correct
62 Correct 102 ms 11892 KB Output is correct
63 Correct 105 ms 11896 KB Output is correct
64 Correct 40 ms 11820 KB Output is correct
65 Correct 74 ms 11996 KB Output is correct
66 Correct 75 ms 11884 KB Output is correct
67 Correct 84 ms 11928 KB Output is correct
68 Correct 72 ms 11940 KB Output is correct
69 Correct 76 ms 11880 KB Output is correct
70 Correct 86 ms 11932 KB Output is correct
71 Correct 558 ms 36176 KB Output is correct
72 Correct 675 ms 36476 KB Output is correct
73 Correct 812 ms 34836 KB Output is correct
74 Correct 1516 ms 35376 KB Output is correct
75 Correct 689 ms 37368 KB Output is correct
76 Correct 1240 ms 35712 KB Output is correct
77 Correct 1324 ms 35412 KB Output is correct
78 Correct 361 ms 39272 KB Output is correct
79 Correct 470 ms 33376 KB Output is correct
80 Correct 639 ms 36396 KB Output is correct
81 Correct 910 ms 36244 KB Output is correct
82 Correct 1280 ms 35240 KB Output is correct
83 Correct 711 ms 34456 KB Output is correct
84 Correct 1230 ms 35280 KB Output is correct
85 Correct 1167 ms 35384 KB Output is correct
86 Correct 363 ms 33280 KB Output is correct
87 Correct 557 ms 36328 KB Output is correct
88 Correct 466 ms 33352 KB Output is correct
89 Correct 835 ms 34948 KB Output is correct
90 Correct 1259 ms 35260 KB Output is correct
91 Correct 636 ms 34428 KB Output is correct
92 Correct 1437 ms 35512 KB Output is correct
93 Correct 1296 ms 35584 KB Output is correct
94 Correct 349 ms 33248 KB Output is correct
95 Correct 967 ms 35348 KB Output is correct
96 Correct 1016 ms 35348 KB Output is correct
97 Correct 988 ms 35344 KB Output is correct
98 Correct 1036 ms 35300 KB Output is correct
99 Correct 1051 ms 35428 KB Output is correct
100 Correct 965 ms 35308 KB Output is correct