답안 #944150

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944150 2024-03-12T09:07:47 Z guechotjrhh Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 40388 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[20], z[20], q[20];
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,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,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,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 4440 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 3 ms 4576 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4572 KB Output is correct
9 Correct 2 ms 4696 KB Output is correct
10 Correct 3 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 3 ms 4576 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4572 KB Output is correct
9 Correct 2 ms 4696 KB Output is correct
10 Correct 3 ms 4444 KB Output is correct
11 Correct 1261 ms 19456 KB Output is correct
12 Correct 1306 ms 18780 KB Output is correct
13 Correct 1301 ms 18224 KB Output is correct
14 Correct 1322 ms 18284 KB Output is correct
15 Correct 1300 ms 19056 KB Output is correct
16 Correct 1367 ms 18360 KB Output is correct
17 Correct 1378 ms 18404 KB Output is correct
18 Correct 1317 ms 20440 KB Output is correct
19 Correct 1292 ms 15468 KB Output is correct
20 Correct 1299 ms 18868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 3 ms 4576 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4572 KB Output is correct
9 Correct 2 ms 4696 KB Output is correct
10 Correct 3 ms 4444 KB Output is correct
11 Correct 1261 ms 19456 KB Output is correct
12 Correct 1306 ms 18780 KB Output is correct
13 Correct 1301 ms 18224 KB Output is correct
14 Correct 1322 ms 18284 KB Output is correct
15 Correct 1300 ms 19056 KB Output is correct
16 Correct 1367 ms 18360 KB Output is correct
17 Correct 1378 ms 18404 KB Output is correct
18 Correct 1317 ms 20440 KB Output is correct
19 Correct 1292 ms 15468 KB Output is correct
20 Correct 1299 ms 18868 KB Output is correct
21 Correct 1376 ms 22288 KB Output is correct
22 Correct 1335 ms 22620 KB Output is correct
23 Correct 1344 ms 21600 KB Output is correct
24 Correct 1390 ms 21240 KB Output is correct
25 Correct 1339 ms 23284 KB Output is correct
26 Correct 1377 ms 21680 KB Output is correct
27 Correct 1426 ms 21900 KB Output is correct
28 Correct 1233 ms 24284 KB Output is correct
29 Correct 1330 ms 20052 KB Output is correct
30 Correct 1341 ms 22324 KB Output is correct
31 Correct 1381 ms 22184 KB Output is correct
32 Correct 1393 ms 22436 KB Output is correct
33 Correct 1357 ms 21048 KB Output is correct
34 Correct 1436 ms 21408 KB Output is correct
35 Correct 1381 ms 21700 KB Output is correct
36 Correct 1241 ms 20300 KB Output is correct
37 Correct 1273 ms 22460 KB Output is correct
38 Correct 1364 ms 20280 KB Output is correct
39 Correct 1423 ms 21576 KB Output is correct
40 Correct 1388 ms 21200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 3 ms 4576 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4572 KB Output is correct
9 Correct 2 ms 4696 KB Output is correct
10 Correct 3 ms 4444 KB Output is correct
11 Correct 95 ms 15356 KB Output is correct
12 Correct 91 ms 15100 KB Output is correct
13 Correct 98 ms 15104 KB Output is correct
14 Correct 110 ms 15104 KB Output is correct
15 Correct 93 ms 15104 KB Output is correct
16 Correct 109 ms 15180 KB Output is correct
17 Correct 118 ms 15128 KB Output is correct
18 Correct 86 ms 15132 KB Output is correct
19 Correct 95 ms 15104 KB Output is correct
20 Correct 95 ms 15096 KB Output is correct
21 Correct 98 ms 15104 KB Output is correct
22 Correct 108 ms 15104 KB Output is correct
23 Correct 95 ms 15104 KB Output is correct
24 Correct 114 ms 15060 KB Output is correct
25 Correct 109 ms 15116 KB Output is correct
26 Correct 88 ms 15016 KB Output is correct
27 Correct 93 ms 15164 KB Output is correct
28 Correct 97 ms 15104 KB Output is correct
29 Correct 101 ms 15104 KB Output is correct
30 Correct 103 ms 15104 KB Output is correct
31 Correct 100 ms 15104 KB Output is correct
32 Correct 106 ms 15168 KB Output is correct
33 Correct 109 ms 15104 KB Output is correct
34 Correct 87 ms 15124 KB Output is correct
35 Correct 107 ms 15328 KB Output is correct
36 Correct 103 ms 15100 KB Output is correct
37 Correct 101 ms 15100 KB Output is correct
38 Correct 100 ms 15100 KB Output is correct
39 Correct 117 ms 15328 KB Output is correct
40 Correct 106 ms 15172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 3 ms 4576 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4572 KB Output is correct
9 Correct 2 ms 4696 KB Output is correct
10 Correct 3 ms 4444 KB Output is correct
11 Correct 1261 ms 19456 KB Output is correct
12 Correct 1306 ms 18780 KB Output is correct
13 Correct 1301 ms 18224 KB Output is correct
14 Correct 1322 ms 18284 KB Output is correct
15 Correct 1300 ms 19056 KB Output is correct
16 Correct 1367 ms 18360 KB Output is correct
17 Correct 1378 ms 18404 KB Output is correct
18 Correct 1317 ms 20440 KB Output is correct
19 Correct 1292 ms 15468 KB Output is correct
20 Correct 1299 ms 18868 KB Output is correct
21 Correct 1376 ms 22288 KB Output is correct
22 Correct 1335 ms 22620 KB Output is correct
23 Correct 1344 ms 21600 KB Output is correct
24 Correct 1390 ms 21240 KB Output is correct
25 Correct 1339 ms 23284 KB Output is correct
26 Correct 1377 ms 21680 KB Output is correct
27 Correct 1426 ms 21900 KB Output is correct
28 Correct 1233 ms 24284 KB Output is correct
29 Correct 1330 ms 20052 KB Output is correct
30 Correct 1341 ms 22324 KB Output is correct
31 Correct 1381 ms 22184 KB Output is correct
32 Correct 1393 ms 22436 KB Output is correct
33 Correct 1357 ms 21048 KB Output is correct
34 Correct 1436 ms 21408 KB Output is correct
35 Correct 1381 ms 21700 KB Output is correct
36 Correct 1241 ms 20300 KB Output is correct
37 Correct 1273 ms 22460 KB Output is correct
38 Correct 1364 ms 20280 KB Output is correct
39 Correct 1423 ms 21576 KB Output is correct
40 Correct 1388 ms 21200 KB Output is correct
41 Correct 95 ms 15356 KB Output is correct
42 Correct 91 ms 15100 KB Output is correct
43 Correct 98 ms 15104 KB Output is correct
44 Correct 110 ms 15104 KB Output is correct
45 Correct 93 ms 15104 KB Output is correct
46 Correct 109 ms 15180 KB Output is correct
47 Correct 118 ms 15128 KB Output is correct
48 Correct 86 ms 15132 KB Output is correct
49 Correct 95 ms 15104 KB Output is correct
50 Correct 95 ms 15096 KB Output is correct
51 Correct 98 ms 15104 KB Output is correct
52 Correct 108 ms 15104 KB Output is correct
53 Correct 95 ms 15104 KB Output is correct
54 Correct 114 ms 15060 KB Output is correct
55 Correct 109 ms 15116 KB Output is correct
56 Correct 88 ms 15016 KB Output is correct
57 Correct 93 ms 15164 KB Output is correct
58 Correct 97 ms 15104 KB Output is correct
59 Correct 101 ms 15104 KB Output is correct
60 Correct 103 ms 15104 KB Output is correct
61 Correct 100 ms 15104 KB Output is correct
62 Correct 106 ms 15168 KB Output is correct
63 Correct 109 ms 15104 KB Output is correct
64 Correct 87 ms 15124 KB Output is correct
65 Correct 107 ms 15328 KB Output is correct
66 Correct 103 ms 15100 KB Output is correct
67 Correct 101 ms 15100 KB Output is correct
68 Correct 100 ms 15100 KB Output is correct
69 Correct 117 ms 15328 KB Output is correct
70 Correct 106 ms 15172 KB Output is correct
71 Correct 1526 ms 39352 KB Output is correct
72 Correct 1432 ms 39368 KB Output is correct
73 Correct 1662 ms 37992 KB Output is correct
74 Correct 1725 ms 38352 KB Output is correct
75 Correct 1491 ms 40388 KB Output is correct
76 Execution timed out 2003 ms 38684 KB Time limit exceeded
77 Halted 0 ms 0 KB -