답안 #142632

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
142632 2019-08-10T06:56:47 Z DrumpfTheGodEmperor Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1374 ms 42344 KB
#include <bits/stdc++.h>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
		freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int L = (1 << 20) + 5;
int l, q, cost[L], defMask = 0, sumSubset[L], sumSubsetOf[L];
signed main() {
	#ifdef BLU
	in("blu.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> l >> q;
	fw (i, 0, (1 << l)) {
		char c;
		cin >> c;
		cost[i] = c - '0';
	}
	//sumSubset[i] stores sum of costs of j that j & i = j.
	fw (i, 0, (1 << l)) sumSubset[i] = cost[i];
	fw (i, 0, l) fw (mask, 0, (1 << l)) {
		if (mask & (1 << i)) sumSubset[mask] += sumSubset[mask ^ (1 << i)];
	}
//	fw (mask, 0, (1 << l)) cout << "sumSubset[" << mask << "] = " << sumSubset[mask] << "\n";
	//sumSubsetOf[i] stores sum of costs of j that j & i = i
	fw (i, 0, (1 << l)) sumSubsetOf[i] = cost[i];
	fw (i, 0, l) bw (mask, (1 << l), 0) {
		if (!(mask & (1 << i))) sumSubsetOf[mask] += sumSubsetOf[mask | (1 << i)];
	}
	while (q--) {
		string t;
		cin >> t;
		reverse(t.begin(), t.end());
		int cnt0 = 0, cnt1 = 0, cntQ = 0;
		fw (i, 0, t.length()) {
			if (t[i] == '0') cnt0++;
			else if (t[i] == '1') cnt1++;
			else cntQ++;
		}
		//At least one of these is <= 6. Iterate over them.
		int ans = 0;
		defMask = 0;
		int alternateMask = 0;
		if (cntQ <= 6) {
			fw (i, 0, t.length()) {
				if (t[i] == '?') alternateMask |= (1 << i);
				else defMask |= ((t[i] - '0') << i);
			}
			//Iterate through all submasks of alternateMask and OR it into defMask
			for (int tmp = alternateMask; ;tmp = (tmp - 1) & alternateMask) {
				int mask = (defMask | tmp);
				ans += cost[mask];
				if (tmp == 0) break;
			}
		} else if (cnt0 <= 6) {
			fw (i, 0, t.length()) {
				if (t[i] == '0') alternateMask |= (1 << i);
				else {
					if (t[i] == '1') defMask |= (1 << i);
				}
			}
			for (int tmp = alternateMask; ; tmp = (tmp - 1) & alternateMask) {
				int mask = (defMask | tmp);
				if (bp(tmp) % 2 == 0) ans += sumSubsetOf[mask];
				else ans -= sumSubsetOf[mask];
				if (tmp == 0) break;
			}
		} else if (cnt1 <= 6) {
//			cout << "case3\n";
//			cout << "cnt1 = " << cnt1 << "\n";
			fw (i, 0, t.length()) {
				if (t[i] == '1') alternateMask |= (1 << i);
				else {
					if (t[i] == '?') defMask |= (1 << i);
				}
			}
			for (int tmp = alternateMask; ; tmp = (tmp - 1) & alternateMask) {
				int mask = (defMask | tmp);
				if ((cnt1 - bp(tmp)) % 2 == 0) ans += sumSubset[mask];
				else ans -= sumSubset[mask];
				if (tmp == 0) break;
			}
		}
		cout << ans << "\n";
	}
	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:45:7:
   fw (i, 0, t.length()) {
       ~~~~~~~~~~~~~~~~                 
snake_escaping.cpp:45:3: note: in expansion of macro 'fw'
   fw (i, 0, t.length()) {
   ^~
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:55:8:
    fw (i, 0, t.length()) {
        ~~~~~~~~~~~~~~~~                
snake_escaping.cpp:55:4: note: in expansion of macro 'fw'
    fw (i, 0, t.length()) {
    ^~
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:66:8:
    fw (i, 0, t.length()) {
        ~~~~~~~~~~~~~~~~                
snake_escaping.cpp:66:4: note: in expansion of macro 'fw'
    fw (i, 0, t.length()) {
    ^~
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:81:8:
    fw (i, 0, t.length()) {
        ~~~~~~~~~~~~~~~~                
snake_escaping.cpp:81:4: note: in expansion of macro 'fw'
    fw (i, 0, t.length()) {
    ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 372 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 372 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 380 KB Output is correct
11 Correct 307 ms 10428 KB Output is correct
12 Correct 354 ms 10168 KB Output is correct
13 Correct 327 ms 9504 KB Output is correct
14 Correct 314 ms 9464 KB Output is correct
15 Correct 390 ms 10492 KB Output is correct
16 Correct 354 ms 9672 KB Output is correct
17 Correct 357 ms 9644 KB Output is correct
18 Correct 212 ms 11512 KB Output is correct
19 Correct 243 ms 8440 KB Output is correct
20 Correct 352 ms 7032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 372 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 380 KB Output is correct
11 Correct 307 ms 10428 KB Output is correct
12 Correct 354 ms 10168 KB Output is correct
13 Correct 327 ms 9504 KB Output is correct
14 Correct 314 ms 9464 KB Output is correct
15 Correct 390 ms 10492 KB Output is correct
16 Correct 354 ms 9672 KB Output is correct
17 Correct 357 ms 9644 KB Output is correct
18 Correct 212 ms 11512 KB Output is correct
19 Correct 243 ms 8440 KB Output is correct
20 Correct 352 ms 7032 KB Output is correct
21 Correct 373 ms 10184 KB Output is correct
22 Correct 501 ms 10312 KB Output is correct
23 Correct 399 ms 9468 KB Output is correct
24 Correct 385 ms 9272 KB Output is correct
25 Correct 410 ms 11316 KB Output is correct
26 Correct 423 ms 9644 KB Output is correct
27 Correct 416 ms 9640 KB Output is correct
28 Correct 221 ms 12280 KB Output is correct
29 Correct 283 ms 8160 KB Output is correct
30 Correct 405 ms 10192 KB Output is correct
31 Correct 459 ms 9916 KB Output is correct
32 Correct 414 ms 9720 KB Output is correct
33 Correct 335 ms 8580 KB Output is correct
34 Correct 388 ms 8696 KB Output is correct
35 Correct 414 ms 6900 KB Output is correct
36 Correct 211 ms 7416 KB Output is correct
37 Correct 428 ms 9264 KB Output is correct
38 Correct 291 ms 7316 KB Output is correct
39 Correct 381 ms 8116 KB Output is correct
40 Correct 383 ms 7800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 372 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 380 KB Output is correct
11 Correct 108 ms 15084 KB Output is correct
12 Correct 110 ms 14968 KB Output is correct
13 Correct 112 ms 14840 KB Output is correct
14 Correct 137 ms 14840 KB Output is correct
15 Correct 116 ms 14984 KB Output is correct
16 Correct 126 ms 14964 KB Output is correct
17 Correct 128 ms 14872 KB Output is correct
18 Correct 94 ms 15096 KB Output is correct
19 Correct 103 ms 14844 KB Output is correct
20 Correct 109 ms 14988 KB Output is correct
21 Correct 118 ms 14968 KB Output is correct
22 Correct 133 ms 14840 KB Output is correct
23 Correct 110 ms 14844 KB Output is correct
24 Correct 118 ms 14840 KB Output is correct
25 Correct 131 ms 14840 KB Output is correct
26 Correct 94 ms 14840 KB Output is correct
27 Correct 110 ms 14956 KB Output is correct
28 Correct 102 ms 14840 KB Output is correct
29 Correct 112 ms 14856 KB Output is correct
30 Correct 114 ms 14840 KB Output is correct
31 Correct 105 ms 14868 KB Output is correct
32 Correct 141 ms 14940 KB Output is correct
33 Correct 131 ms 14840 KB Output is correct
34 Correct 94 ms 14776 KB Output is correct
35 Correct 123 ms 14852 KB Output is correct
36 Correct 119 ms 14968 KB Output is correct
37 Correct 121 ms 14852 KB Output is correct
38 Correct 121 ms 14968 KB Output is correct
39 Correct 121 ms 14840 KB Output is correct
40 Correct 130 ms 14908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 372 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 380 KB Output is correct
11 Correct 307 ms 10428 KB Output is correct
12 Correct 354 ms 10168 KB Output is correct
13 Correct 327 ms 9504 KB Output is correct
14 Correct 314 ms 9464 KB Output is correct
15 Correct 390 ms 10492 KB Output is correct
16 Correct 354 ms 9672 KB Output is correct
17 Correct 357 ms 9644 KB Output is correct
18 Correct 212 ms 11512 KB Output is correct
19 Correct 243 ms 8440 KB Output is correct
20 Correct 352 ms 7032 KB Output is correct
21 Correct 373 ms 10184 KB Output is correct
22 Correct 501 ms 10312 KB Output is correct
23 Correct 399 ms 9468 KB Output is correct
24 Correct 385 ms 9272 KB Output is correct
25 Correct 410 ms 11316 KB Output is correct
26 Correct 423 ms 9644 KB Output is correct
27 Correct 416 ms 9640 KB Output is correct
28 Correct 221 ms 12280 KB Output is correct
29 Correct 283 ms 8160 KB Output is correct
30 Correct 405 ms 10192 KB Output is correct
31 Correct 459 ms 9916 KB Output is correct
32 Correct 414 ms 9720 KB Output is correct
33 Correct 335 ms 8580 KB Output is correct
34 Correct 388 ms 8696 KB Output is correct
35 Correct 414 ms 6900 KB Output is correct
36 Correct 211 ms 7416 KB Output is correct
37 Correct 428 ms 9264 KB Output is correct
38 Correct 291 ms 7316 KB Output is correct
39 Correct 381 ms 8116 KB Output is correct
40 Correct 383 ms 7800 KB Output is correct
41 Correct 108 ms 15084 KB Output is correct
42 Correct 110 ms 14968 KB Output is correct
43 Correct 112 ms 14840 KB Output is correct
44 Correct 137 ms 14840 KB Output is correct
45 Correct 116 ms 14984 KB Output is correct
46 Correct 126 ms 14964 KB Output is correct
47 Correct 128 ms 14872 KB Output is correct
48 Correct 94 ms 15096 KB Output is correct
49 Correct 103 ms 14844 KB Output is correct
50 Correct 109 ms 14988 KB Output is correct
51 Correct 118 ms 14968 KB Output is correct
52 Correct 133 ms 14840 KB Output is correct
53 Correct 110 ms 14844 KB Output is correct
54 Correct 118 ms 14840 KB Output is correct
55 Correct 131 ms 14840 KB Output is correct
56 Correct 94 ms 14840 KB Output is correct
57 Correct 110 ms 14956 KB Output is correct
58 Correct 102 ms 14840 KB Output is correct
59 Correct 112 ms 14856 KB Output is correct
60 Correct 114 ms 14840 KB Output is correct
61 Correct 105 ms 14868 KB Output is correct
62 Correct 141 ms 14940 KB Output is correct
63 Correct 131 ms 14840 KB Output is correct
64 Correct 94 ms 14776 KB Output is correct
65 Correct 123 ms 14852 KB Output is correct
66 Correct 119 ms 14968 KB Output is correct
67 Correct 121 ms 14852 KB Output is correct
68 Correct 121 ms 14968 KB Output is correct
69 Correct 121 ms 14840 KB Output is correct
70 Correct 130 ms 14908 KB Output is correct
71 Correct 676 ms 17944 KB Output is correct
72 Correct 704 ms 17968 KB Output is correct
73 Correct 739 ms 16504 KB Output is correct
74 Correct 1187 ms 23936 KB Output is correct
75 Correct 719 ms 40492 KB Output is correct
76 Correct 1283 ms 38600 KB Output is correct
77 Correct 1146 ms 38492 KB Output is correct
78 Correct 393 ms 42344 KB Output is correct
79 Correct 547 ms 36232 KB Output is correct
80 Correct 700 ms 39460 KB Output is correct
81 Correct 925 ms 39284 KB Output is correct
82 Correct 1374 ms 38120 KB Output is correct
83 Correct 583 ms 37504 KB Output is correct
84 Correct 766 ms 38296 KB Output is correct
85 Correct 1138 ms 38512 KB Output is correct
86 Correct 386 ms 36212 KB Output is correct
87 Correct 667 ms 39284 KB Output is correct
88 Correct 559 ms 36256 KB Output is correct
89 Correct 734 ms 37872 KB Output is correct
90 Correct 758 ms 38228 KB Output is correct
91 Correct 601 ms 37460 KB Output is correct
92 Correct 1253 ms 38816 KB Output is correct
93 Correct 1111 ms 38396 KB Output is correct
94 Correct 363 ms 36080 KB Output is correct
95 Correct 979 ms 38408 KB Output is correct
96 Correct 1039 ms 38460 KB Output is correct
97 Correct 1000 ms 38468 KB Output is correct
98 Correct 983 ms 38304 KB Output is correct
99 Correct 989 ms 38276 KB Output is correct
100 Correct 1093 ms 38388 KB Output is correct