답안 #535016

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535016 2022-03-09T09:59:57 Z pooyashams Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1895 ms 42068 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int lgmx = 20;
const int maxn = (1<<lgmx);

int val[maxn];
int sub[maxn];
int sup[maxn];

int32_t main()
{
	int L, q;
	scanf("%d %d", &L, &q);
	const int n = (1<<L);
	for(int i = 0; i < n; i++)
	{
		char c;
		scanf(" %c ", &c);
		val[i] = c-'0';
		sub[i] = val[i];
		sup[i] = val[i];
	}
	for(int j = 0; j < L; j++)
		for(int i = 0; i < n; i++)
			if((i>>j) & 1)
				sub[i] += sub[i^(1<<j)];
	for(int j = L-1; j >= 0; j--)
		for(int i = n-1; i >= 0; i--)
			if((i>>j) & 1)
				sup[i^(1<<j)] += sup[i];
	vector<int> vec[3];
	for(int i = 0; i < 3; i++) vec[i].reserve(20);
	const int l3 = L/3;
	while(q--)
	{
		for(int i = 0; i < 3; i++) vec[i].clear();
		for(int i = L-1; i >= 0; i--)
		{
			char c; scanf(" %c ", &c);
			if(c == '?')
				vec[2].push_back(i);
			else
				vec[c-'0'].push_back(i);
		}
		int ans = 0;
		if((int)(vec[0].size()) <= l3)
		{
			int a = vec[0].size();
			int k = 0;
			for(int j: vec[1]) k ^= (1<<j);
			for(int i = 0; i < (1<<a); i++)
			{
				int x = k;
				for(int j = i; j > 0; j -= j&-j)
					x ^= (1<<vec[0][__builtin_ctz(j)]);
				if(__builtin_popcount(i) & 1)
					ans -= sup[x];
				else
					ans += sup[x];
			}
		}
		else if((int)(vec[1].size()) <= l3)
		{
			int a = vec[1].size();
			int k = 0;
			for(int j: vec[2]) k ^= (1<<j);
			for(int i = 0; i < (1<<a); i++)
			{
				int x = k;
				for(int j = i; j > 0; j -= j&-j)
					x ^= (1<<vec[1][__builtin_ctz(j)]);
				if( (a-__builtin_popcount(i)) & 1)
					ans -= sub[x];
				else
					ans += sub[x];
			}
		}
		else
		{
			int a = vec[2].size();
			int k = 0;
			for(int j: vec[1]) k ^= (1<<j);
			for(int i = 0; i < (1<<a); i++)
			{
				int x = k;
				for(int j = i; j > 0; j -= j&-j)
					x ^= (1<<vec[2][__builtin_ctz(j)]);
				ans += val[x];
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int32_t main()':
snake_escaping.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |  scanf("%d %d", &L, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
snake_escaping.cpp:34:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |   scanf(" %c ", &c);
      |   ~~~~~^~~~~~~~~~~~
snake_escaping.cpp:55:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |    char c; scanf(" %c ", &c);
      |            ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 702 ms 4224 KB Output is correct
12 Correct 711 ms 3984 KB Output is correct
13 Correct 696 ms 3384 KB Output is correct
14 Correct 714 ms 3268 KB Output is correct
15 Correct 790 ms 4320 KB Output is correct
16 Correct 724 ms 3668 KB Output is correct
17 Correct 707 ms 3424 KB Output is correct
18 Correct 592 ms 5304 KB Output is correct
19 Correct 620 ms 2316 KB Output is correct
20 Correct 646 ms 3904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 702 ms 4224 KB Output is correct
12 Correct 711 ms 3984 KB Output is correct
13 Correct 696 ms 3384 KB Output is correct
14 Correct 714 ms 3268 KB Output is correct
15 Correct 790 ms 4320 KB Output is correct
16 Correct 724 ms 3668 KB Output is correct
17 Correct 707 ms 3424 KB Output is correct
18 Correct 592 ms 5304 KB Output is correct
19 Correct 620 ms 2316 KB Output is correct
20 Correct 646 ms 3904 KB Output is correct
21 Correct 813 ms 4496 KB Output is correct
22 Correct 829 ms 4456 KB Output is correct
23 Correct 846 ms 3592 KB Output is correct
24 Correct 880 ms 3396 KB Output is correct
25 Correct 848 ms 5400 KB Output is correct
26 Correct 904 ms 3876 KB Output is correct
27 Correct 865 ms 3828 KB Output is correct
28 Correct 737 ms 6380 KB Output is correct
29 Correct 750 ms 2320 KB Output is correct
30 Correct 814 ms 4468 KB Output is correct
31 Correct 844 ms 4512 KB Output is correct
32 Correct 886 ms 4504 KB Output is correct
33 Correct 840 ms 3304 KB Output is correct
34 Correct 856 ms 3428 KB Output is correct
35 Correct 901 ms 3836 KB Output is correct
36 Correct 768 ms 2324 KB Output is correct
37 Correct 794 ms 4408 KB Output is correct
38 Correct 782 ms 2292 KB Output is correct
39 Correct 822 ms 3572 KB Output is correct
40 Correct 890 ms 3476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 146 ms 12716 KB Output is correct
12 Correct 156 ms 12828 KB Output is correct
13 Correct 161 ms 12660 KB Output is correct
14 Correct 171 ms 12672 KB Output is correct
15 Correct 151 ms 12860 KB Output is correct
16 Correct 173 ms 12708 KB Output is correct
17 Correct 173 ms 12836 KB Output is correct
18 Correct 138 ms 12868 KB Output is correct
19 Correct 144 ms 12704 KB Output is correct
20 Correct 148 ms 12868 KB Output is correct
21 Correct 159 ms 12836 KB Output is correct
22 Correct 169 ms 12772 KB Output is correct
23 Correct 154 ms 12704 KB Output is correct
24 Correct 181 ms 12744 KB Output is correct
25 Correct 174 ms 12696 KB Output is correct
26 Correct 142 ms 12572 KB Output is correct
27 Correct 149 ms 12812 KB Output is correct
28 Correct 149 ms 12612 KB Output is correct
29 Correct 159 ms 12704 KB Output is correct
30 Correct 161 ms 12740 KB Output is correct
31 Correct 156 ms 12736 KB Output is correct
32 Correct 184 ms 12868 KB Output is correct
33 Correct 168 ms 12724 KB Output is correct
34 Correct 148 ms 12588 KB Output is correct
35 Correct 170 ms 12704 KB Output is correct
36 Correct 160 ms 12672 KB Output is correct
37 Correct 162 ms 12740 KB Output is correct
38 Correct 169 ms 12816 KB Output is correct
39 Correct 167 ms 12692 KB Output is correct
40 Correct 160 ms 12740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 702 ms 4224 KB Output is correct
12 Correct 711 ms 3984 KB Output is correct
13 Correct 696 ms 3384 KB Output is correct
14 Correct 714 ms 3268 KB Output is correct
15 Correct 790 ms 4320 KB Output is correct
16 Correct 724 ms 3668 KB Output is correct
17 Correct 707 ms 3424 KB Output is correct
18 Correct 592 ms 5304 KB Output is correct
19 Correct 620 ms 2316 KB Output is correct
20 Correct 646 ms 3904 KB Output is correct
21 Correct 813 ms 4496 KB Output is correct
22 Correct 829 ms 4456 KB Output is correct
23 Correct 846 ms 3592 KB Output is correct
24 Correct 880 ms 3396 KB Output is correct
25 Correct 848 ms 5400 KB Output is correct
26 Correct 904 ms 3876 KB Output is correct
27 Correct 865 ms 3828 KB Output is correct
28 Correct 737 ms 6380 KB Output is correct
29 Correct 750 ms 2320 KB Output is correct
30 Correct 814 ms 4468 KB Output is correct
31 Correct 844 ms 4512 KB Output is correct
32 Correct 886 ms 4504 KB Output is correct
33 Correct 840 ms 3304 KB Output is correct
34 Correct 856 ms 3428 KB Output is correct
35 Correct 901 ms 3836 KB Output is correct
36 Correct 768 ms 2324 KB Output is correct
37 Correct 794 ms 4408 KB Output is correct
38 Correct 782 ms 2292 KB Output is correct
39 Correct 822 ms 3572 KB Output is correct
40 Correct 890 ms 3476 KB Output is correct
41 Correct 146 ms 12716 KB Output is correct
42 Correct 156 ms 12828 KB Output is correct
43 Correct 161 ms 12660 KB Output is correct
44 Correct 171 ms 12672 KB Output is correct
45 Correct 151 ms 12860 KB Output is correct
46 Correct 173 ms 12708 KB Output is correct
47 Correct 173 ms 12836 KB Output is correct
48 Correct 138 ms 12868 KB Output is correct
49 Correct 144 ms 12704 KB Output is correct
50 Correct 148 ms 12868 KB Output is correct
51 Correct 159 ms 12836 KB Output is correct
52 Correct 169 ms 12772 KB Output is correct
53 Correct 154 ms 12704 KB Output is correct
54 Correct 181 ms 12744 KB Output is correct
55 Correct 174 ms 12696 KB Output is correct
56 Correct 142 ms 12572 KB Output is correct
57 Correct 149 ms 12812 KB Output is correct
58 Correct 149 ms 12612 KB Output is correct
59 Correct 159 ms 12704 KB Output is correct
60 Correct 161 ms 12740 KB Output is correct
61 Correct 156 ms 12736 KB Output is correct
62 Correct 184 ms 12868 KB Output is correct
63 Correct 168 ms 12724 KB Output is correct
64 Correct 148 ms 12588 KB Output is correct
65 Correct 170 ms 12704 KB Output is correct
66 Correct 160 ms 12672 KB Output is correct
67 Correct 162 ms 12740 KB Output is correct
68 Correct 169 ms 12816 KB Output is correct
69 Correct 167 ms 12692 KB Output is correct
70 Correct 160 ms 12740 KB Output is correct
71 Correct 1314 ms 17560 KB Output is correct
72 Correct 1317 ms 17720 KB Output is correct
73 Correct 1438 ms 16232 KB Output is correct
74 Correct 1755 ms 21352 KB Output is correct
75 Correct 1330 ms 40088 KB Output is correct
76 Correct 1724 ms 38504 KB Output is correct
77 Correct 1787 ms 38308 KB Output is correct
78 Correct 1124 ms 42068 KB Output is correct
79 Correct 1221 ms 36076 KB Output is correct
80 Correct 1289 ms 39364 KB Output is correct
81 Correct 1495 ms 39240 KB Output is correct
82 Correct 1720 ms 38288 KB Output is correct
83 Correct 1355 ms 37256 KB Output is correct
84 Correct 1895 ms 38204 KB Output is correct
85 Correct 1842 ms 38228 KB Output is correct
86 Correct 1213 ms 36348 KB Output is correct
87 Correct 1301 ms 39224 KB Output is correct
88 Correct 1343 ms 36216 KB Output is correct
89 Correct 1497 ms 37952 KB Output is correct
90 Correct 1628 ms 38156 KB Output is correct
91 Correct 1366 ms 37304 KB Output is correct
92 Correct 1892 ms 38428 KB Output is correct
93 Correct 1812 ms 38348 KB Output is correct
94 Correct 1202 ms 36124 KB Output is correct
95 Correct 1663 ms 38296 KB Output is correct
96 Correct 1643 ms 38188 KB Output is correct
97 Correct 1705 ms 38220 KB Output is correct
98 Correct 1648 ms 38176 KB Output is correct
99 Correct 1676 ms 38200 KB Output is correct
100 Correct 1668 ms 38288 KB Output is correct