답안 #1098860

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1098860 2024-10-10T08:49:23 Z trie Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1625 ms 39388 KB
#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define ii pair<int, int>
#define iii pair<ii, int>
#define ll long long
#define pb push_back
#define pli pair<ll, int>
#define all(v) (v).begin(), (v).end()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define cntbit(mask) __builtin_popcount(mask)
#define getbit(x, i) ((x >> i) & 1)
#define MASK(i) (1 << i)
#define pli pair<ll, int>
using namespace std;

template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return 1;} return 0;}

template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return 1;} return 0;}

const int N = 1e5 + 5;
const int V = 1e6 + 5;
const int MOD = 1e9 + 7;

int n, q, cost[MASK(20) + 5], p[MASK(20) + 5];
string s;

void solve() { 
	cin >> n >> q;
	
	cin >> s;
	
	for(int i = 0 ; i < (1 << n) ; i++) p[i] = cost[i] = (s[i] - '0');
	
	for(int i = 0 ; i < n ; i++)
		for(int mask = 0 ; mask < (1 << n) ; mask++)
			if (getbit(mask, i)) cost[mask] += cost[mask ^ MASK(i)];
			
	for(int i = 0 ; i < q ; i++) {
		string t;	cin >> t;
		reverse(all(t));
		
		int mask1, mask2;
		mask1 = mask2 = 0;
		
		for(int i = 0 ; i < n ; i++) 
			if (t[i] == '1') mask1 |= MASK(i);
			else if (t[i] == '?') mask2 |= MASK(i);
			
		int ans = 0;

		if (cntbit(mask2) <= 10) {
			for(int submask = mask2 ; submask >= 0 ; submask = (submask - 1) & mask2) {
				ans += p[mask1 + submask];
				if (submask == 0) break;
			}
		} else {
			int c = cntbit(mask1);
			for(int submask = mask1 ; submask >= 0 ; submask = (submask - 1) & mask1) {
				if (cntbit(submask) % 2 == c % 2) ans += cost[submask + mask2];
				else ans -= cost[submask + mask2];
				
				if (submask == 0) break;
			}
		}
		
		cout << ans << '\n';
	}
} 

int main(void) {
    //freopen("task.inp", "r", stdin);
    //freopen("task.out", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
    int t = 1; while(t--) solve();
    return 0;
}
 
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 195 ms 14944 KB Output is correct
12 Correct 184 ms 14460 KB Output is correct
13 Correct 136 ms 13704 KB Output is correct
14 Correct 174 ms 13840 KB Output is correct
15 Correct 177 ms 14932 KB Output is correct
16 Correct 164 ms 14020 KB Output is correct
17 Correct 180 ms 14032 KB Output is correct
18 Correct 1083 ms 15956 KB Output is correct
19 Correct 137 ms 12824 KB Output is correct
20 Correct 193 ms 14528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 195 ms 14944 KB Output is correct
12 Correct 184 ms 14460 KB Output is correct
13 Correct 136 ms 13704 KB Output is correct
14 Correct 174 ms 13840 KB Output is correct
15 Correct 177 ms 14932 KB Output is correct
16 Correct 164 ms 14020 KB Output is correct
17 Correct 180 ms 14032 KB Output is correct
18 Correct 1083 ms 15956 KB Output is correct
19 Correct 137 ms 12824 KB Output is correct
20 Correct 193 ms 14528 KB Output is correct
21 Correct 279 ms 17728 KB Output is correct
22 Correct 274 ms 18360 KB Output is correct
23 Correct 153 ms 17236 KB Output is correct
24 Correct 155 ms 17232 KB Output is correct
25 Correct 407 ms 19136 KB Output is correct
26 Correct 240 ms 17556 KB Output is correct
27 Correct 231 ms 17540 KB Output is correct
28 Correct 134 ms 20048 KB Output is correct
29 Correct 156 ms 16052 KB Output is correct
30 Correct 339 ms 18256 KB Output is correct
31 Correct 208 ms 17996 KB Output is correct
32 Correct 180 ms 18004 KB Output is correct
33 Correct 171 ms 17088 KB Output is correct
34 Correct 177 ms 17236 KB Output is correct
35 Correct 208 ms 17496 KB Output is correct
36 Correct 126 ms 15976 KB Output is correct
37 Correct 172 ms 18116 KB Output is correct
38 Correct 171 ms 16212 KB Output is correct
39 Correct 176 ms 17232 KB Output is correct
40 Correct 156 ms 17232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 94 ms 12020 KB Output is correct
12 Correct 60 ms 12016 KB Output is correct
13 Correct 35 ms 11944 KB Output is correct
14 Correct 41 ms 12016 KB Output is correct
15 Correct 32 ms 12016 KB Output is correct
16 Correct 49 ms 11908 KB Output is correct
17 Correct 45 ms 12008 KB Output is correct
18 Correct 32 ms 12024 KB Output is correct
19 Correct 34 ms 12028 KB Output is correct
20 Correct 72 ms 12020 KB Output is correct
21 Correct 103 ms 12020 KB Output is correct
22 Correct 41 ms 12016 KB Output is correct
23 Correct 33 ms 11764 KB Output is correct
24 Correct 45 ms 12128 KB Output is correct
25 Correct 45 ms 11956 KB Output is correct
26 Correct 28 ms 11856 KB Output is correct
27 Correct 108 ms 12020 KB Output is correct
28 Correct 38 ms 11812 KB Output is correct
29 Correct 39 ms 12020 KB Output is correct
30 Correct 39 ms 12016 KB Output is correct
31 Correct 34 ms 11760 KB Output is correct
32 Correct 52 ms 12020 KB Output is correct
33 Correct 59 ms 11964 KB Output is correct
34 Correct 31 ms 11772 KB Output is correct
35 Correct 73 ms 12016 KB Output is correct
36 Correct 50 ms 12012 KB Output is correct
37 Correct 59 ms 12016 KB Output is correct
38 Correct 52 ms 12016 KB Output is correct
39 Correct 48 ms 12016 KB Output is correct
40 Correct 69 ms 12020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 195 ms 14944 KB Output is correct
12 Correct 184 ms 14460 KB Output is correct
13 Correct 136 ms 13704 KB Output is correct
14 Correct 174 ms 13840 KB Output is correct
15 Correct 177 ms 14932 KB Output is correct
16 Correct 164 ms 14020 KB Output is correct
17 Correct 180 ms 14032 KB Output is correct
18 Correct 1083 ms 15956 KB Output is correct
19 Correct 137 ms 12824 KB Output is correct
20 Correct 193 ms 14528 KB Output is correct
21 Correct 279 ms 17728 KB Output is correct
22 Correct 274 ms 18360 KB Output is correct
23 Correct 153 ms 17236 KB Output is correct
24 Correct 155 ms 17232 KB Output is correct
25 Correct 407 ms 19136 KB Output is correct
26 Correct 240 ms 17556 KB Output is correct
27 Correct 231 ms 17540 KB Output is correct
28 Correct 134 ms 20048 KB Output is correct
29 Correct 156 ms 16052 KB Output is correct
30 Correct 339 ms 18256 KB Output is correct
31 Correct 208 ms 17996 KB Output is correct
32 Correct 180 ms 18004 KB Output is correct
33 Correct 171 ms 17088 KB Output is correct
34 Correct 177 ms 17236 KB Output is correct
35 Correct 208 ms 17496 KB Output is correct
36 Correct 126 ms 15976 KB Output is correct
37 Correct 172 ms 18116 KB Output is correct
38 Correct 171 ms 16212 KB Output is correct
39 Correct 176 ms 17232 KB Output is correct
40 Correct 156 ms 17232 KB Output is correct
41 Correct 94 ms 12020 KB Output is correct
42 Correct 60 ms 12016 KB Output is correct
43 Correct 35 ms 11944 KB Output is correct
44 Correct 41 ms 12016 KB Output is correct
45 Correct 32 ms 12016 KB Output is correct
46 Correct 49 ms 11908 KB Output is correct
47 Correct 45 ms 12008 KB Output is correct
48 Correct 32 ms 12024 KB Output is correct
49 Correct 34 ms 12028 KB Output is correct
50 Correct 72 ms 12020 KB Output is correct
51 Correct 103 ms 12020 KB Output is correct
52 Correct 41 ms 12016 KB Output is correct
53 Correct 33 ms 11764 KB Output is correct
54 Correct 45 ms 12128 KB Output is correct
55 Correct 45 ms 11956 KB Output is correct
56 Correct 28 ms 11856 KB Output is correct
57 Correct 108 ms 12020 KB Output is correct
58 Correct 38 ms 11812 KB Output is correct
59 Correct 39 ms 12020 KB Output is correct
60 Correct 39 ms 12016 KB Output is correct
61 Correct 34 ms 11760 KB Output is correct
62 Correct 52 ms 12020 KB Output is correct
63 Correct 59 ms 11964 KB Output is correct
64 Correct 31 ms 11772 KB Output is correct
65 Correct 73 ms 12016 KB Output is correct
66 Correct 50 ms 12012 KB Output is correct
67 Correct 59 ms 12016 KB Output is correct
68 Correct 52 ms 12016 KB Output is correct
69 Correct 48 ms 12016 KB Output is correct
70 Correct 69 ms 12020 KB Output is correct
71 Correct 1559 ms 36188 KB Output is correct
72 Correct 613 ms 36336 KB Output is correct
73 Correct 304 ms 34884 KB Output is correct
74 Correct 464 ms 35312 KB Output is correct
75 Correct 275 ms 37360 KB Output is correct
76 Correct 549 ms 35568 KB Output is correct
77 Correct 624 ms 35308 KB Output is correct
78 Correct 203 ms 39388 KB Output is correct
79 Correct 260 ms 33272 KB Output is correct
80 Correct 1103 ms 36480 KB Output is correct
81 Correct 1625 ms 36192 KB Output is correct
82 Correct 451 ms 35152 KB Output is correct
83 Correct 252 ms 34436 KB Output is correct
84 Correct 337 ms 35140 KB Output is correct
85 Correct 530 ms 35364 KB Output is correct
86 Correct 170 ms 33268 KB Output is correct
87 Correct 1549 ms 36256 KB Output is correct
88 Correct 282 ms 33140 KB Output is correct
89 Correct 388 ms 35060 KB Output is correct
90 Correct 359 ms 35316 KB Output is correct
91 Correct 241 ms 34296 KB Output is correct
92 Correct 571 ms 35572 KB Output is correct
93 Correct 555 ms 35404 KB Output is correct
94 Correct 166 ms 33100 KB Output is correct
95 Correct 600 ms 35308 KB Output is correct
96 Correct 569 ms 35200 KB Output is correct
97 Correct 565 ms 35296 KB Output is correct
98 Correct 550 ms 35412 KB Output is correct
99 Correct 594 ms 35312 KB Output is correct
100 Correct 556 ms 35316 KB Output is correct