답안 #564023

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
564023 2022-05-18T12:50:53 Z SeDunion Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1057 ms 39276 KB
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("bmi,abm,fma,avx,avx2,popcnt,lzcnt")
#include <iostream>
#include <cassert>
#include <algorithm>
#include <string>
#include <bitset> 
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#ifndef LOCAL
	#include <bits/stdc++.h>
	#define cerr if(false)cerr
#endif
 
using namespace std;
using ll = long long;

const int N = 2e6 + 66;

int up[N];
int dw[N];
vector<int>pos;
string vals, qs;
int L;



int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int q;
	cin >> L >> q;
	cin >> vals;
	for (int i = 0 ; i < (1 << L) ; ++ i) {
		up[i] = dw[i] = vals[i] - '0';
	}
	for (int i = 0 ; i < L ; ++ i) {
		for (int m = 0 ; m < (1 << L) ; ++ m) {
			if (m >> i & 1) {
				dw[m] += dw[m ^ (1 << i)];
			}
		}
	}
	for (int i = 0 ; i < L ; ++ i) {
		for (int m = (1 << L) - 1 ; m >= 0 ; -- m) {
			if (m >> i & 1) {
				up[m ^ (1 << i)] += up[m];
			}
		}
	}
	for (int m = 0 ; m < (1 << L) ; ++ m) {
		cerr << bitset<3>(m) << " " << dw[m] << endl;
	}
	while (q--) {
		int ans = 0;
		cin >> qs;
		reverse(qs.begin(), qs.end());
		int a = count(qs.begin(), qs.end(), '0');
		int c = count(qs.begin(), qs.end(), '?');
		if (c <= 6) {
			pos.clear();
			for (int i = 0 ; i < L ; ++ i) {
				if (qs[i] == '?') {
					pos.emplace_back(i);
				}
			}
			int n = pos.size();
			int base = 0;
			for (int i = 0 ; i < L ; ++ i) {
				if (qs[i] == '1') {
					base |= 1 << i;
				}
			}
			for (int m = 0 ; m < (1 << n) ; ++ m) {
				int mask = base;
				for (int i = 0 ; i < n ; ++ i) {
					if (m >> i & 1) mask |= 1 << pos[i];
				}
				ans += vals[mask] - '0';
			}
		} else if (a <= 6) {
			pos.clear();
			for (int i = 0 ; i < L ; ++ i) {
				if (qs[i] == '0') {
					pos.emplace_back(i);
				}
			}
			int n = pos.size();
			int base = 0;
			for (int i = 0 ; i < L ; ++ i) {
				if (qs[i] == '1') {
					base |= 1 << i;
				}
			}
			for (int m = 0 ; m < (1 << n) ; ++ m) {
				int mask = base;
				for (int i = 0 ; i < n ; ++ i) {
					if (m >> i & 1) mask |= 1 << pos[i];
				}
				int par = __builtin_popcount(m) & 1;
				int cur = up[mask] * (par ? -1 : 1);
				ans += cur;
			}
		} else {
			pos.clear();
			for (int i = 0 ; i < L ; ++ i) {
				if (qs[i] == '1') {
					pos.emplace_back(i);
				}
			}
			int n = pos.size();
			int base = 0;
			for (int i = 0 ; i < L ; ++ i) {
				if (qs[i] == '?') {
					base |= 1 << i;
				}
			}
			for (int m = 0 ; m < (1 << n) ; ++ m) {
				int mask = base;
				for (int i = 0 ; i < n ; ++ i) {
					if (m >> i & 1) mask |= 1 << pos[i];
				}
				int par = __builtin_popcount(((1 << n) - 1) ^ m) & 1;
				int cur = dw[mask] * (par ? -1 : 1);
				ans += cur;
			}
		}
		cout << ans << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 436 ms 4336 KB Output is correct
12 Correct 428 ms 4020 KB Output is correct
13 Correct 270 ms 3188 KB Output is correct
14 Correct 267 ms 3236 KB Output is correct
15 Correct 653 ms 4424 KB Output is correct
16 Correct 389 ms 3628 KB Output is correct
17 Correct 363 ms 3516 KB Output is correct
18 Correct 186 ms 5424 KB Output is correct
19 Correct 199 ms 2472 KB Output is correct
20 Correct 443 ms 4072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 436 ms 4336 KB Output is correct
12 Correct 428 ms 4020 KB Output is correct
13 Correct 270 ms 3188 KB Output is correct
14 Correct 267 ms 3236 KB Output is correct
15 Correct 653 ms 4424 KB Output is correct
16 Correct 389 ms 3628 KB Output is correct
17 Correct 363 ms 3516 KB Output is correct
18 Correct 186 ms 5424 KB Output is correct
19 Correct 199 ms 2472 KB Output is correct
20 Correct 443 ms 4072 KB Output is correct
21 Correct 242 ms 4476 KB Output is correct
22 Correct 574 ms 4684 KB Output is correct
23 Correct 402 ms 3688 KB Output is correct
24 Correct 378 ms 3476 KB Output is correct
25 Correct 292 ms 5552 KB Output is correct
26 Correct 496 ms 3952 KB Output is correct
27 Correct 482 ms 3984 KB Output is correct
28 Correct 204 ms 6544 KB Output is correct
29 Correct 236 ms 2492 KB Output is correct
30 Correct 437 ms 4684 KB Output is correct
31 Correct 674 ms 4676 KB Output is correct
32 Correct 485 ms 4460 KB Output is correct
33 Correct 311 ms 3404 KB Output is correct
34 Correct 362 ms 3532 KB Output is correct
35 Correct 451 ms 3936 KB Output is correct
36 Correct 187 ms 2536 KB Output is correct
37 Correct 635 ms 4632 KB Output is correct
38 Correct 241 ms 2508 KB Output is correct
39 Correct 402 ms 3652 KB Output is correct
40 Correct 358 ms 3472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 47 ms 9832 KB Output is correct
12 Correct 55 ms 9832 KB Output is correct
13 Correct 62 ms 11836 KB Output is correct
14 Correct 75 ms 11856 KB Output is correct
15 Correct 56 ms 12112 KB Output is correct
16 Correct 92 ms 11960 KB Output is correct
17 Correct 73 ms 11856 KB Output is correct
18 Correct 43 ms 12128 KB Output is correct
19 Correct 49 ms 11732 KB Output is correct
20 Correct 54 ms 11884 KB Output is correct
21 Correct 65 ms 11992 KB Output is correct
22 Correct 75 ms 11856 KB Output is correct
23 Correct 55 ms 11828 KB Output is correct
24 Correct 70 ms 11864 KB Output is correct
25 Correct 75 ms 11872 KB Output is correct
26 Correct 45 ms 11900 KB Output is correct
27 Correct 53 ms 12012 KB Output is correct
28 Correct 47 ms 11724 KB Output is correct
29 Correct 61 ms 11820 KB Output is correct
30 Correct 89 ms 11824 KB Output is correct
31 Correct 54 ms 11840 KB Output is correct
32 Correct 106 ms 11912 KB Output is correct
33 Correct 74 ms 11860 KB Output is correct
34 Correct 46 ms 11732 KB Output is correct
35 Correct 68 ms 11884 KB Output is correct
36 Correct 74 ms 11940 KB Output is correct
37 Correct 85 ms 11884 KB Output is correct
38 Correct 71 ms 11892 KB Output is correct
39 Correct 72 ms 11884 KB Output is correct
40 Correct 72 ms 11860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 436 ms 4336 KB Output is correct
12 Correct 428 ms 4020 KB Output is correct
13 Correct 270 ms 3188 KB Output is correct
14 Correct 267 ms 3236 KB Output is correct
15 Correct 653 ms 4424 KB Output is correct
16 Correct 389 ms 3628 KB Output is correct
17 Correct 363 ms 3516 KB Output is correct
18 Correct 186 ms 5424 KB Output is correct
19 Correct 199 ms 2472 KB Output is correct
20 Correct 443 ms 4072 KB Output is correct
21 Correct 242 ms 4476 KB Output is correct
22 Correct 574 ms 4684 KB Output is correct
23 Correct 402 ms 3688 KB Output is correct
24 Correct 378 ms 3476 KB Output is correct
25 Correct 292 ms 5552 KB Output is correct
26 Correct 496 ms 3952 KB Output is correct
27 Correct 482 ms 3984 KB Output is correct
28 Correct 204 ms 6544 KB Output is correct
29 Correct 236 ms 2492 KB Output is correct
30 Correct 437 ms 4684 KB Output is correct
31 Correct 674 ms 4676 KB Output is correct
32 Correct 485 ms 4460 KB Output is correct
33 Correct 311 ms 3404 KB Output is correct
34 Correct 362 ms 3532 KB Output is correct
35 Correct 451 ms 3936 KB Output is correct
36 Correct 187 ms 2536 KB Output is correct
37 Correct 635 ms 4632 KB Output is correct
38 Correct 241 ms 2508 KB Output is correct
39 Correct 402 ms 3652 KB Output is correct
40 Correct 358 ms 3472 KB Output is correct
41 Correct 47 ms 9832 KB Output is correct
42 Correct 55 ms 9832 KB Output is correct
43 Correct 62 ms 11836 KB Output is correct
44 Correct 75 ms 11856 KB Output is correct
45 Correct 56 ms 12112 KB Output is correct
46 Correct 92 ms 11960 KB Output is correct
47 Correct 73 ms 11856 KB Output is correct
48 Correct 43 ms 12128 KB Output is correct
49 Correct 49 ms 11732 KB Output is correct
50 Correct 54 ms 11884 KB Output is correct
51 Correct 65 ms 11992 KB Output is correct
52 Correct 75 ms 11856 KB Output is correct
53 Correct 55 ms 11828 KB Output is correct
54 Correct 70 ms 11864 KB Output is correct
55 Correct 75 ms 11872 KB Output is correct
56 Correct 45 ms 11900 KB Output is correct
57 Correct 53 ms 12012 KB Output is correct
58 Correct 47 ms 11724 KB Output is correct
59 Correct 61 ms 11820 KB Output is correct
60 Correct 89 ms 11824 KB Output is correct
61 Correct 54 ms 11840 KB Output is correct
62 Correct 106 ms 11912 KB Output is correct
63 Correct 74 ms 11860 KB Output is correct
64 Correct 46 ms 11732 KB Output is correct
65 Correct 68 ms 11884 KB Output is correct
66 Correct 74 ms 11940 KB Output is correct
67 Correct 85 ms 11884 KB Output is correct
68 Correct 71 ms 11892 KB Output is correct
69 Correct 72 ms 11884 KB Output is correct
70 Correct 72 ms 11860 KB Output is correct
71 Correct 329 ms 36284 KB Output is correct
72 Correct 431 ms 36400 KB Output is correct
73 Correct 594 ms 34848 KB Output is correct
74 Correct 946 ms 35308 KB Output is correct
75 Correct 480 ms 37388 KB Output is correct
76 Correct 930 ms 35680 KB Output is correct
77 Correct 864 ms 35384 KB Output is correct
78 Correct 257 ms 39276 KB Output is correct
79 Correct 332 ms 33276 KB Output is correct
80 Correct 392 ms 36332 KB Output is correct
81 Correct 630 ms 36208 KB Output is correct
82 Correct 920 ms 35232 KB Output is correct
83 Correct 474 ms 34452 KB Output is correct
84 Correct 799 ms 35288 KB Output is correct
85 Correct 895 ms 35528 KB Output is correct
86 Correct 262 ms 33192 KB Output is correct
87 Correct 343 ms 36260 KB Output is correct
88 Correct 338 ms 33304 KB Output is correct
89 Correct 619 ms 34944 KB Output is correct
90 Correct 805 ms 35224 KB Output is correct
91 Correct 454 ms 34380 KB Output is correct
92 Correct 1057 ms 35688 KB Output is correct
93 Correct 867 ms 35476 KB Output is correct
94 Correct 248 ms 33256 KB Output is correct
95 Correct 782 ms 35356 KB Output is correct
96 Correct 808 ms 35284 KB Output is correct
97 Correct 768 ms 35208 KB Output is correct
98 Correct 734 ms 35348 KB Output is correct
99 Correct 801 ms 35416 KB Output is correct
100 Correct 804 ms 35328 KB Output is correct