Submission #142628

# Submission time Handle Problem Language Result Execution time Memory
142628 2019-08-10T06:46:27 Z DrumpfTheGodEmperor Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 39504 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];
int getMask(vector<int> &pos, int replaceMask) {
	int ansMask = defMask;
	fw (i, 0, pos.size()) {
		int bitID = pos[i];
		int bit = ((replaceMask >> i) & 1);
		ansMask |= (bit << bitID);
	}
	return ansMask;
}
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;
		vector<int> pos; pos.clear();
		if (cntQ <= 6) {
			fw (i, 0, t.length()) {
				if (t[i] == '?') pos.pb(i);
				else defMask |= ((t[i] - '0') << i);
			}
			fw (replacementMask, 0, (1 << cntQ)) {
				int mask = getMask(pos, replacementMask);
				ans += cost[mask];
			}
		} else if (cnt0 <= 6) {
			fw (i, 0, t.length()) {
				if (t[i] == '0') pos.pb(i);
				else {
					if (t[i] == '1') defMask |= (1 << i);
				}
			}
			fw (replacementMask, 0, (1 << cnt0)) {
				int mask = getMask(pos, replacementMask);
				if (bp(replacementMask) % 2 == 0) {
					ans += sumSubsetOf[mask];
				} else ans -= sumSubsetOf[mask];
			}
		} else if (cnt1 <= 6) {
//			cout << "cnt1 = " << cnt1 << "\n";
			fw (i, 0, t.length()) {
				if (t[i] == '1') pos.pb(i);
				else {
					if (t[i] == '?') defMask |= (1 << i);
				}
			}
//			cout << "t = " << t << "\n";
			fw (replacementMask, 0, (1 << cnt1)) {
//				cout << "replacementMask = " << replacementMask << "\n";
				int mask = getMask(pos, replacementMask);
//				cout << "mask = " << mask << "\n";
				if ((cnt1 - bp(replacementMask)) % 2 == 0) {
					ans += sumSubset[mask];
				} else ans -= sumSubset[mask];
			}
		}
		cout << ans << "\n";
	}
	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int getMask(std::vector<int>&, int)':
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:20:6:
  fw (i, 0, pos.size()) {
      ~~~~~~~~~~~~~~~~                  
snake_escaping.cpp:20:2: note: in expansion of macro 'fw'
  fw (i, 0, pos.size()) {
  ^~
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:54:7:
   fw (i, 0, t.length()) {
       ~~~~~~~~~~~~~~~~                 
snake_escaping.cpp:54: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:64:8:
    fw (i, 0, t.length()) {
        ~~~~~~~~~~~~~~~~                
snake_escaping.cpp:64: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:73:8:
    fw (i, 0, t.length()) {
        ~~~~~~~~~~~~~~~~                
snake_escaping.cpp:73: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:87:8:
    fw (i, 0, t.length()) {
        ~~~~~~~~~~~~~~~~                
snake_escaping.cpp:87:4: note: in expansion of macro 'fw'
    fw (i, 0, t.length()) {
    ^~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 392 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 392 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 873 ms 9952 KB Output is correct
12 Correct 810 ms 9716 KB Output is correct
13 Correct 536 ms 8812 KB Output is correct
14 Correct 561 ms 9040 KB Output is correct
15 Correct 1311 ms 9952 KB Output is correct
16 Correct 687 ms 9156 KB Output is correct
17 Correct 659 ms 9048 KB Output is correct
18 Correct 212 ms 10844 KB Output is correct
19 Correct 255 ms 7928 KB Output is correct
20 Correct 785 ms 14880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 392 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 873 ms 9952 KB Output is correct
12 Correct 810 ms 9716 KB Output is correct
13 Correct 536 ms 8812 KB Output is correct
14 Correct 561 ms 9040 KB Output is correct
15 Correct 1311 ms 9952 KB Output is correct
16 Correct 687 ms 9156 KB Output is correct
17 Correct 659 ms 9048 KB Output is correct
18 Correct 212 ms 10844 KB Output is correct
19 Correct 255 ms 7928 KB Output is correct
20 Correct 785 ms 14880 KB Output is correct
21 Correct 397 ms 8640 KB Output is correct
22 Correct 1134 ms 8692 KB Output is correct
23 Correct 737 ms 7652 KB Output is correct
24 Correct 766 ms 7284 KB Output is correct
25 Correct 628 ms 9080 KB Output is correct
26 Correct 874 ms 7564 KB Output is correct
27 Correct 828 ms 7372 KB Output is correct
28 Correct 229 ms 9760 KB Output is correct
29 Correct 304 ms 5760 KB Output is correct
30 Correct 747 ms 7772 KB Output is correct
31 Correct 1382 ms 7500 KB Output is correct
32 Correct 962 ms 7236 KB Output is correct
33 Correct 547 ms 6136 KB Output is correct
34 Correct 705 ms 6244 KB Output is correct
35 Correct 841 ms 17748 KB Output is correct
36 Correct 227 ms 4856 KB Output is correct
37 Correct 1346 ms 6700 KB Output is correct
38 Correct 306 ms 4532 KB Output is correct
39 Correct 708 ms 5612 KB Output is correct
40 Correct 710 ms 5240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 392 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 109 ms 14984 KB Output is correct
12 Correct 118 ms 14992 KB Output is correct
13 Correct 173 ms 15120 KB Output is correct
14 Correct 231 ms 14944 KB Output is correct
15 Correct 132 ms 15096 KB Output is correct
16 Correct 218 ms 15016 KB Output is correct
17 Correct 200 ms 14920 KB Output is correct
18 Correct 95 ms 15124 KB Output is correct
19 Correct 109 ms 14776 KB Output is correct
20 Correct 114 ms 14980 KB Output is correct
21 Correct 159 ms 14948 KB Output is correct
22 Correct 212 ms 14976 KB Output is correct
23 Correct 123 ms 14840 KB Output is correct
24 Correct 186 ms 14968 KB Output is correct
25 Correct 194 ms 14884 KB Output is correct
26 Correct 101 ms 14812 KB Output is correct
27 Correct 109 ms 14892 KB Output is correct
28 Correct 106 ms 14796 KB Output is correct
29 Correct 146 ms 14972 KB Output is correct
30 Correct 179 ms 14968 KB Output is correct
31 Correct 124 ms 14968 KB Output is correct
32 Correct 218 ms 14968 KB Output is correct
33 Correct 198 ms 15000 KB Output is correct
34 Correct 94 ms 14764 KB Output is correct
35 Correct 170 ms 14940 KB Output is correct
36 Correct 172 ms 14900 KB Output is correct
37 Correct 172 ms 15072 KB Output is correct
38 Correct 165 ms 14940 KB Output is correct
39 Correct 170 ms 15072 KB Output is correct
40 Correct 166 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 392 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 873 ms 9952 KB Output is correct
12 Correct 810 ms 9716 KB Output is correct
13 Correct 536 ms 8812 KB Output is correct
14 Correct 561 ms 9040 KB Output is correct
15 Correct 1311 ms 9952 KB Output is correct
16 Correct 687 ms 9156 KB Output is correct
17 Correct 659 ms 9048 KB Output is correct
18 Correct 212 ms 10844 KB Output is correct
19 Correct 255 ms 7928 KB Output is correct
20 Correct 785 ms 14880 KB Output is correct
21 Correct 397 ms 8640 KB Output is correct
22 Correct 1134 ms 8692 KB Output is correct
23 Correct 737 ms 7652 KB Output is correct
24 Correct 766 ms 7284 KB Output is correct
25 Correct 628 ms 9080 KB Output is correct
26 Correct 874 ms 7564 KB Output is correct
27 Correct 828 ms 7372 KB Output is correct
28 Correct 229 ms 9760 KB Output is correct
29 Correct 304 ms 5760 KB Output is correct
30 Correct 747 ms 7772 KB Output is correct
31 Correct 1382 ms 7500 KB Output is correct
32 Correct 962 ms 7236 KB Output is correct
33 Correct 547 ms 6136 KB Output is correct
34 Correct 705 ms 6244 KB Output is correct
35 Correct 841 ms 17748 KB Output is correct
36 Correct 227 ms 4856 KB Output is correct
37 Correct 1346 ms 6700 KB Output is correct
38 Correct 306 ms 4532 KB Output is correct
39 Correct 708 ms 5612 KB Output is correct
40 Correct 710 ms 5240 KB Output is correct
41 Correct 109 ms 14984 KB Output is correct
42 Correct 118 ms 14992 KB Output is correct
43 Correct 173 ms 15120 KB Output is correct
44 Correct 231 ms 14944 KB Output is correct
45 Correct 132 ms 15096 KB Output is correct
46 Correct 218 ms 15016 KB Output is correct
47 Correct 200 ms 14920 KB Output is correct
48 Correct 95 ms 15124 KB Output is correct
49 Correct 109 ms 14776 KB Output is correct
50 Correct 114 ms 14980 KB Output is correct
51 Correct 159 ms 14948 KB Output is correct
52 Correct 212 ms 14976 KB Output is correct
53 Correct 123 ms 14840 KB Output is correct
54 Correct 186 ms 14968 KB Output is correct
55 Correct 194 ms 14884 KB Output is correct
56 Correct 101 ms 14812 KB Output is correct
57 Correct 109 ms 14892 KB Output is correct
58 Correct 106 ms 14796 KB Output is correct
59 Correct 146 ms 14972 KB Output is correct
60 Correct 179 ms 14968 KB Output is correct
61 Correct 124 ms 14968 KB Output is correct
62 Correct 218 ms 14968 KB Output is correct
63 Correct 198 ms 15000 KB Output is correct
64 Correct 94 ms 14764 KB Output is correct
65 Correct 170 ms 14940 KB Output is correct
66 Correct 172 ms 14900 KB Output is correct
67 Correct 172 ms 15072 KB Output is correct
68 Correct 165 ms 14940 KB Output is correct
69 Correct 170 ms 15072 KB Output is correct
70 Correct 166 ms 14940 KB Output is correct
71 Correct 728 ms 39252 KB Output is correct
72 Correct 884 ms 39504 KB Output is correct
73 Correct 1456 ms 37980 KB Output is correct
74 Execution timed out 2023 ms 32144 KB Time limit exceeded
75 Halted 0 ms 0 KB -