답안 #943984

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
943984 2024-03-12T06:13:49 Z guechotjrhh Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1912 ms 65536 KB
#include<iostream>
#include<string>
using namespace std;

#include<string>
#include<vector>
#define maxp 1048580
using namespace std;
int st[maxp],n,p,res,zer[maxp], on[maxp];
int ab(int x) { return x > 0 ? x : -x; }
void init(int N, int Q, string S) {
	n = N; p = 1 << n;
	for (int i = 0; i < p; i++) zer[i] = on[i] = st[i] = S[i] - '0';
	for (int i = 1; i < p; i<<=1) {
		for (int j = 0; j < p; j++) {
			if (j & i) zer[j] += zer[j ^ i];
			else on[j] += on[j ^ i];
		}
	}
}
int* o, * z, * q;
int oc, zc, qc;
void rec1(int i, int c, int a) {
	if (i == oc) {res += (a^zer[c]) + (a & 1);return;}
	rec1(i + 1, c, a);
	rec1(i + 1, c ^ o[i], ~a);
}
void rec0(int i, int c, int a) {
	if (i == zc) {res += (a^on[c]) + (a&1); return; }
	rec0(i + 1, c, a);
	rec0(i + 1, c ^ z[i], ~a);
}
void recq(int i, int c) {
	if (i == qc) { res += st[c]; return; }
	recq(i + 1, c);
	recq(i + 1, c ^ q[i]);
}
int query(string A) {
	oc = zc = qc = 0;
	for (int i = 0; i < n; i++) {
		if (A[i] == '0') zc++;
		else if (A[i] == '1') oc++;
		else qc++;
	}
	if (oc < zc && oc < qc) {
		int c = 0;
		//vector<int> o;
		o = new int[oc];
		int po = 0;
		for (int i = 0; i < n; i++) {
			if (A[i] == '1') o[po++] = (1 << (n - 1 - i));
			else if(A[i]=='?') c += (1 << (n - 1 - i));
		}
		res = 0;
		rec1(0, c, 0);
		return ab(res);
	}
	else if (qc > zc) {
		int c = 0;
		//vector<int> z;
		z = new int[zc];
		int pz = 0;
		for (int i = 0; i < n; i++) {
			if (A[i] == '1') c += (1 << (n - 1 - i));
			else if (A[i] == '0') z[pz++]=(1 << (n - 1 - i));
		}
		res = 0;
		rec0(0, c, 0);
		return ab(res);
	}
	else {
		res = 0;
		int c = 0;
		q = new int[qc];
		int pq = 0;
		for (int i = 0; i < n; i++) {
			if (A[i] == '1') c += (1 << (n - 1 - i));
			else if (A[i] == '?') q[pq++]=(1 << (n - 1 - i));
		}
		recq(0, c);
		return res;
	}
	return 0;
}
/*
4 8
3141592653589793
0101
?01?
??1?
?0??
1?00
01?1
??10
????

3 5
12345678
000
0??
1?0
?11
???
*/
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int N, Q; cin >> N >> Q;
	string S; cin >> S;
	init(N, Q, move(S));
	
	for (int i = 0; i < Q; i++) {
		string A;
		cin >> A;
		cout << query(move(A)) << endl;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 4 ms 4440 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 4 ms 4444 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4564 KB Output is correct
8 Correct 3 ms 4572 KB Output is correct
9 Correct 2 ms 4440 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 4 ms 4440 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 4 ms 4444 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4564 KB Output is correct
8 Correct 3 ms 4572 KB Output is correct
9 Correct 2 ms 4440 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 1330 ms 50616 KB Output is correct
12 Correct 1333 ms 50192 KB Output is correct
13 Correct 1372 ms 49424 KB Output is correct
14 Correct 1400 ms 50004 KB Output is correct
15 Correct 1461 ms 50728 KB Output is correct
16 Correct 1409 ms 49860 KB Output is correct
17 Correct 1401 ms 49748 KB Output is correct
18 Correct 1271 ms 51560 KB Output is correct
19 Correct 1372 ms 48432 KB Output is correct
20 Correct 1391 ms 50516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 4 ms 4440 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 4 ms 4444 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4564 KB Output is correct
8 Correct 3 ms 4572 KB Output is correct
9 Correct 2 ms 4440 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 1330 ms 50616 KB Output is correct
12 Correct 1333 ms 50192 KB Output is correct
13 Correct 1372 ms 49424 KB Output is correct
14 Correct 1400 ms 50004 KB Output is correct
15 Correct 1461 ms 50728 KB Output is correct
16 Correct 1409 ms 49860 KB Output is correct
17 Correct 1401 ms 49748 KB Output is correct
18 Correct 1271 ms 51560 KB Output is correct
19 Correct 1372 ms 48432 KB Output is correct
20 Correct 1391 ms 50516 KB Output is correct
21 Correct 1394 ms 53376 KB Output is correct
22 Correct 1380 ms 53812 KB Output is correct
23 Correct 1495 ms 52932 KB Output is correct
24 Correct 1464 ms 52524 KB Output is correct
25 Correct 1408 ms 54816 KB Output is correct
26 Correct 1500 ms 53028 KB Output is correct
27 Correct 1455 ms 52852 KB Output is correct
28 Correct 1281 ms 55544 KB Output is correct
29 Correct 1402 ms 51696 KB Output is correct
30 Correct 1405 ms 53564 KB Output is correct
31 Correct 1420 ms 53692 KB Output is correct
32 Correct 1472 ms 53444 KB Output is correct
33 Correct 1407 ms 52404 KB Output is correct
34 Correct 1450 ms 52816 KB Output is correct
35 Correct 1472 ms 52976 KB Output is correct
36 Correct 1304 ms 51520 KB Output is correct
37 Correct 1326 ms 53676 KB Output is correct
38 Correct 1398 ms 51748 KB Output is correct
39 Correct 1457 ms 52652 KB Output is correct
40 Correct 1449 ms 52512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 4 ms 4440 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 4 ms 4444 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4564 KB Output is correct
8 Correct 3 ms 4572 KB Output is correct
9 Correct 2 ms 4440 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 97 ms 16608 KB Output is correct
12 Correct 91 ms 16464 KB Output is correct
13 Correct 102 ms 16356 KB Output is correct
14 Correct 120 ms 16424 KB Output is correct
15 Correct 94 ms 16548 KB Output is correct
16 Correct 112 ms 16452 KB Output is correct
17 Correct 125 ms 16368 KB Output is correct
18 Correct 91 ms 16764 KB Output is correct
19 Correct 97 ms 16348 KB Output is correct
20 Correct 96 ms 16472 KB Output is correct
21 Correct 99 ms 16604 KB Output is correct
22 Correct 116 ms 16588 KB Output is correct
23 Correct 98 ms 16472 KB Output is correct
24 Correct 109 ms 16348 KB Output is correct
25 Correct 105 ms 16476 KB Output is correct
26 Correct 87 ms 16348 KB Output is correct
27 Correct 91 ms 16588 KB Output is correct
28 Correct 96 ms 16368 KB Output is correct
29 Correct 102 ms 16448 KB Output is correct
30 Correct 110 ms 16348 KB Output is correct
31 Correct 104 ms 16356 KB Output is correct
32 Correct 112 ms 16452 KB Output is correct
33 Correct 111 ms 16448 KB Output is correct
34 Correct 87 ms 16352 KB Output is correct
35 Correct 103 ms 16348 KB Output is correct
36 Correct 104 ms 16352 KB Output is correct
37 Correct 108 ms 16568 KB Output is correct
38 Correct 105 ms 16548 KB Output is correct
39 Correct 120 ms 16444 KB Output is correct
40 Correct 105 ms 16348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 4 ms 4440 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 4 ms 4444 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4564 KB Output is correct
8 Correct 3 ms 4572 KB Output is correct
9 Correct 2 ms 4440 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 1330 ms 50616 KB Output is correct
12 Correct 1333 ms 50192 KB Output is correct
13 Correct 1372 ms 49424 KB Output is correct
14 Correct 1400 ms 50004 KB Output is correct
15 Correct 1461 ms 50728 KB Output is correct
16 Correct 1409 ms 49860 KB Output is correct
17 Correct 1401 ms 49748 KB Output is correct
18 Correct 1271 ms 51560 KB Output is correct
19 Correct 1372 ms 48432 KB Output is correct
20 Correct 1391 ms 50516 KB Output is correct
21 Correct 1394 ms 53376 KB Output is correct
22 Correct 1380 ms 53812 KB Output is correct
23 Correct 1495 ms 52932 KB Output is correct
24 Correct 1464 ms 52524 KB Output is correct
25 Correct 1408 ms 54816 KB Output is correct
26 Correct 1500 ms 53028 KB Output is correct
27 Correct 1455 ms 52852 KB Output is correct
28 Correct 1281 ms 55544 KB Output is correct
29 Correct 1402 ms 51696 KB Output is correct
30 Correct 1405 ms 53564 KB Output is correct
31 Correct 1420 ms 53692 KB Output is correct
32 Correct 1472 ms 53444 KB Output is correct
33 Correct 1407 ms 52404 KB Output is correct
34 Correct 1450 ms 52816 KB Output is correct
35 Correct 1472 ms 52976 KB Output is correct
36 Correct 1304 ms 51520 KB Output is correct
37 Correct 1326 ms 53676 KB Output is correct
38 Correct 1398 ms 51748 KB Output is correct
39 Correct 1457 ms 52652 KB Output is correct
40 Correct 1449 ms 52512 KB Output is correct
41 Correct 97 ms 16608 KB Output is correct
42 Correct 91 ms 16464 KB Output is correct
43 Correct 102 ms 16356 KB Output is correct
44 Correct 120 ms 16424 KB Output is correct
45 Correct 94 ms 16548 KB Output is correct
46 Correct 112 ms 16452 KB Output is correct
47 Correct 125 ms 16368 KB Output is correct
48 Correct 91 ms 16764 KB Output is correct
49 Correct 97 ms 16348 KB Output is correct
50 Correct 96 ms 16472 KB Output is correct
51 Correct 99 ms 16604 KB Output is correct
52 Correct 116 ms 16588 KB Output is correct
53 Correct 98 ms 16472 KB Output is correct
54 Correct 109 ms 16348 KB Output is correct
55 Correct 105 ms 16476 KB Output is correct
56 Correct 87 ms 16348 KB Output is correct
57 Correct 91 ms 16588 KB Output is correct
58 Correct 96 ms 16368 KB Output is correct
59 Correct 102 ms 16448 KB Output is correct
60 Correct 110 ms 16348 KB Output is correct
61 Correct 104 ms 16356 KB Output is correct
62 Correct 112 ms 16452 KB Output is correct
63 Correct 111 ms 16448 KB Output is correct
64 Correct 87 ms 16352 KB Output is correct
65 Correct 103 ms 16348 KB Output is correct
66 Correct 104 ms 16352 KB Output is correct
67 Correct 108 ms 16568 KB Output is correct
68 Correct 105 ms 16548 KB Output is correct
69 Correct 120 ms 16444 KB Output is correct
70 Correct 105 ms 16348 KB Output is correct
71 Correct 1619 ms 65536 KB Output is correct
72 Correct 1544 ms 65536 KB Output is correct
73 Correct 1662 ms 65536 KB Output is correct
74 Correct 1789 ms 65536 KB Output is correct
75 Correct 1573 ms 65536 KB Output is correct
76 Correct 1817 ms 65536 KB Output is correct
77 Correct 1797 ms 65536 KB Output is correct
78 Correct 1374 ms 65536 KB Output is correct
79 Correct 1573 ms 65536 KB Output is correct
80 Correct 1577 ms 65536 KB Output is correct
81 Correct 1692 ms 65536 KB Output is correct
82 Correct 1856 ms 65536 KB Output is correct
83 Correct 1574 ms 65536 KB Output is correct
84 Correct 1884 ms 65536 KB Output is correct
85 Correct 1887 ms 65536 KB Output is correct
86 Correct 1371 ms 65536 KB Output is correct
87 Correct 1480 ms 65536 KB Output is correct
88 Correct 1551 ms 65536 KB Output is correct
89 Correct 1699 ms 65536 KB Output is correct
90 Correct 1804 ms 65536 KB Output is correct
91 Correct 1537 ms 65536 KB Output is correct
92 Correct 1912 ms 65536 KB Output is correct
93 Correct 1898 ms 65536 KB Output is correct
94 Correct 1362 ms 65536 KB Output is correct
95 Correct 1810 ms 65536 KB Output is correct
96 Correct 1738 ms 65536 KB Output is correct
97 Correct 1769 ms 65536 KB Output is correct
98 Correct 1794 ms 65536 KB Output is correct
99 Correct 1777 ms 65536 KB Output is correct
100 Correct 1819 ms 65536 KB Output is correct