답안 #952730

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
952730 2024-03-24T16:21:17 Z jmyszka2007 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1308 ms 39208 KB
#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {return o << '(' << p.first << ", " << p.second << ')';}
template<size_t Index = 0, typename... Types>
ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;}
template<typename... Types>
ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';}
struct custom_hash {static uint64_t splitmix64(uint64_t x) {x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x = (x ^ (x >> 27)) * 0x94d049bb133111eb;return x ^ (x >> 31);}
size_t operator()(uint64_t x) const {static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}};
//#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#endif
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define pi pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
void solve() {
	//ifstream cin("nazwa.in");
	//ofstream cout("nazwa.out");
	int l, t;
	cin >> l >> t;
	string c;
	cin >> c;
	vi dpdol((1 << l), 0), dpgora((1 << l), 0);
	for(int i = 0; i < (1 << l); i++) {
		dpdol[i] = (c[i] - '0');
		dpgora[i] = (c[i] - '0');
	}
	for(int i = 0; i < l; i++) {
		for(int mask = 0; mask < (1 << l); mask++) {
			if(mask & (1 << i)) {
				dpdol[mask] += dpdol[mask ^ (1 << i)];
			}
			else {
				dpgora[mask] += dpgora[mask ^ (1 << i)];
			}
		}
	}
	while(t--) {
		string a;
		cin >> a;
		reverse(all(a));
		int ilezap = 0, ilezer = 0, ilejed = 0;
		for(auto x : a) {
			if(x == '?') {
				ilezap++;
			}
			else if(x == '0') {
				ilezer++;
			}
			else {
				ilejed++;
			}
		}
		if(min({ilezap, ilezer, ilejed}) == ilezer) {
			vi kt;
			for(int i = 0; i < l; i++) {
				if(a[i] == '0') {
					kt.eb(i);
				}
			}
			int aktmask = 0;
			for(int i = 0; i < l; i++) {
				if(a[i] == '1') {
					aktmask += (1 << i);
				}
			}
			int ans = 0;
			for(int mask = 0; mask < (1 << sz(kt)); mask++) {
				int nmask = aktmask;
				for(int j = 0; j < sz(kt); j++) {
					if(mask & (1 << j)) {
						nmask ^= (1 << kt[j]);
					}
				}
				if(__builtin_popcount(mask) & 1) {
					ans -= dpgora[nmask];
				}
				else {
					ans += dpgora[nmask];
				}
			}
			cout << ans << '\n';
		}
		else if(min({ilezap, ilezer, ilejed}) == ilejed) {
			vi kt;
			for(int i = 0; i < l; i++) {
				if(a[i] == '1') {
					kt.eb(i);
				}
			}
			int aktmask = 0;
			for(int i = 0; i < l; i++) {
				if(a[i] == '1' || a[i] == '?') {
					aktmask += (1 << i);
				}
			}
			int ans = 0;
			for(int mask = 0; mask < (1 << sz(kt)); mask++) {
				int nmask = aktmask;
				for(int j = 0; j < sz(kt); j++) {
					if(mask & (1 << j)) {
						nmask ^= (1 << kt[j]);
					}
				}
				if(__builtin_popcount(mask) & 1) {
					ans -= dpdol[nmask];
				}
				else {
					ans += dpdol[nmask];
				}
			}
			cout << ans << '\n';
		}
		else {
			vi kt;
			for(int i = 0; i < l; i++) {
				if(a[i] == '?') {
					kt.eb(i);
				}
			}
			int aktmask = 0;
			for(int i = 0; i < l; i++) {
				if(a[i] == '1') {
					aktmask += (1 << i);
				}
			}
			int ans = 0;
			for(int mask = 0; mask < (1 << sz(kt)); mask++) {
				int nmask = aktmask;
				for(int j = 0; j < sz(kt); j++) {
					if(mask & (1 << j)) {
						nmask ^= (1 << kt[j]);
					}
				}
				ans += (c[nmask] - '0');
			}
			cout << ans << '\n';
		}
	}
}
int main() {
	fastio();
	int t = 1;
	//cin >> t;
	while(t--) {
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 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 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 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 189 ms 15184 KB Output is correct
12 Correct 243 ms 14672 KB Output is correct
13 Correct 339 ms 13908 KB Output is correct
14 Correct 422 ms 14144 KB Output is correct
15 Correct 305 ms 15188 KB Output is correct
16 Correct 401 ms 14264 KB Output is correct
17 Correct 386 ms 14356 KB Output is correct
18 Correct 164 ms 16212 KB Output is correct
19 Correct 187 ms 13000 KB Output is correct
20 Correct 228 ms 14676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 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 189 ms 15184 KB Output is correct
12 Correct 243 ms 14672 KB Output is correct
13 Correct 339 ms 13908 KB Output is correct
14 Correct 422 ms 14144 KB Output is correct
15 Correct 305 ms 15188 KB Output is correct
16 Correct 401 ms 14264 KB Output is correct
17 Correct 386 ms 14356 KB Output is correct
18 Correct 164 ms 16212 KB Output is correct
19 Correct 187 ms 13000 KB Output is correct
20 Correct 228 ms 14676 KB Output is correct
21 Correct 257 ms 18096 KB Output is correct
22 Correct 289 ms 18380 KB Output is correct
23 Correct 437 ms 17240 KB Output is correct
24 Correct 548 ms 17284 KB Output is correct
25 Correct 416 ms 19024 KB Output is correct
26 Correct 493 ms 17744 KB Output is correct
27 Correct 484 ms 17508 KB Output is correct
28 Correct 194 ms 20052 KB Output is correct
29 Correct 268 ms 16208 KB Output is correct
30 Correct 278 ms 18324 KB Output is correct
31 Correct 453 ms 18148 KB Output is correct
32 Correct 509 ms 18000 KB Output is correct
33 Correct 359 ms 17172 KB Output is correct
34 Correct 524 ms 17232 KB Output is correct
35 Correct 470 ms 17660 KB Output is correct
36 Correct 169 ms 16216 KB Output is correct
37 Correct 274 ms 18000 KB Output is correct
38 Correct 281 ms 16128 KB Output is correct
39 Correct 423 ms 17236 KB Output is correct
40 Correct 509 ms 17236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 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 50 ms 12016 KB Output is correct
12 Correct 52 ms 12016 KB Output is correct
13 Correct 60 ms 11860 KB Output is correct
14 Correct 94 ms 12008 KB Output is correct
15 Correct 55 ms 12048 KB Output is correct
16 Correct 81 ms 11868 KB Output is correct
17 Correct 80 ms 12020 KB Output is correct
18 Correct 39 ms 12024 KB Output is correct
19 Correct 47 ms 11760 KB Output is correct
20 Correct 49 ms 12020 KB Output is correct
21 Correct 65 ms 11948 KB Output is correct
22 Correct 93 ms 11860 KB Output is correct
23 Correct 55 ms 11864 KB Output is correct
24 Correct 81 ms 12020 KB Output is correct
25 Correct 71 ms 11860 KB Output is correct
26 Correct 38 ms 11860 KB Output is correct
27 Correct 47 ms 12020 KB Output is correct
28 Correct 50 ms 11768 KB Output is correct
29 Correct 62 ms 11824 KB Output is correct
30 Correct 80 ms 12016 KB Output is correct
31 Correct 55 ms 12016 KB Output is correct
32 Correct 82 ms 12020 KB Output is correct
33 Correct 73 ms 12020 KB Output is correct
34 Correct 43 ms 11872 KB Output is correct
35 Correct 66 ms 12020 KB Output is correct
36 Correct 67 ms 12020 KB Output is correct
37 Correct 69 ms 12020 KB Output is correct
38 Correct 71 ms 11860 KB Output is correct
39 Correct 64 ms 12276 KB Output is correct
40 Correct 68 ms 11880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 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 189 ms 15184 KB Output is correct
12 Correct 243 ms 14672 KB Output is correct
13 Correct 339 ms 13908 KB Output is correct
14 Correct 422 ms 14144 KB Output is correct
15 Correct 305 ms 15188 KB Output is correct
16 Correct 401 ms 14264 KB Output is correct
17 Correct 386 ms 14356 KB Output is correct
18 Correct 164 ms 16212 KB Output is correct
19 Correct 187 ms 13000 KB Output is correct
20 Correct 228 ms 14676 KB Output is correct
21 Correct 257 ms 18096 KB Output is correct
22 Correct 289 ms 18380 KB Output is correct
23 Correct 437 ms 17240 KB Output is correct
24 Correct 548 ms 17284 KB Output is correct
25 Correct 416 ms 19024 KB Output is correct
26 Correct 493 ms 17744 KB Output is correct
27 Correct 484 ms 17508 KB Output is correct
28 Correct 194 ms 20052 KB Output is correct
29 Correct 268 ms 16208 KB Output is correct
30 Correct 278 ms 18324 KB Output is correct
31 Correct 453 ms 18148 KB Output is correct
32 Correct 509 ms 18000 KB Output is correct
33 Correct 359 ms 17172 KB Output is correct
34 Correct 524 ms 17232 KB Output is correct
35 Correct 470 ms 17660 KB Output is correct
36 Correct 169 ms 16216 KB Output is correct
37 Correct 274 ms 18000 KB Output is correct
38 Correct 281 ms 16128 KB Output is correct
39 Correct 423 ms 17236 KB Output is correct
40 Correct 509 ms 17236 KB Output is correct
41 Correct 50 ms 12016 KB Output is correct
42 Correct 52 ms 12016 KB Output is correct
43 Correct 60 ms 11860 KB Output is correct
44 Correct 94 ms 12008 KB Output is correct
45 Correct 55 ms 12048 KB Output is correct
46 Correct 81 ms 11868 KB Output is correct
47 Correct 80 ms 12020 KB Output is correct
48 Correct 39 ms 12024 KB Output is correct
49 Correct 47 ms 11760 KB Output is correct
50 Correct 49 ms 12020 KB Output is correct
51 Correct 65 ms 11948 KB Output is correct
52 Correct 93 ms 11860 KB Output is correct
53 Correct 55 ms 11864 KB Output is correct
54 Correct 81 ms 12020 KB Output is correct
55 Correct 71 ms 11860 KB Output is correct
56 Correct 38 ms 11860 KB Output is correct
57 Correct 47 ms 12020 KB Output is correct
58 Correct 50 ms 11768 KB Output is correct
59 Correct 62 ms 11824 KB Output is correct
60 Correct 80 ms 12016 KB Output is correct
61 Correct 55 ms 12016 KB Output is correct
62 Correct 82 ms 12020 KB Output is correct
63 Correct 73 ms 12020 KB Output is correct
64 Correct 43 ms 11872 KB Output is correct
65 Correct 66 ms 12020 KB Output is correct
66 Correct 67 ms 12020 KB Output is correct
67 Correct 69 ms 12020 KB Output is correct
68 Correct 71 ms 11860 KB Output is correct
69 Correct 64 ms 12276 KB Output is correct
70 Correct 68 ms 11880 KB Output is correct
71 Correct 433 ms 36336 KB Output is correct
72 Correct 456 ms 36328 KB Output is correct
73 Correct 730 ms 34872 KB Output is correct
74 Correct 1308 ms 35376 KB Output is correct
75 Correct 602 ms 37452 KB Output is correct
76 Correct 1131 ms 35624 KB Output is correct
77 Correct 981 ms 35468 KB Output is correct
78 Correct 281 ms 39208 KB Output is correct
79 Correct 432 ms 33244 KB Output is correct
80 Correct 461 ms 36352 KB Output is correct
81 Correct 765 ms 36656 KB Output is correct
82 Correct 1271 ms 35572 KB Output is correct
83 Correct 536 ms 34348 KB Output is correct
84 Correct 1133 ms 35404 KB Output is correct
85 Correct 949 ms 35400 KB Output is correct
86 Correct 288 ms 33192 KB Output is correct
87 Correct 447 ms 36384 KB Output is correct
88 Correct 499 ms 33292 KB Output is correct
89 Correct 765 ms 35092 KB Output is correct
90 Correct 1098 ms 35232 KB Output is correct
91 Correct 538 ms 34392 KB Output is correct
92 Correct 1188 ms 35792 KB Output is correct
93 Correct 1018 ms 35152 KB Output is correct
94 Correct 282 ms 33316 KB Output is correct
95 Correct 802 ms 35476 KB Output is correct
96 Correct 842 ms 35736 KB Output is correct
97 Correct 806 ms 35500 KB Output is correct
98 Correct 778 ms 35384 KB Output is correct
99 Correct 797 ms 35508 KB Output is correct
100 Correct 773 ms 35292 KB Output is correct