답안 #315419

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
315419 2020-10-22T21:25:39 Z aZvezda Snake Escaping (JOI18_snake_escaping) C++14
22 / 100
1683 ms 65540 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "

const ll MAX_N = 1e6 + 10;
const ll MAX_RIGHT = 4800000; ll MAX_LEFT = 6;
ll pow3[50];
ll dp[MAX_RIGHT];
ll price[MAX_N];
string queries[MAX_N];
ll ans[MAX_N];
ll n, q;
ll sum[MAX_RIGHT], first2[MAX_RIGHT];
ll rightPart[MAX_N];

void prec() {
	for(ll i = 0; i < q; i ++) {
		ll currMask = 0;
		for(ll j = n - 1; j >= MAX_LEFT; j --) {
			currMask *= 3;
			currMask += (ll)(queries[i][j] - '0');
		}
		rightPart[i] = currMask;
	}

	ll triBits = n - MAX_LEFT;
	for(ll i = 0; i < pow3[triBits]; i ++) {
		bool flag = false;
		for(ll j = triBits - 1; j >= 0; j --) {
			if((i / pow3[j]) % 3 == 2) {
				first2[i] = j;
				flag = true;
				break;
			}
		}
		if(!flag) {
			ll currMask = 0;
			for(ll j = triBits - 1; j >= 0; j --) {
				currMask += ((i / pow3[j]) % 3) * (1 << (j + MAX_LEFT));
			}
			sum[i] = currMask;
			first2[i] = -1;
		}
	}
}

void getAns(ll mask) {
	vector<ll> indices;
	for(ll i = 0; i < q; i ++) {
		for(ll j = 0; j < MAX_LEFT; j ++) {
			if(mask & (1 << j)) {
				if(queries[i][j] == '0') {
					break;
				}
			} else {
				if(queries[i][j] == '1') {
					break;
				}				
			}
			if(j == MAX_LEFT - 1) {
				indices.push_back(i);
			}
		}
	}
	
	ll triBits = n - MAX_LEFT;
	for(ll i = 0; i < pow3[triBits]; i ++) {
		if(first2[i] != -1) {
			ll j = first2[i];		
			dp[i] = dp[i - pow3[j]] + dp[i - 2 * pow3[j]]; 
		} else {
			ll currMask = mask + sum[i];
			dp[i] = price[currMask];			
		}
	}
	
	for(auto it : indices) {
		ans[it] += dp[rightPart[it]];
	}
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	pow3[0] = 1;
	for(ll i = 1; i < 50; i ++) {
		pow3[i] = pow3[i - 1] * 3ll;
	}
	cin >> n >> q;
	chkmin(MAX_LEFT, n);
	string in;
	cin >> in;
	for(ll i = 0; i < (1 << n); i ++) {
		price[i] = in[i] - '0';
	}
	for(ll i = 0; i < q; i ++) {
		cin >> queries[i];
		reverse(queries[i].begin(), queries[i].end());
		for(auto &it : queries[i]) {
			if(it == '?') {
				it = '2';
			}
		}
	}
		
	prec();
	for(ll mask = 0; mask < (1 << MAX_LEFT); mask ++) {
		getAns(mask);
	}
	for(ll i = 0; i < q; i ++) {
		cout << ans[i] << endl;
	}
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 31744 KB Output is correct
2 Correct 22 ms 31744 KB Output is correct
3 Correct 22 ms 31744 KB Output is correct
4 Correct 23 ms 31744 KB Output is correct
5 Correct 22 ms 31736 KB Output is correct
6 Correct 22 ms 31744 KB Output is correct
7 Correct 22 ms 31744 KB Output is correct
8 Correct 24 ms 31744 KB Output is correct
9 Correct 22 ms 31744 KB Output is correct
10 Correct 22 ms 31744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 31744 KB Output is correct
2 Correct 22 ms 31744 KB Output is correct
3 Correct 22 ms 31744 KB Output is correct
4 Correct 23 ms 31744 KB Output is correct
5 Correct 22 ms 31736 KB Output is correct
6 Correct 22 ms 31744 KB Output is correct
7 Correct 22 ms 31744 KB Output is correct
8 Correct 24 ms 31744 KB Output is correct
9 Correct 22 ms 31744 KB Output is correct
10 Correct 22 ms 31744 KB Output is correct
11 Correct 1243 ms 61068 KB Output is correct
12 Correct 1271 ms 60080 KB Output is correct
13 Correct 1202 ms 53468 KB Output is correct
14 Correct 1331 ms 52488 KB Output is correct
15 Correct 1405 ms 52860 KB Output is correct
16 Correct 1392 ms 52560 KB Output is correct
17 Correct 1389 ms 51784 KB Output is correct
18 Correct 1679 ms 64828 KB Output is correct
19 Correct 1112 ms 50780 KB Output is correct
20 Correct 1313 ms 60904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 31744 KB Output is correct
2 Correct 22 ms 31744 KB Output is correct
3 Correct 22 ms 31744 KB Output is correct
4 Correct 23 ms 31744 KB Output is correct
5 Correct 22 ms 31736 KB Output is correct
6 Correct 22 ms 31744 KB Output is correct
7 Correct 22 ms 31744 KB Output is correct
8 Correct 24 ms 31744 KB Output is correct
9 Correct 22 ms 31744 KB Output is correct
10 Correct 22 ms 31744 KB Output is correct
11 Correct 1243 ms 61068 KB Output is correct
12 Correct 1271 ms 60080 KB Output is correct
13 Correct 1202 ms 53468 KB Output is correct
14 Correct 1331 ms 52488 KB Output is correct
15 Correct 1405 ms 52860 KB Output is correct
16 Correct 1392 ms 52560 KB Output is correct
17 Correct 1389 ms 51784 KB Output is correct
18 Correct 1679 ms 64828 KB Output is correct
19 Correct 1112 ms 50780 KB Output is correct
20 Correct 1313 ms 60904 KB Output is correct
21 Correct 1368 ms 65144 KB Output is correct
22 Correct 1296 ms 60852 KB Output is correct
23 Correct 1282 ms 53844 KB Output is correct
24 Correct 1362 ms 52892 KB Output is correct
25 Correct 1421 ms 53912 KB Output is correct
26 Correct 1411 ms 53080 KB Output is correct
27 Correct 1418 ms 52056 KB Output is correct
28 Correct 1683 ms 65084 KB Output is correct
29 Correct 1125 ms 50652 KB Output is correct
30 Correct 1335 ms 62408 KB Output is correct
31 Correct 1429 ms 54428 KB Output is correct
32 Correct 1414 ms 53232 KB Output is correct
33 Correct 1056 ms 52316 KB Output is correct
34 Correct 1389 ms 51832 KB Output is correct
35 Correct 1418 ms 52196 KB Output is correct
36 Correct 471 ms 56164 KB Output is correct
37 Correct 1193 ms 61288 KB Output is correct
38 Correct 1153 ms 50516 KB Output is correct
39 Correct 1298 ms 51624 KB Output is correct
40 Correct 1372 ms 52184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 31744 KB Output is correct
2 Correct 22 ms 31744 KB Output is correct
3 Correct 22 ms 31744 KB Output is correct
4 Correct 23 ms 31744 KB Output is correct
5 Correct 22 ms 31736 KB Output is correct
6 Correct 22 ms 31744 KB Output is correct
7 Correct 22 ms 31744 KB Output is correct
8 Correct 24 ms 31744 KB Output is correct
9 Correct 22 ms 31744 KB Output is correct
10 Correct 22 ms 31744 KB Output is correct
11 Runtime error 137 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 31744 KB Output is correct
2 Correct 22 ms 31744 KB Output is correct
3 Correct 22 ms 31744 KB Output is correct
4 Correct 23 ms 31744 KB Output is correct
5 Correct 22 ms 31736 KB Output is correct
6 Correct 22 ms 31744 KB Output is correct
7 Correct 22 ms 31744 KB Output is correct
8 Correct 24 ms 31744 KB Output is correct
9 Correct 22 ms 31744 KB Output is correct
10 Correct 22 ms 31744 KB Output is correct
11 Correct 1243 ms 61068 KB Output is correct
12 Correct 1271 ms 60080 KB Output is correct
13 Correct 1202 ms 53468 KB Output is correct
14 Correct 1331 ms 52488 KB Output is correct
15 Correct 1405 ms 52860 KB Output is correct
16 Correct 1392 ms 52560 KB Output is correct
17 Correct 1389 ms 51784 KB Output is correct
18 Correct 1679 ms 64828 KB Output is correct
19 Correct 1112 ms 50780 KB Output is correct
20 Correct 1313 ms 60904 KB Output is correct
21 Correct 1368 ms 65144 KB Output is correct
22 Correct 1296 ms 60852 KB Output is correct
23 Correct 1282 ms 53844 KB Output is correct
24 Correct 1362 ms 52892 KB Output is correct
25 Correct 1421 ms 53912 KB Output is correct
26 Correct 1411 ms 53080 KB Output is correct
27 Correct 1418 ms 52056 KB Output is correct
28 Correct 1683 ms 65084 KB Output is correct
29 Correct 1125 ms 50652 KB Output is correct
30 Correct 1335 ms 62408 KB Output is correct
31 Correct 1429 ms 54428 KB Output is correct
32 Correct 1414 ms 53232 KB Output is correct
33 Correct 1056 ms 52316 KB Output is correct
34 Correct 1389 ms 51832 KB Output is correct
35 Correct 1418 ms 52196 KB Output is correct
36 Correct 471 ms 56164 KB Output is correct
37 Correct 1193 ms 61288 KB Output is correct
38 Correct 1153 ms 50516 KB Output is correct
39 Correct 1298 ms 51624 KB Output is correct
40 Correct 1372 ms 52184 KB Output is correct
41 Runtime error 137 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
42 Halted 0 ms 0 KB -