답안 #886848

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
886848 2023-12-13T04:00:35 Z socho Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1025 ms 43284 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double D;
typedef vector<int> VI;
typedef set<int> SI;
typedef map<int, int> MII;
typedef pair<int, int> PII;

#define A first
#define B second
#define SZ(x) int(x.size())
#define PB push_back
#define FR(i, a, b) for (int i = (a); i < (b); i++)
#define FOR(i, n) FR(i, 0, n)
#define RF(i, a, b) for (int i = (a); i >= (b); i--)
#define FRA(a, x) for (auto a: (x))

template <typename T> inline void set_min(T &a, T b) {if(b < a) a = b;}
template <typename T> inline void set_max(T &a, T b) {if(b > a) a = b;}

int n, q;
string se;
const int MXN = 20;
int dp[1<<MXN];
int dp2[1<<MXN];
int val[1<<MXN];

int ms[MXN];
int p;

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	
	cin >> n >> q;
	cin >> se;
	
	FOR(i, 1<<n) {
		val[i] = (se[i] - '0');
		dp[i] = dp2[i] = (se[i] - '0');
	}
	
	FOR(b, n) {
		FOR(i, 1<<n) {
			if (i & (1 << b)) dp[i] += dp[i ^ (1 << b)];
			else dp2[i] += dp2[i ^ (1 << b)];
		}
	}
	
	string s;
	FOR(_, q) {
		cin >> s;
		reverse(s.begin(), s.end());
		
		int n0 = 0, n1 = 0, nq = 0;
		FRA(x, s) {
			if (x == '?') nq++;
			else if (x == '0') n0++;
			else n1++;
		}
		
		int ans = 0;
		if (nq <= 6) {
			// just add over the bitmasks
			int main = 0;
			FOR(i, n) {
				if (s[i] == '?') ms[p++] = i;
				else if (s[i] == '1') main += (1 << i);
			}
			FOR(i, 1<<nq) {
				int p = main;
				FOR(j, nq) {
					if (i & (1 << j)) p += (1 << ms[j]);
				}
				ans += val[p];
			}
		}
		else if (n0 <= 6) {
			// 0011??
			// super(001100)
			// -super(011100)
			// -super(101100)
			// +super(111100)
			int main = 0;
			FOR(i, n) {
				if (s[i] == '0') ms[p++] = i;
				else if (s[i] == '1') main += (1 << i);
			}
			FOR(i, 1<<n0) {
				int p = main;
				FOR(j, n0) {
					if (i & (1 << j)) {
						p += (1 << ms[j]);
					}
				}
				if (__builtin_popcount(i) % 2 == 0) ans += dp2[p];
				else ans -= dp2[p];
			}
		}
		else { // n1 <= 6
			// 0011??
			// sub(001111)
			// -sub(001011)
			// -sub(000111)
			// +sub(000011)
			int main = 0;
			FOR(i, n) {
				if (s[i] == '1') ms[p++] = i;
				else if (s[i] == '?') main += (1 << i);
			}
			FOR(i, 1<<n1) {
				int p = main;
				FOR(j, n1) {
					if (i & (1 << j)) p += (1 << ms[j]);
				}
				if ((n1 - __builtin_popcount(i)) % 2 == 0) ans += dp[p];
				else ans -= dp[p];
			}
		}
		cout << ans << '\n';
		p = 0;
		
	}
	
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 367 ms 8396 KB Output is correct
12 Correct 414 ms 8104 KB Output is correct
13 Correct 253 ms 7284 KB Output is correct
14 Correct 246 ms 7760 KB Output is correct
15 Correct 702 ms 8388 KB Output is correct
16 Correct 341 ms 7556 KB Output is correct
17 Correct 342 ms 7560 KB Output is correct
18 Correct 134 ms 9552 KB Output is correct
19 Correct 171 ms 6484 KB Output is correct
20 Correct 375 ms 8020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 367 ms 8396 KB Output is correct
12 Correct 414 ms 8104 KB Output is correct
13 Correct 253 ms 7284 KB Output is correct
14 Correct 246 ms 7760 KB Output is correct
15 Correct 702 ms 8388 KB Output is correct
16 Correct 341 ms 7556 KB Output is correct
17 Correct 342 ms 7560 KB Output is correct
18 Correct 134 ms 9552 KB Output is correct
19 Correct 171 ms 6484 KB Output is correct
20 Correct 375 ms 8020 KB Output is correct
21 Correct 184 ms 8528 KB Output is correct
22 Correct 614 ms 8684 KB Output is correct
23 Correct 370 ms 7760 KB Output is correct
24 Correct 335 ms 8128 KB Output is correct
25 Correct 286 ms 9376 KB Output is correct
26 Correct 463 ms 7980 KB Output is correct
27 Correct 431 ms 8040 KB Output is correct
28 Correct 148 ms 10580 KB Output is correct
29 Correct 199 ms 6480 KB Output is correct
30 Correct 378 ms 8528 KB Output is correct
31 Correct 764 ms 8632 KB Output is correct
32 Correct 469 ms 8788 KB Output is correct
33 Correct 271 ms 7376 KB Output is correct
34 Correct 338 ms 7864 KB Output is correct
35 Correct 430 ms 8016 KB Output is correct
36 Correct 148 ms 6484 KB Output is correct
37 Correct 731 ms 8800 KB Output is correct
38 Correct 210 ms 6484 KB Output is correct
39 Correct 363 ms 7676 KB Output is correct
40 Correct 326 ms 7620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 42 ms 13992 KB Output is correct
12 Correct 40 ms 14584 KB Output is correct
13 Correct 51 ms 16144 KB Output is correct
14 Correct 73 ms 16196 KB Output is correct
15 Correct 49 ms 16148 KB Output is correct
16 Correct 69 ms 16112 KB Output is correct
17 Correct 65 ms 15956 KB Output is correct
18 Correct 31 ms 16112 KB Output is correct
19 Correct 45 ms 15856 KB Output is correct
20 Correct 37 ms 16372 KB Output is correct
21 Correct 52 ms 16116 KB Output is correct
22 Correct 72 ms 16116 KB Output is correct
23 Correct 43 ms 16112 KB Output is correct
24 Correct 67 ms 16112 KB Output is correct
25 Correct 63 ms 16116 KB Output is correct
26 Correct 30 ms 15928 KB Output is correct
27 Correct 36 ms 16216 KB Output is correct
28 Correct 36 ms 15860 KB Output is correct
29 Correct 48 ms 16112 KB Output is correct
30 Correct 66 ms 16112 KB Output is correct
31 Correct 43 ms 16112 KB Output is correct
32 Correct 65 ms 16160 KB Output is correct
33 Correct 58 ms 16112 KB Output is correct
34 Correct 31 ms 15956 KB Output is correct
35 Correct 51 ms 15944 KB Output is correct
36 Correct 52 ms 16096 KB Output is correct
37 Correct 51 ms 16116 KB Output is correct
38 Correct 51 ms 16112 KB Output is correct
39 Correct 51 ms 16116 KB Output is correct
40 Correct 52 ms 16096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 367 ms 8396 KB Output is correct
12 Correct 414 ms 8104 KB Output is correct
13 Correct 253 ms 7284 KB Output is correct
14 Correct 246 ms 7760 KB Output is correct
15 Correct 702 ms 8388 KB Output is correct
16 Correct 341 ms 7556 KB Output is correct
17 Correct 342 ms 7560 KB Output is correct
18 Correct 134 ms 9552 KB Output is correct
19 Correct 171 ms 6484 KB Output is correct
20 Correct 375 ms 8020 KB Output is correct
21 Correct 184 ms 8528 KB Output is correct
22 Correct 614 ms 8684 KB Output is correct
23 Correct 370 ms 7760 KB Output is correct
24 Correct 335 ms 8128 KB Output is correct
25 Correct 286 ms 9376 KB Output is correct
26 Correct 463 ms 7980 KB Output is correct
27 Correct 431 ms 8040 KB Output is correct
28 Correct 148 ms 10580 KB Output is correct
29 Correct 199 ms 6480 KB Output is correct
30 Correct 378 ms 8528 KB Output is correct
31 Correct 764 ms 8632 KB Output is correct
32 Correct 469 ms 8788 KB Output is correct
33 Correct 271 ms 7376 KB Output is correct
34 Correct 338 ms 7864 KB Output is correct
35 Correct 430 ms 8016 KB Output is correct
36 Correct 148 ms 6484 KB Output is correct
37 Correct 731 ms 8800 KB Output is correct
38 Correct 210 ms 6484 KB Output is correct
39 Correct 363 ms 7676 KB Output is correct
40 Correct 326 ms 7620 KB Output is correct
41 Correct 42 ms 13992 KB Output is correct
42 Correct 40 ms 14584 KB Output is correct
43 Correct 51 ms 16144 KB Output is correct
44 Correct 73 ms 16196 KB Output is correct
45 Correct 49 ms 16148 KB Output is correct
46 Correct 69 ms 16112 KB Output is correct
47 Correct 65 ms 15956 KB Output is correct
48 Correct 31 ms 16112 KB Output is correct
49 Correct 45 ms 15856 KB Output is correct
50 Correct 37 ms 16372 KB Output is correct
51 Correct 52 ms 16116 KB Output is correct
52 Correct 72 ms 16116 KB Output is correct
53 Correct 43 ms 16112 KB Output is correct
54 Correct 67 ms 16112 KB Output is correct
55 Correct 63 ms 16116 KB Output is correct
56 Correct 30 ms 15928 KB Output is correct
57 Correct 36 ms 16216 KB Output is correct
58 Correct 36 ms 15860 KB Output is correct
59 Correct 48 ms 16112 KB Output is correct
60 Correct 66 ms 16112 KB Output is correct
61 Correct 43 ms 16112 KB Output is correct
62 Correct 65 ms 16160 KB Output is correct
63 Correct 58 ms 16112 KB Output is correct
64 Correct 31 ms 15956 KB Output is correct
65 Correct 51 ms 15944 KB Output is correct
66 Correct 52 ms 16096 KB Output is correct
67 Correct 51 ms 16116 KB Output is correct
68 Correct 51 ms 16112 KB Output is correct
69 Correct 51 ms 16116 KB Output is correct
70 Correct 52 ms 16096 KB Output is correct
71 Correct 269 ms 40432 KB Output is correct
72 Correct 381 ms 40692 KB Output is correct
73 Correct 555 ms 39284 KB Output is correct
74 Correct 1014 ms 39476 KB Output is correct
75 Correct 473 ms 41716 KB Output is correct
76 Correct 921 ms 39760 KB Output is correct
77 Correct 727 ms 39640 KB Output is correct
78 Correct 192 ms 43284 KB Output is correct
79 Correct 295 ms 37364 KB Output is correct
80 Correct 306 ms 40440 KB Output is correct
81 Correct 643 ms 40448 KB Output is correct
82 Correct 1025 ms 39380 KB Output is correct
83 Correct 415 ms 38644 KB Output is correct
84 Correct 892 ms 39316 KB Output is correct
85 Correct 720 ms 39484 KB Output is correct
86 Correct 199 ms 37360 KB Output is correct
87 Correct 309 ms 40596 KB Output is correct
88 Correct 312 ms 37488 KB Output is correct
89 Correct 546 ms 39152 KB Output is correct
90 Correct 894 ms 39488 KB Output is correct
91 Correct 427 ms 38400 KB Output is correct
92 Correct 930 ms 39808 KB Output is correct
93 Correct 758 ms 39516 KB Output is correct
94 Correct 210 ms 37480 KB Output is correct
95 Correct 601 ms 39784 KB Output is correct
96 Correct 660 ms 39492 KB Output is correct
97 Correct 620 ms 39924 KB Output is correct
98 Correct 605 ms 39408 KB Output is correct
99 Correct 610 ms 39588 KB Output is correct
100 Correct 650 ms 39412 KB Output is correct