답안 #535331

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535331 2022-03-10T03:23:49 Z amunduzbaev Snake Escaping (JOI18_snake_escaping) C++17
22 / 100
909 ms 65536 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
 
const int N = 20;
int dp[2][1 << N ][N];
int a[1 << N];
 
signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n, m; cin>>n>>m;
	for(int i=0;i<(1 << n);i++){
		char c; cin>>c;
		a[i] = c - '0';
	}
	for(int mask=(1 << n) - 1;~mask;mask--){
		int res = a[mask];
		for(int i=n-1;~i;i--){
			if(!(mask >> i & 1)){
				res += dp[0][mask | (1 << i)][i];
			}
			dp[0][mask][i] = res;
		}
	}
	
	for(int mask=0;mask<(1 << n);mask++){
		int res = a[mask];
		for(int i=n-1;~i;i--){
			if(mask >> i & 1){
				res += dp[1][mask ^ (1 << i)][i];
			}
			dp[1][mask][i] = res;
		}
	}
	
	ar<int, 2> c {};
	int res, in, t;
	vector<int> p;
	while(m--){
		string s; cin>>s;
		reverse(s.begin(), s.end());
		c[0] = c[1] = c[2] = 0;
		for(int i=0;i<n;i++){
			if(s[i] == '0') c[0]++;
			if(s[i] == '1') c[1]++;
			if(s[i] == '?') c[2]++;
		}
		
		res = 0; p.clear();
		if(c[2] <= 6){
			in = 0;
			for(int i=0;i<n;i++){
				if(s[i] == '0' || s[i] == '1') in |= ((s[i] - '0') << i);
				else p.push_back(i);
			}
			
			for(int mask=0;mask < (1 << c[2]);mask++){
				int tot = in;
				for(int j=0;j<c[2];j++){
					if(mask >> j & 1) tot |= (1 << p[j]);
				}
				
				res += a[tot];
			}
		} 
		else if(c[0] <= 6) {
			t = 0, in = 0;
			for(int i=0;i<n;i++){
				if(s[i] == '1') in |= (1 << i);
				if(s[i] == '0') p.push_back(i);
			}
			
			for(int mask=0;mask < (1 << c[0]);mask++){
				int tot = in;
				for(int j=0;j<c[0];j++){
					if(mask >> j & 1) tot |= (1 << p[j]);
				}
				
				if(__builtin_popcount(mask)&1) res -= dp[t][tot][0];
				else res += dp[t][tot][0];
			}
		} else {
			t = 1, in = (1 << n) - 1;
			for(int i=0;i<n;i++){
				if(s[i] == '0') in ^= (1 << i);
				if(s[i] == '1') p.push_back(i);
			}
			
			for(int mask=0;mask<(1 << c[1]);mask++){
				int tot = in;
				for(int j=0;j<c[1];j++){
					if(mask >> j & 1) tot ^= (1 << p[j]);
				}
				
				if(__builtin_popcount(mask)&1) res -= dp[t][tot][0];
				else res += dp[t][tot][0];
			}
		}
		
		cout<<res<<"\n";
	}
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:44:22: warning: array subscript 2 is outside array bounds of 'std::array<int, 2> [1]' [-Warray-bounds]
   44 |   c[0] = c[1] = c[2] = 0;
snake_escaping.cpp:38:13: note: while referencing 'c'
   38 |  ar<int, 2> c {};
      |             ^
snake_escaping.cpp:48:24: warning: array subscript 2 is outside array bounds of 'std::array<int, 2> [1]' [-Warray-bounds]
   48 |    if(s[i] == '?') c[2]++;
snake_escaping.cpp:38:13: note: while referencing 'c'
   38 |  ar<int, 2> c {};
      |             ^
snake_escaping.cpp:48:24: warning: array subscript 2 is outside array bounds of 'std::array<int, 2> [1]' [-Warray-bounds]
   48 |    if(s[i] == '?') c[2]++;
snake_escaping.cpp:38:13: note: while referencing 'c'
   38 |  ar<int, 2> c {};
      |             ^
snake_escaping.cpp:52:11: warning: array subscript 2 is outside array bounds of 'std::array<int, 2> [1]' [-Warray-bounds]
   52 |   if(c[2] <= 6){
snake_escaping.cpp:38:13: note: while referencing 'c'
   38 |  ar<int, 2> c {};
      |             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 392 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 420 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 392 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 420 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 456 ms 4420 KB Output is correct
12 Correct 451 ms 4080 KB Output is correct
13 Correct 331 ms 3388 KB Output is correct
14 Correct 279 ms 3464 KB Output is correct
15 Correct 769 ms 4592 KB Output is correct
16 Correct 396 ms 3636 KB Output is correct
17 Correct 414 ms 3576 KB Output is correct
18 Correct 175 ms 5432 KB Output is correct
19 Correct 209 ms 2440 KB Output is correct
20 Correct 478 ms 4232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 392 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 420 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 456 ms 4420 KB Output is correct
12 Correct 451 ms 4080 KB Output is correct
13 Correct 331 ms 3388 KB Output is correct
14 Correct 279 ms 3464 KB Output is correct
15 Correct 769 ms 4592 KB Output is correct
16 Correct 396 ms 3636 KB Output is correct
17 Correct 414 ms 3576 KB Output is correct
18 Correct 175 ms 5432 KB Output is correct
19 Correct 209 ms 2440 KB Output is correct
20 Correct 478 ms 4232 KB Output is correct
21 Correct 279 ms 5632 KB Output is correct
22 Correct 720 ms 5876 KB Output is correct
23 Correct 476 ms 4816 KB Output is correct
24 Correct 412 ms 4692 KB Output is correct
25 Correct 359 ms 6604 KB Output is correct
26 Correct 589 ms 5128 KB Output is correct
27 Correct 555 ms 5292 KB Output is correct
28 Correct 180 ms 7612 KB Output is correct
29 Correct 267 ms 3576 KB Output is correct
30 Correct 486 ms 5792 KB Output is correct
31 Correct 909 ms 5596 KB Output is correct
32 Correct 577 ms 5692 KB Output is correct
33 Correct 362 ms 4488 KB Output is correct
34 Correct 410 ms 4672 KB Output is correct
35 Correct 547 ms 5156 KB Output is correct
36 Correct 198 ms 3536 KB Output is correct
37 Correct 813 ms 5560 KB Output is correct
38 Correct 240 ms 3636 KB Output is correct
39 Correct 463 ms 4804 KB Output is correct
40 Correct 406 ms 4556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 392 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 420 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Runtime error 78 ms 65536 KB Execution killed with signal 9
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 392 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 420 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 456 ms 4420 KB Output is correct
12 Correct 451 ms 4080 KB Output is correct
13 Correct 331 ms 3388 KB Output is correct
14 Correct 279 ms 3464 KB Output is correct
15 Correct 769 ms 4592 KB Output is correct
16 Correct 396 ms 3636 KB Output is correct
17 Correct 414 ms 3576 KB Output is correct
18 Correct 175 ms 5432 KB Output is correct
19 Correct 209 ms 2440 KB Output is correct
20 Correct 478 ms 4232 KB Output is correct
21 Correct 279 ms 5632 KB Output is correct
22 Correct 720 ms 5876 KB Output is correct
23 Correct 476 ms 4816 KB Output is correct
24 Correct 412 ms 4692 KB Output is correct
25 Correct 359 ms 6604 KB Output is correct
26 Correct 589 ms 5128 KB Output is correct
27 Correct 555 ms 5292 KB Output is correct
28 Correct 180 ms 7612 KB Output is correct
29 Correct 267 ms 3576 KB Output is correct
30 Correct 486 ms 5792 KB Output is correct
31 Correct 909 ms 5596 KB Output is correct
32 Correct 577 ms 5692 KB Output is correct
33 Correct 362 ms 4488 KB Output is correct
34 Correct 410 ms 4672 KB Output is correct
35 Correct 547 ms 5156 KB Output is correct
36 Correct 198 ms 3536 KB Output is correct
37 Correct 813 ms 5560 KB Output is correct
38 Correct 240 ms 3636 KB Output is correct
39 Correct 463 ms 4804 KB Output is correct
40 Correct 406 ms 4556 KB Output is correct
41 Runtime error 78 ms 65536 KB Execution killed with signal 9
42 Halted 0 ms 0 KB -