Submission #42163

# Submission time Handle Problem Language Result Execution time Memory
42163 2018-02-23T05:32:16 Z fest Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 65536 KB
// fest
#include <bits/stdc++.h>	

#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define FILENAME ""
#define __ fflush(stdout)

typedef long long ll;
typedef long double ld;    

using namespace std;

void FREOPEN() {
	#ifdef COMP
		freopen(".in", "r", stdin);
		freopen("1.out", "w", stdout);
	#else
		freopen(FILENAME".in", "r", stdin);
		freopen(FILENAME".out", "w", stdout);
	#endif
}                           

inline double Time() {return (clock() * 1.0) / CLOCKS_PER_SEC; }             

const int N = 20, inf = 1e9 * 2, MOD = (int)1e9 + 7;

char CH[N];

const ll INF = 1e18;

const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};

int ones[N], dp[2][2][1 << N], cost[2][1 << N], qs[N];

bool built[1 << N];

int n;

inline int rev(int x) {
	int ret = 0;
	for (int i = 0; i < n; i++) {
		if (x & (1 << i)) continue;
		ret |= (1 << i);
	} 
	return ret;
}

void build(int ty) {
	for (int mask = 0; mask < (1 << n); mask++) {
		dp[ty][0][mask] += cost[ty][mask];
		if (mask & 1) dp[ty][0][mask] += cost[ty][mask ^ 1];
	}		
	for (int bit = 1; bit < n; bit++) {
		int j = (bit & 1);
		for (int mask = 0; mask < (1 << n); mask++) {
			dp[ty][j][mask] = dp[ty][j ^ 1][mask];
			if (mask & (1 << bit)) dp[ty][j][mask] += dp[ty][j ^ 1][mask ^ (1 << bit)];
		}
	}
}

inline int calc(int ty, int cnt) {
	int ret = 0;
	int alw = 0;
	int r = 0;
	for (int i = 0; i < n; i++) {
		if (qs[i] == 2) alw |= (1 << i);
		if (qs[i] == 1) ones[r++] = i;
	}
	for (int mask = 0; mask < (1 << cnt); mask++) {
		int now = alw;
		for (int i = 0; i < r; i++) {
			if (mask & (1 << i)) now |= (1 << ones[i]);
		}
		if (built[mask]) ret -= dp[ty][(n - 1) & 1][now];
		else ret += dp[ty][(n - 1) & 1][now];
	}	
	return abs(ret);
} 

int main() {
  
	int q;
	cin >> n >> q;
	scanf("\n");
	for (int i = 0; i < (1 << n); i++) {
		char x = getchar();
		cost[0][i] = (int)(x - '0');
		cost[1][rev(i)] = (int)(x - '0');
	} 
	for (int mask = 0; mask < (1 << n); mask++) built[mask] = (__builtin_popcount(mask) & 1);
	build(0);
	build(1);
	while (q--) {
		getchar();
		int zer = 0, one = 0, que = 0;
		for (int i = n - 1; i >= 0; i--) {
			char x = getchar();
			if (x == '?') qs[i] = 2, que++;
			if (x == '1') qs[i] = 1, one++;
			if (x == '0') qs[i] = 0, zer++;
		}
		if (!one) {
			int ret = 0;
			for (int i = 0; i < n; i++) {
				if (qs[i] == 2) ret |= (1 << i);
			}
			printf("%d\n", dp[0][(n - 1) & 1][ret]);
			continue;
		}
		if (!zer) {
			int ret = 0;
			for (int i = 0; i < n; i++) {
				if (qs[i] == 2) ret |= (1 << i);
			}
			printf("%d\n", dp[1][(n - 1) & 1][ret]);
			continue;
		}
		if (que < 7) {
			int ans = 0;
			int r = 0;
			int alw = 0;
			for (int i = 0; i < n; i++) {
				if (qs[i] == 2) ones[r++] = i; 
				if (qs[i] == 1) alw |= (1 << i);
			}
			for (int mask = 0; mask < (1 << que); mask++) {
				int ret = alw;
				for (int i = 0; i < r; i++) {
					if (mask & (1 << i)) ret |= (1 << ones[i]);
				}
				ans += cost[0][ret];
			}
			printf("%d\n", ans);
			continue;
		}
		if (one < 7) {
			printf("%d\n", calc(0, one));
			continue;
		}
		for (int i = 0; i < n; i++) if (qs[i] < 2) qs[i] ^= 1;
		printf("%d\n", calc(1, zer));
	}
	return 0;	
}

Compilation message

snake_escaping.cpp: In function 'void FREOPEN()':
snake_escaping.cpp:25:37: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(FILENAME".in", "r", stdin);
                                     ^
snake_escaping.cpp:26:39: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(FILENAME".out", "w", stdout);
                                       ^
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:93:13: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("\n");
             ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 668 KB Output is correct
4 Correct 2 ms 668 KB Output is correct
5 Correct 3 ms 668 KB Output is correct
6 Correct 2 ms 668 KB Output is correct
7 Correct 2 ms 668 KB Output is correct
8 Correct 2 ms 668 KB Output is correct
9 Correct 2 ms 668 KB Output is correct
10 Correct 2 ms 668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 668 KB Output is correct
4 Correct 2 ms 668 KB Output is correct
5 Correct 3 ms 668 KB Output is correct
6 Correct 2 ms 668 KB Output is correct
7 Correct 2 ms 668 KB Output is correct
8 Correct 2 ms 668 KB Output is correct
9 Correct 2 ms 668 KB Output is correct
10 Correct 2 ms 668 KB Output is correct
11 Correct 297 ms 7564 KB Output is correct
12 Correct 321 ms 17940 KB Output is correct
13 Correct 400 ms 27980 KB Output is correct
14 Correct 397 ms 37284 KB Output is correct
15 Correct 859 ms 38432 KB Output is correct
16 Correct 493 ms 38432 KB Output is correct
17 Correct 486 ms 38432 KB Output is correct
18 Correct 218 ms 39164 KB Output is correct
19 Correct 300 ms 39164 KB Output is correct
20 Correct 321 ms 39164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 668 KB Output is correct
4 Correct 2 ms 668 KB Output is correct
5 Correct 3 ms 668 KB Output is correct
6 Correct 2 ms 668 KB Output is correct
7 Correct 2 ms 668 KB Output is correct
8 Correct 2 ms 668 KB Output is correct
9 Correct 2 ms 668 KB Output is correct
10 Correct 2 ms 668 KB Output is correct
11 Correct 297 ms 7564 KB Output is correct
12 Correct 321 ms 17940 KB Output is correct
13 Correct 400 ms 27980 KB Output is correct
14 Correct 397 ms 37284 KB Output is correct
15 Correct 859 ms 38432 KB Output is correct
16 Correct 493 ms 38432 KB Output is correct
17 Correct 486 ms 38432 KB Output is correct
18 Correct 218 ms 39164 KB Output is correct
19 Correct 300 ms 39164 KB Output is correct
20 Correct 321 ms 39164 KB Output is correct
21 Correct 382 ms 39164 KB Output is correct
22 Correct 396 ms 52928 KB Output is correct
23 Correct 569 ms 65536 KB Output is correct
24 Correct 564 ms 65536 KB Output is correct
25 Correct 493 ms 65536 KB Output is correct
26 Correct 672 ms 65536 KB Output is correct
27 Correct 682 ms 65536 KB Output is correct
28 Correct 287 ms 65536 KB Output is correct
29 Correct 404 ms 65536 KB Output is correct
30 Correct 399 ms 65536 KB Output is correct
31 Correct 1033 ms 65536 KB Output is correct
32 Correct 707 ms 65536 KB Output is correct
33 Correct 445 ms 65536 KB Output is correct
34 Correct 550 ms 65536 KB Output is correct
35 Correct 672 ms 65536 KB Output is correct
36 Correct 272 ms 65536 KB Output is correct
37 Correct 371 ms 65536 KB Output is correct
38 Correct 399 ms 65536 KB Output is correct
39 Correct 525 ms 65536 KB Output is correct
40 Correct 536 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 668 KB Output is correct
4 Correct 2 ms 668 KB Output is correct
5 Correct 3 ms 668 KB Output is correct
6 Correct 2 ms 668 KB Output is correct
7 Correct 2 ms 668 KB Output is correct
8 Correct 2 ms 668 KB Output is correct
9 Correct 2 ms 668 KB Output is correct
10 Correct 2 ms 668 KB Output is correct
11 Correct 216 ms 65536 KB Output is correct
12 Correct 206 ms 65536 KB Output is correct
13 Correct 219 ms 65536 KB Output is correct
14 Correct 240 ms 65536 KB Output is correct
15 Correct 211 ms 65536 KB Output is correct
16 Correct 256 ms 65536 KB Output is correct
17 Correct 250 ms 65536 KB Output is correct
18 Correct 191 ms 65536 KB Output is correct
19 Correct 202 ms 65536 KB Output is correct
20 Correct 215 ms 65536 KB Output is correct
21 Correct 263 ms 65536 KB Output is correct
22 Correct 253 ms 65536 KB Output is correct
23 Correct 214 ms 65536 KB Output is correct
24 Correct 238 ms 65536 KB Output is correct
25 Correct 247 ms 65536 KB Output is correct
26 Correct 187 ms 65536 KB Output is correct
27 Correct 215 ms 65536 KB Output is correct
28 Correct 207 ms 65536 KB Output is correct
29 Correct 239 ms 65536 KB Output is correct
30 Correct 239 ms 65536 KB Output is correct
31 Correct 205 ms 65536 KB Output is correct
32 Correct 245 ms 65536 KB Output is correct
33 Correct 242 ms 65536 KB Output is correct
34 Correct 191 ms 65536 KB Output is correct
35 Correct 227 ms 65536 KB Output is correct
36 Correct 231 ms 65536 KB Output is correct
37 Correct 237 ms 65536 KB Output is correct
38 Correct 231 ms 65536 KB Output is correct
39 Correct 240 ms 65536 KB Output is correct
40 Correct 236 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 668 KB Output is correct
4 Correct 2 ms 668 KB Output is correct
5 Correct 3 ms 668 KB Output is correct
6 Correct 2 ms 668 KB Output is correct
7 Correct 2 ms 668 KB Output is correct
8 Correct 2 ms 668 KB Output is correct
9 Correct 2 ms 668 KB Output is correct
10 Correct 2 ms 668 KB Output is correct
11 Correct 297 ms 7564 KB Output is correct
12 Correct 321 ms 17940 KB Output is correct
13 Correct 400 ms 27980 KB Output is correct
14 Correct 397 ms 37284 KB Output is correct
15 Correct 859 ms 38432 KB Output is correct
16 Correct 493 ms 38432 KB Output is correct
17 Correct 486 ms 38432 KB Output is correct
18 Correct 218 ms 39164 KB Output is correct
19 Correct 300 ms 39164 KB Output is correct
20 Correct 321 ms 39164 KB Output is correct
21 Correct 382 ms 39164 KB Output is correct
22 Correct 396 ms 52928 KB Output is correct
23 Correct 569 ms 65536 KB Output is correct
24 Correct 564 ms 65536 KB Output is correct
25 Correct 493 ms 65536 KB Output is correct
26 Correct 672 ms 65536 KB Output is correct
27 Correct 682 ms 65536 KB Output is correct
28 Correct 287 ms 65536 KB Output is correct
29 Correct 404 ms 65536 KB Output is correct
30 Correct 399 ms 65536 KB Output is correct
31 Correct 1033 ms 65536 KB Output is correct
32 Correct 707 ms 65536 KB Output is correct
33 Correct 445 ms 65536 KB Output is correct
34 Correct 550 ms 65536 KB Output is correct
35 Correct 672 ms 65536 KB Output is correct
36 Correct 272 ms 65536 KB Output is correct
37 Correct 371 ms 65536 KB Output is correct
38 Correct 399 ms 65536 KB Output is correct
39 Correct 525 ms 65536 KB Output is correct
40 Correct 536 ms 65536 KB Output is correct
41 Correct 216 ms 65536 KB Output is correct
42 Correct 206 ms 65536 KB Output is correct
43 Correct 219 ms 65536 KB Output is correct
44 Correct 240 ms 65536 KB Output is correct
45 Correct 211 ms 65536 KB Output is correct
46 Correct 256 ms 65536 KB Output is correct
47 Correct 250 ms 65536 KB Output is correct
48 Correct 191 ms 65536 KB Output is correct
49 Correct 202 ms 65536 KB Output is correct
50 Correct 215 ms 65536 KB Output is correct
51 Correct 263 ms 65536 KB Output is correct
52 Correct 253 ms 65536 KB Output is correct
53 Correct 214 ms 65536 KB Output is correct
54 Correct 238 ms 65536 KB Output is correct
55 Correct 247 ms 65536 KB Output is correct
56 Correct 187 ms 65536 KB Output is correct
57 Correct 215 ms 65536 KB Output is correct
58 Correct 207 ms 65536 KB Output is correct
59 Correct 239 ms 65536 KB Output is correct
60 Correct 239 ms 65536 KB Output is correct
61 Correct 205 ms 65536 KB Output is correct
62 Correct 245 ms 65536 KB Output is correct
63 Correct 242 ms 65536 KB Output is correct
64 Correct 191 ms 65536 KB Output is correct
65 Correct 227 ms 65536 KB Output is correct
66 Correct 231 ms 65536 KB Output is correct
67 Correct 237 ms 65536 KB Output is correct
68 Correct 231 ms 65536 KB Output is correct
69 Correct 240 ms 65536 KB Output is correct
70 Correct 236 ms 65536 KB Output is correct
71 Correct 762 ms 65536 KB Output is correct
72 Correct 784 ms 65536 KB Output is correct
73 Correct 1080 ms 65536 KB Output is correct
74 Correct 1504 ms 65536 KB Output is correct
75 Correct 913 ms 65536 KB Output is correct
76 Correct 1998 ms 65536 KB Output is correct
77 Correct 1854 ms 65536 KB Output is correct
78 Correct 525 ms 65536 KB Output is correct
79 Correct 800 ms 65536 KB Output is correct
80 Correct 813 ms 65536 KB Output is correct
81 Correct 1270 ms 65536 KB Output is correct
82 Correct 1831 ms 65536 KB Output is correct
83 Correct 885 ms 65536 KB Output is correct
84 Correct 1731 ms 65536 KB Output is correct
85 Execution timed out 2031 ms 65536 KB Time limit exceeded
86 Halted 0 ms 0 KB -