답안 #679541

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679541 2023-01-08T13:10:00 Z SanguineChameleon Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
934 ms 42172 KB
// BEGIN BOILERPLATE CODE

#include <bits/stdc++.h>
using namespace std;

#ifdef KAMIRULEZ
	const bool kami_loc = true;
	const long long kami_seed = 69420;
#else
	const bool kami_loc = false;
	const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif

const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);

long long rand_range(long long rmin, long long rmax) {
	uniform_int_distribution<long long> rdist(rmin, rmax);
	return rdist(kami_gen);
}

long double rand_real(long double rmin, long double rmax) {
	uniform_real_distribution<long double> rdist(rmin, rmax);
	return rdist(kami_gen);
}

void file_io(string fi, string fo) {
	if (fi != "" && (!kami_loc || fi == kami_fi)) {
		freopen(fi.c_str(), "r", stdin);
	}
	if (fo != "" && (!kami_loc || fo == kami_fo)) {
		freopen(fo.c_str(), "w", stdout);
	}
}

void set_up() {
	if (kami_loc) {
		file_io(kami_fi, kami_fo);
	}
	ios_base::sync_with_stdio(0);
	cin.tie(0);
}

void just_do_it();

void just_exec_it() {
	if (kami_loc) {
		auto pstart = chrono::steady_clock::now();
		just_do_it();
		auto pend = chrono::steady_clock::now();
		long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
		string bar(50, '=');
		cout << '\n' << bar << '\n';
		cout << "Time: " << ptime << " ms" << '\n';
	}
	else {
		just_do_it();
	}
}

int main() {
	set_up();
	just_exec_it();
	return 0;
}

// END BOILERPLATE CODE

// BEGIN MAIN CODE

const int ms = (1 << 20) + 20;
int a[ms];
int sub[ms];
int sup[ms];

void just_do_it() {
	int n, q;
	cin >> n >> q;
	for (int i = 0; i < (1 << n); i++) {
		char c;
		cin >> c;
		a[i] = c - '0';
	}
	for (int i = 0; i < (1 << n); i++) {
		sub[i] = a[i];
		sup[i] = a[i];
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < (1 << n); j++) {
			if ((j >> i) & 1) {
				sub[j] += sub[j ^ (1 << i)];
			}
			else {
				sup[j] += sup[j ^ (1 << i)];
			}
		}
	}
	for (int i = 0; i < q; i++) {
		string s;
		cin >> s;
		int m0 = 0;
		int m1 = 0;
		int m2 = 0;
		for (int j = 0; j < n; j++) {
			m0 <<= 1;
			m1 <<= 1;
			m2 <<= 1;
			if (s[j] == '0') {
				m0 |= 1;
			}
			else if (s[j] == '1') {
				m1 |= 1;
			}
			else if (s[j] == '?') {
				m2 |= 1;
			}
		}
		if (__builtin_popcount(m0) <= 6) {
			int res = 0;
			for (int k = m0; ; k = (k - 1) & m0) {
				if (__builtin_popcount(k) & 1) {
					res -= sup[k | m1];
				}
				else {
					res += sup[k | m1];
				}
				if (k == 0) {
					break;
				}
			}
			cout << res << '\n';
		}
		else if (__builtin_popcount(m1) <= 6) {
			int res = 0;
			for (int k = m1; ; k = (k - 1) & m1) {
				if (__builtin_popcount(m1 ^ k) & 1) {
					res -= sub[k | m2];
				}
				else {
					res += sub[k | m2];
				}
				if (k == 0) {
					break;
				}
			}
			cout << res << '\n';
		}
		else if (__builtin_popcount(m2) <= 6) {
			int res = 0;
			for (int k = m2; ; k = (k - 1) & m2) {
				res += a[k | m1];
				if (k == 0) {
					break;
				}
			}
			cout << res << '\n';
		}
	}
}

// END MAIN CODE

Compilation message

snake_escaping.cpp: In function 'void file_io(std::string, std::string)':
snake_escaping.cpp:30:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |   freopen(fi.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |   freopen(fo.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 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 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 192 ms 15028 KB Output is correct
12 Correct 267 ms 14712 KB Output is correct
13 Correct 311 ms 14008 KB Output is correct
14 Correct 307 ms 14036 KB Output is correct
15 Correct 219 ms 15020 KB Output is correct
16 Correct 242 ms 14276 KB Output is correct
17 Correct 271 ms 14284 KB Output is correct
18 Correct 161 ms 16084 KB Output is correct
19 Correct 275 ms 13004 KB Output is correct
20 Correct 207 ms 14752 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 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 192 ms 15028 KB Output is correct
12 Correct 267 ms 14712 KB Output is correct
13 Correct 311 ms 14008 KB Output is correct
14 Correct 307 ms 14036 KB Output is correct
15 Correct 219 ms 15020 KB Output is correct
16 Correct 242 ms 14276 KB Output is correct
17 Correct 271 ms 14284 KB Output is correct
18 Correct 161 ms 16084 KB Output is correct
19 Correct 275 ms 13004 KB Output is correct
20 Correct 207 ms 14752 KB Output is correct
21 Correct 206 ms 18112 KB Output is correct
22 Correct 285 ms 18324 KB Output is correct
23 Correct 458 ms 17300 KB Output is correct
24 Correct 343 ms 17032 KB Output is correct
25 Correct 245 ms 19012 KB Output is correct
26 Correct 296 ms 17628 KB Output is correct
27 Correct 316 ms 17540 KB Output is correct
28 Correct 169 ms 20016 KB Output is correct
29 Correct 403 ms 16148 KB Output is correct
30 Correct 224 ms 18264 KB Output is correct
31 Correct 306 ms 18024 KB Output is correct
32 Correct 290 ms 18060 KB Output is correct
33 Correct 247 ms 17060 KB Output is correct
34 Correct 330 ms 17232 KB Output is correct
35 Correct 356 ms 17464 KB Output is correct
36 Correct 178 ms 16152 KB Output is correct
37 Correct 209 ms 18092 KB Output is correct
38 Correct 346 ms 16140 KB Output is correct
39 Correct 289 ms 17352 KB Output is correct
40 Correct 284 ms 17096 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 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 57 ms 14824 KB Output is correct
12 Correct 55 ms 14844 KB Output is correct
13 Correct 68 ms 14804 KB Output is correct
14 Correct 67 ms 14800 KB Output is correct
15 Correct 55 ms 14920 KB Output is correct
16 Correct 78 ms 14800 KB Output is correct
17 Correct 74 ms 14796 KB Output is correct
18 Correct 46 ms 15052 KB Output is correct
19 Correct 52 ms 14648 KB Output is correct
20 Correct 52 ms 14816 KB Output is correct
21 Correct 64 ms 14820 KB Output is correct
22 Correct 67 ms 14888 KB Output is correct
23 Correct 54 ms 14752 KB Output is correct
24 Correct 77 ms 14860 KB Output is correct
25 Correct 79 ms 14864 KB Output is correct
26 Correct 47 ms 14664 KB Output is correct
27 Correct 57 ms 14900 KB Output is correct
28 Correct 56 ms 14728 KB Output is correct
29 Correct 63 ms 14800 KB Output is correct
30 Correct 61 ms 14844 KB Output is correct
31 Correct 56 ms 14816 KB Output is correct
32 Correct 81 ms 14796 KB Output is correct
33 Correct 80 ms 14828 KB Output is correct
34 Correct 48 ms 14672 KB Output is correct
35 Correct 92 ms 14796 KB Output is correct
36 Correct 70 ms 14832 KB Output is correct
37 Correct 76 ms 14780 KB Output is correct
38 Correct 70 ms 14824 KB Output is correct
39 Correct 69 ms 14828 KB Output is correct
40 Correct 67 ms 14796 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 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 192 ms 15028 KB Output is correct
12 Correct 267 ms 14712 KB Output is correct
13 Correct 311 ms 14008 KB Output is correct
14 Correct 307 ms 14036 KB Output is correct
15 Correct 219 ms 15020 KB Output is correct
16 Correct 242 ms 14276 KB Output is correct
17 Correct 271 ms 14284 KB Output is correct
18 Correct 161 ms 16084 KB Output is correct
19 Correct 275 ms 13004 KB Output is correct
20 Correct 207 ms 14752 KB Output is correct
21 Correct 206 ms 18112 KB Output is correct
22 Correct 285 ms 18324 KB Output is correct
23 Correct 458 ms 17300 KB Output is correct
24 Correct 343 ms 17032 KB Output is correct
25 Correct 245 ms 19012 KB Output is correct
26 Correct 296 ms 17628 KB Output is correct
27 Correct 316 ms 17540 KB Output is correct
28 Correct 169 ms 20016 KB Output is correct
29 Correct 403 ms 16148 KB Output is correct
30 Correct 224 ms 18264 KB Output is correct
31 Correct 306 ms 18024 KB Output is correct
32 Correct 290 ms 18060 KB Output is correct
33 Correct 247 ms 17060 KB Output is correct
34 Correct 330 ms 17232 KB Output is correct
35 Correct 356 ms 17464 KB Output is correct
36 Correct 178 ms 16152 KB Output is correct
37 Correct 209 ms 18092 KB Output is correct
38 Correct 346 ms 16140 KB Output is correct
39 Correct 289 ms 17352 KB Output is correct
40 Correct 284 ms 17096 KB Output is correct
41 Correct 57 ms 14824 KB Output is correct
42 Correct 55 ms 14844 KB Output is correct
43 Correct 68 ms 14804 KB Output is correct
44 Correct 67 ms 14800 KB Output is correct
45 Correct 55 ms 14920 KB Output is correct
46 Correct 78 ms 14800 KB Output is correct
47 Correct 74 ms 14796 KB Output is correct
48 Correct 46 ms 15052 KB Output is correct
49 Correct 52 ms 14648 KB Output is correct
50 Correct 52 ms 14816 KB Output is correct
51 Correct 64 ms 14820 KB Output is correct
52 Correct 67 ms 14888 KB Output is correct
53 Correct 54 ms 14752 KB Output is correct
54 Correct 77 ms 14860 KB Output is correct
55 Correct 79 ms 14864 KB Output is correct
56 Correct 47 ms 14664 KB Output is correct
57 Correct 57 ms 14900 KB Output is correct
58 Correct 56 ms 14728 KB Output is correct
59 Correct 63 ms 14800 KB Output is correct
60 Correct 61 ms 14844 KB Output is correct
61 Correct 56 ms 14816 KB Output is correct
62 Correct 81 ms 14796 KB Output is correct
63 Correct 80 ms 14828 KB Output is correct
64 Correct 48 ms 14672 KB Output is correct
65 Correct 92 ms 14796 KB Output is correct
66 Correct 70 ms 14832 KB Output is correct
67 Correct 76 ms 14780 KB Output is correct
68 Correct 70 ms 14824 KB Output is correct
69 Correct 69 ms 14828 KB Output is correct
70 Correct 67 ms 14796 KB Output is correct
71 Correct 377 ms 39156 KB Output is correct
72 Correct 383 ms 39236 KB Output is correct
73 Correct 533 ms 37712 KB Output is correct
74 Correct 645 ms 38092 KB Output is correct
75 Correct 397 ms 40172 KB Output is correct
76 Correct 716 ms 38472 KB Output is correct
77 Correct 813 ms 38316 KB Output is correct
78 Correct 263 ms 42172 KB Output is correct
79 Correct 361 ms 36208 KB Output is correct
80 Correct 374 ms 39352 KB Output is correct
81 Correct 535 ms 39116 KB Output is correct
82 Correct 674 ms 38136 KB Output is correct
83 Correct 405 ms 37356 KB Output is correct
84 Correct 934 ms 38248 KB Output is correct
85 Correct 848 ms 38324 KB Output is correct
86 Correct 267 ms 36160 KB Output is correct
87 Correct 342 ms 39060 KB Output is correct
88 Correct 410 ms 36148 KB Output is correct
89 Correct 586 ms 37792 KB Output is correct
90 Correct 518 ms 38124 KB Output is correct
91 Correct 421 ms 37400 KB Output is correct
92 Correct 874 ms 38504 KB Output is correct
93 Correct 852 ms 38440 KB Output is correct
94 Correct 247 ms 36108 KB Output is correct
95 Correct 658 ms 38224 KB Output is correct
96 Correct 648 ms 38128 KB Output is correct
97 Correct 666 ms 38204 KB Output is correct
98 Correct 688 ms 38184 KB Output is correct
99 Correct 706 ms 38196 KB Output is correct
100 Correct 675 ms 38200 KB Output is correct