답안 #334263

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
334263 2020-12-08T21:36:26 Z ChrisT Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 40548 KB
#include <bits/stdc++.h>
using namespace std;
int val[1 << 20], sum1[1 << 20], sum2[1 << 20]; 
char str[25];
int main() { //nut
	int n,q;
	scanf ("%d %d",&n,&q);
	for (int i = 0; i < (1 << n); i++) {
		char ch; scanf (" %c",&ch); 
		sum2[((1 << n) - 1) ^ i] = sum1[i] = val[i] = ch - '0';
	}
	for (int i = 0; i < n; i++) {
		for (int mask = 0; mask < (1 << n); mask++) if (mask >> i & 1) {
			sum1[mask] += sum1[mask ^ (1 << i)];
			sum2[mask] += sum2[mask ^ (1 << i)];
		}
	}
	while (q--) {
		scanf ("%s",str);
		reverse(str,str+n);
		int cnt0 = 0, cnt1 = 0, cntq = 0;
		for (int i = 0; i < n; i++) {
			if (str[i] == '0') ++cnt0;
			else if (str[i] == '1') ++cnt1;
			else ++cntq;
		}
		if (cntq <= cnt0 && cntq <= cnt1) { //brute force ?s
			vector<int> pos; pos.reserve(cntq);
			int mask = 0, ret = 0;
			for (int i = 0; i < n; i++) {
				if (str[i] == '?') pos.push_back(i);
				mask |= (str[i] == '1') << i;
			}
			for (int qmask = 0; qmask < (1 << cntq); qmask++) {
				int nmask = mask;
				for (int i = 0; i < cntq; i++) nmask |= (qmask >> i & 1) << pos[i];
				ret += val[nmask];
			}
			printf ("%d\n",ret);
		} else if (cnt0 <= cnt1) { //PIE
			vector<int> pos; pos.reserve(cnt0);
			int mask = 0, ret = 0;
			for (int i = 0; i < n; i++) {
				if (str[i] == '0') pos.push_back(i);
				mask |= (str[i] != '1') << i;
			}
			for (int mask0 = 0; mask0 < (1 << cnt0); mask0++) {
				int nmask = mask;
				for (int i = 0; i < cnt0; i++) nmask ^= (mask0 >> i & 1) << pos[i];
				ret += (__builtin_popcount(mask0) & 1 ? -1 : 1) * sum2[nmask];
			}
			printf ("%d\n",ret);
		} else { //'' ''
			vector<int> pos; pos.reserve(cnt1);
			int mask = 0, ret = 0;
			for (int i = 0; i < n; i++) {
				if (str[i] == '1') pos.push_back(i);
				mask |= (str[i] != '0') << i;
			}
			for (int mask1 = 0; mask1 < (1 << cnt1); mask1++) {
				int nmask = mask;
				for (int i = 0; i < cnt1; i++) nmask ^= (mask1 >> i & 1) << pos[i];
				ret += (__builtin_popcount(mask1) & 1 ? -1 : 1) * sum1[nmask];
			}
			printf ("%d\n",ret);
		}
	}
    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:7:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    7 |  scanf ("%d %d",&n,&q);
      |  ~~~~~~^~~~~~~~~~~~~~~
snake_escaping.cpp:9:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    9 |   char ch; scanf (" %c",&ch);
      |            ~~~~~~^~~~~~~~~~~
snake_escaping.cpp:19:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   19 |   scanf ("%s",str);
      |   ~~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 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 492 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 384 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 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 331 ms 15084 KB Output is correct
12 Correct 342 ms 14828 KB Output is correct
13 Correct 408 ms 14060 KB Output is correct
14 Correct 436 ms 14080 KB Output is correct
15 Correct 386 ms 15084 KB Output is correct
16 Correct 453 ms 14316 KB Output is correct
17 Correct 445 ms 14316 KB Output is correct
18 Correct 288 ms 16108 KB Output is correct
19 Correct 322 ms 13160 KB Output is correct
20 Correct 356 ms 14828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 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 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 331 ms 15084 KB Output is correct
12 Correct 342 ms 14828 KB Output is correct
13 Correct 408 ms 14060 KB Output is correct
14 Correct 436 ms 14080 KB Output is correct
15 Correct 386 ms 15084 KB Output is correct
16 Correct 453 ms 14316 KB Output is correct
17 Correct 445 ms 14316 KB Output is correct
18 Correct 288 ms 16108 KB Output is correct
19 Correct 322 ms 13160 KB Output is correct
20 Correct 356 ms 14828 KB Output is correct
21 Correct 378 ms 14700 KB Output is correct
22 Correct 396 ms 14828 KB Output is correct
23 Correct 517 ms 14060 KB Output is correct
24 Correct 551 ms 13820 KB Output is correct
25 Correct 436 ms 15852 KB Output is correct
26 Correct 570 ms 14472 KB Output is correct
27 Correct 538 ms 14572 KB Output is correct
28 Correct 313 ms 16876 KB Output is correct
29 Correct 374 ms 13112 KB Output is correct
30 Correct 399 ms 15212 KB Output is correct
31 Correct 511 ms 15212 KB Output is correct
32 Correct 603 ms 15084 KB Output is correct
33 Correct 458 ms 14204 KB Output is correct
34 Correct 549 ms 14188 KB Output is correct
35 Correct 546 ms 14628 KB Output is correct
36 Correct 326 ms 13292 KB Output is correct
37 Correct 364 ms 15340 KB Output is correct
38 Correct 382 ms 13524 KB Output is correct
39 Correct 496 ms 14700 KB Output is correct
40 Correct 548 ms 14496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 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 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 132 ms 14956 KB Output is correct
12 Correct 132 ms 14956 KB Output is correct
13 Correct 159 ms 14828 KB Output is correct
14 Correct 202 ms 14828 KB Output is correct
15 Correct 137 ms 14956 KB Output is correct
16 Correct 218 ms 14984 KB Output is correct
17 Correct 196 ms 14904 KB Output is correct
18 Correct 120 ms 15084 KB Output is correct
19 Correct 133 ms 14748 KB Output is correct
20 Correct 130 ms 14956 KB Output is correct
21 Correct 161 ms 15084 KB Output is correct
22 Correct 194 ms 14828 KB Output is correct
23 Correct 137 ms 14828 KB Output is correct
24 Correct 198 ms 15024 KB Output is correct
25 Correct 193 ms 14956 KB Output is correct
26 Correct 123 ms 14956 KB Output is correct
27 Correct 128 ms 14956 KB Output is correct
28 Correct 130 ms 14828 KB Output is correct
29 Correct 162 ms 14828 KB Output is correct
30 Correct 179 ms 14828 KB Output is correct
31 Correct 142 ms 14812 KB Output is correct
32 Correct 225 ms 14964 KB Output is correct
33 Correct 194 ms 14956 KB Output is correct
34 Correct 121 ms 14828 KB Output is correct
35 Correct 193 ms 14956 KB Output is correct
36 Correct 172 ms 14956 KB Output is correct
37 Correct 176 ms 14956 KB Output is correct
38 Correct 172 ms 14956 KB Output is correct
39 Correct 176 ms 14956 KB Output is correct
40 Correct 174 ms 14956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 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 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 331 ms 15084 KB Output is correct
12 Correct 342 ms 14828 KB Output is correct
13 Correct 408 ms 14060 KB Output is correct
14 Correct 436 ms 14080 KB Output is correct
15 Correct 386 ms 15084 KB Output is correct
16 Correct 453 ms 14316 KB Output is correct
17 Correct 445 ms 14316 KB Output is correct
18 Correct 288 ms 16108 KB Output is correct
19 Correct 322 ms 13160 KB Output is correct
20 Correct 356 ms 14828 KB Output is correct
21 Correct 378 ms 14700 KB Output is correct
22 Correct 396 ms 14828 KB Output is correct
23 Correct 517 ms 14060 KB Output is correct
24 Correct 551 ms 13820 KB Output is correct
25 Correct 436 ms 15852 KB Output is correct
26 Correct 570 ms 14472 KB Output is correct
27 Correct 538 ms 14572 KB Output is correct
28 Correct 313 ms 16876 KB Output is correct
29 Correct 374 ms 13112 KB Output is correct
30 Correct 399 ms 15212 KB Output is correct
31 Correct 511 ms 15212 KB Output is correct
32 Correct 603 ms 15084 KB Output is correct
33 Correct 458 ms 14204 KB Output is correct
34 Correct 549 ms 14188 KB Output is correct
35 Correct 546 ms 14628 KB Output is correct
36 Correct 326 ms 13292 KB Output is correct
37 Correct 364 ms 15340 KB Output is correct
38 Correct 382 ms 13524 KB Output is correct
39 Correct 496 ms 14700 KB Output is correct
40 Correct 548 ms 14496 KB Output is correct
41 Correct 132 ms 14956 KB Output is correct
42 Correct 132 ms 14956 KB Output is correct
43 Correct 159 ms 14828 KB Output is correct
44 Correct 202 ms 14828 KB Output is correct
45 Correct 137 ms 14956 KB Output is correct
46 Correct 218 ms 14984 KB Output is correct
47 Correct 196 ms 14904 KB Output is correct
48 Correct 120 ms 15084 KB Output is correct
49 Correct 133 ms 14748 KB Output is correct
50 Correct 130 ms 14956 KB Output is correct
51 Correct 161 ms 15084 KB Output is correct
52 Correct 194 ms 14828 KB Output is correct
53 Correct 137 ms 14828 KB Output is correct
54 Correct 198 ms 15024 KB Output is correct
55 Correct 193 ms 14956 KB Output is correct
56 Correct 123 ms 14956 KB Output is correct
57 Correct 128 ms 14956 KB Output is correct
58 Correct 130 ms 14828 KB Output is correct
59 Correct 162 ms 14828 KB Output is correct
60 Correct 179 ms 14828 KB Output is correct
61 Correct 142 ms 14812 KB Output is correct
62 Correct 225 ms 14964 KB Output is correct
63 Correct 194 ms 14956 KB Output is correct
64 Correct 121 ms 14828 KB Output is correct
65 Correct 193 ms 14956 KB Output is correct
66 Correct 172 ms 14956 KB Output is correct
67 Correct 176 ms 14956 KB Output is correct
68 Correct 172 ms 14956 KB Output is correct
69 Correct 176 ms 14956 KB Output is correct
70 Correct 174 ms 14956 KB Output is correct
71 Correct 688 ms 39292 KB Output is correct
72 Correct 717 ms 39436 KB Output is correct
73 Correct 1211 ms 37980 KB Output is correct
74 Correct 1911 ms 38484 KB Output is correct
75 Correct 811 ms 40548 KB Output is correct
76 Execution timed out 2093 ms 36368 KB Time limit exceeded
77 Halted 0 ms 0 KB -