Submission #160132

# Submission time Handle Problem Language Result Execution time Memory
160132 2019-10-26T06:32:30 Z GioChkhaidze Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
820 ms 65536 KB
#include <bits/stdc++.h>
using namespace std;
const int Q=1e6+5;
const int M20=1048576;
const int M13=1594323;
int n,q,C[M20+5],M[20],dp[M13+5],ANS[Q],Y[Q],Last2[M13+5];	
bool Bo[Q][(1<<7)+5];
main () { 
	ios::sync_with_stdio(false);
	cin>>n>>q;
	
	M[0]=1;
	for (int i=1; i<=13; i++) 
		M[i]=M[i-1]*3;
	
	int Mid=13;
	
	if (n<=Mid) {	
		for (int i=0; i<(1<<n); i++) {
			char c;
			cin>>c;
			C[i]=c-'0';
			
			int x=0;
			for (int j=0; j<n; j++) 
				if (i&(1<<j)) x+=M[j];
			dp[x]+=C[i];
		}
		
		for (int i=0; i<M[n]; i++) {
			int x=i;
			for (int j=n-1; j>=0; j--) 
				if (x-2*M[j]>=0) x-=2*M[j],dp[i]=(dp[i-2*M[j]]+dp[i-M[j]]);	
					else
				if (x-M[j]>=0) x-=M[j];
		}
		
		for (int i=1; i<=q; i++) {
			string s;
			int x=0;
			cin>>s;	
					
			for (int j=0; j<s.size(); j++) 
				if (s[s.size()-j-1]=='?') x+=2*M[j];	
					else
				if (s[s.size()-j-1]=='1') x+=M[j];
			
			printf("%d\n",dp[x]);
		}
	}
		else {
		for (int i=0; i<(1<<n); i++) {
			char c;
			cin>>c;
			C[i]=c-'0';
		}

		int K=(n-Mid);
		
		for (int i=1; i<=q; i++) {
			string S;
			cin>>S;	
			
			for (int k=S.size()-1; k>=K; k--) {
				if (S[k]=='?') Y[i]+=2*M[S.size()-1-k];	
					else
				if (S[k]=='1') Y[i]+=M[S.size()-1-k];
			}
	
			for (int j=0; j<(1<<K); j++) {
				int ko=0;
				for (int k=0; k<K; k++)
					if ((S[K-k-1]=='1' && (!((1<<k)&j))) ||
					    (S[K-k-1]=='0' && (((1<<k)&j)))) { ko=1; break; }
					    
				Bo[i][j]=(!ko);	    
			}
		}
		
		for (int i=0; i<M[Mid]; i++) {
			int x=i;
			Last2[i]=-1;
			for (int j=Mid-1; j>=0; j--) 
				if (x>=2*M[j]) { Last2[i]=j; break; }
						else
				if (x>=M[j]) x-=M[j];
		}
		
		for (int i=0; i<(1<<K); i++) {
			for (int j=0; j<M[Mid]; j++)
				dp[j]=0;

			for (int j=0; j<(1<<Mid); j++) {
				int x=0;
				for (int k=0; k<Mid; k++) 
					if (j&(1<<k)) x+=M[k];
				dp[x]=C[i*(1<<Mid)+j];
			}
			
			for (int j=0; j<M[Mid]; j++) {
				int x=j;
				if (Last2[j]==-1) continue;
				dp[j]=(dp[j-2*M[Last2[j]]]+dp[j-M[Last2[j]]]);	
			}
			
			for (int j=1; j<=q; j++) {
				if (!Bo[j][i]) continue;
				ANS[j]+=dp[Y[j]];
			}
		}
		
		for (int i=1; i<=q; i++) 
			printf("%d\n",ANS[i]);		
	}
}

Compilation message

snake_escaping.cpp:8:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () { 
       ^
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:43:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int j=0; j<s.size(); j++) 
                  ~^~~~~~~~~
snake_escaping.cpp:101:9: warning: unused variable 'x' [-Wunused-variable]
     int x=j;
         ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 632 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 4 ms 632 KB Output is correct
4 Correct 4 ms 636 KB Output is correct
5 Correct 4 ms 632 KB Output is correct
6 Correct 4 ms 632 KB Output is correct
7 Correct 4 ms 632 KB Output is correct
8 Correct 4 ms 632 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
10 Correct 4 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 632 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 4 ms 632 KB Output is correct
4 Correct 4 ms 636 KB Output is correct
5 Correct 4 ms 632 KB Output is correct
6 Correct 4 ms 632 KB Output is correct
7 Correct 4 ms 632 KB Output is correct
8 Correct 4 ms 632 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
10 Correct 4 ms 632 KB Output is correct
11 Correct 247 ms 4688 KB Output is correct
12 Correct 268 ms 4216 KB Output is correct
13 Correct 275 ms 3604 KB Output is correct
14 Correct 277 ms 3832 KB Output is correct
15 Correct 275 ms 4720 KB Output is correct
16 Correct 293 ms 3832 KB Output is correct
17 Correct 290 ms 3704 KB Output is correct
18 Correct 206 ms 5752 KB Output is correct
19 Correct 251 ms 2552 KB Output is correct
20 Correct 264 ms 4376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 632 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 4 ms 632 KB Output is correct
4 Correct 4 ms 636 KB Output is correct
5 Correct 4 ms 632 KB Output is correct
6 Correct 4 ms 632 KB Output is correct
7 Correct 4 ms 632 KB Output is correct
8 Correct 4 ms 632 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
10 Correct 4 ms 632 KB Output is correct
11 Correct 247 ms 4688 KB Output is correct
12 Correct 268 ms 4216 KB Output is correct
13 Correct 275 ms 3604 KB Output is correct
14 Correct 277 ms 3832 KB Output is correct
15 Correct 275 ms 4720 KB Output is correct
16 Correct 293 ms 3832 KB Output is correct
17 Correct 290 ms 3704 KB Output is correct
18 Correct 206 ms 5752 KB Output is correct
19 Correct 251 ms 2552 KB Output is correct
20 Correct 264 ms 4376 KB Output is correct
21 Correct 330 ms 10676 KB Output is correct
22 Correct 356 ms 10920 KB Output is correct
23 Correct 447 ms 9792 KB Output is correct
24 Correct 436 ms 9728 KB Output is correct
25 Correct 377 ms 11732 KB Output is correct
26 Correct 451 ms 10232 KB Output is correct
27 Correct 457 ms 10488 KB Output is correct
28 Correct 268 ms 12664 KB Output is correct
29 Correct 335 ms 8696 KB Output is correct
30 Correct 367 ms 10872 KB Output is correct
31 Correct 436 ms 10752 KB Output is correct
32 Correct 427 ms 10728 KB Output is correct
33 Correct 359 ms 9464 KB Output is correct
34 Correct 439 ms 9720 KB Output is correct
35 Correct 469 ms 10232 KB Output is correct
36 Correct 256 ms 8696 KB Output is correct
37 Correct 337 ms 10760 KB Output is correct
38 Correct 346 ms 8696 KB Output is correct
39 Correct 462 ms 9984 KB Output is correct
40 Correct 431 ms 9692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 632 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 4 ms 632 KB Output is correct
4 Correct 4 ms 636 KB Output is correct
5 Correct 4 ms 632 KB Output is correct
6 Correct 4 ms 632 KB Output is correct
7 Correct 4 ms 632 KB Output is correct
8 Correct 4 ms 632 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
10 Correct 4 ms 632 KB Output is correct
11 Correct 777 ms 24312 KB Output is correct
12 Correct 806 ms 26368 KB Output is correct
13 Correct 771 ms 26084 KB Output is correct
14 Correct 760 ms 26256 KB Output is correct
15 Correct 756 ms 26344 KB Output is correct
16 Correct 741 ms 26232 KB Output is correct
17 Correct 775 ms 26360 KB Output is correct
18 Correct 793 ms 26360 KB Output is correct
19 Correct 738 ms 26104 KB Output is correct
20 Correct 765 ms 26360 KB Output is correct
21 Correct 776 ms 26360 KB Output is correct
22 Correct 770 ms 26164 KB Output is correct
23 Correct 748 ms 26104 KB Output is correct
24 Correct 787 ms 26232 KB Output is correct
25 Correct 820 ms 26104 KB Output is correct
26 Correct 744 ms 26104 KB Output is correct
27 Correct 787 ms 26104 KB Output is correct
28 Correct 747 ms 26104 KB Output is correct
29 Correct 765 ms 26188 KB Output is correct
30 Correct 779 ms 26232 KB Output is correct
31 Correct 747 ms 26312 KB Output is correct
32 Correct 784 ms 26104 KB Output is correct
33 Correct 781 ms 26104 KB Output is correct
34 Correct 739 ms 25844 KB Output is correct
35 Correct 764 ms 26232 KB Output is correct
36 Correct 754 ms 26400 KB Output is correct
37 Correct 762 ms 26232 KB Output is correct
38 Correct 768 ms 26232 KB Output is correct
39 Correct 777 ms 26232 KB Output is correct
40 Correct 780 ms 26232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 632 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 4 ms 632 KB Output is correct
4 Correct 4 ms 636 KB Output is correct
5 Correct 4 ms 632 KB Output is correct
6 Correct 4 ms 632 KB Output is correct
7 Correct 4 ms 632 KB Output is correct
8 Correct 4 ms 632 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
10 Correct 4 ms 632 KB Output is correct
11 Correct 247 ms 4688 KB Output is correct
12 Correct 268 ms 4216 KB Output is correct
13 Correct 275 ms 3604 KB Output is correct
14 Correct 277 ms 3832 KB Output is correct
15 Correct 275 ms 4720 KB Output is correct
16 Correct 293 ms 3832 KB Output is correct
17 Correct 290 ms 3704 KB Output is correct
18 Correct 206 ms 5752 KB Output is correct
19 Correct 251 ms 2552 KB Output is correct
20 Correct 264 ms 4376 KB Output is correct
21 Correct 330 ms 10676 KB Output is correct
22 Correct 356 ms 10920 KB Output is correct
23 Correct 447 ms 9792 KB Output is correct
24 Correct 436 ms 9728 KB Output is correct
25 Correct 377 ms 11732 KB Output is correct
26 Correct 451 ms 10232 KB Output is correct
27 Correct 457 ms 10488 KB Output is correct
28 Correct 268 ms 12664 KB Output is correct
29 Correct 335 ms 8696 KB Output is correct
30 Correct 367 ms 10872 KB Output is correct
31 Correct 436 ms 10752 KB Output is correct
32 Correct 427 ms 10728 KB Output is correct
33 Correct 359 ms 9464 KB Output is correct
34 Correct 439 ms 9720 KB Output is correct
35 Correct 469 ms 10232 KB Output is correct
36 Correct 256 ms 8696 KB Output is correct
37 Correct 337 ms 10760 KB Output is correct
38 Correct 346 ms 8696 KB Output is correct
39 Correct 462 ms 9984 KB Output is correct
40 Correct 431 ms 9692 KB Output is correct
41 Correct 777 ms 24312 KB Output is correct
42 Correct 806 ms 26368 KB Output is correct
43 Correct 771 ms 26084 KB Output is correct
44 Correct 760 ms 26256 KB Output is correct
45 Correct 756 ms 26344 KB Output is correct
46 Correct 741 ms 26232 KB Output is correct
47 Correct 775 ms 26360 KB Output is correct
48 Correct 793 ms 26360 KB Output is correct
49 Correct 738 ms 26104 KB Output is correct
50 Correct 765 ms 26360 KB Output is correct
51 Correct 776 ms 26360 KB Output is correct
52 Correct 770 ms 26164 KB Output is correct
53 Correct 748 ms 26104 KB Output is correct
54 Correct 787 ms 26232 KB Output is correct
55 Correct 820 ms 26104 KB Output is correct
56 Correct 744 ms 26104 KB Output is correct
57 Correct 787 ms 26104 KB Output is correct
58 Correct 747 ms 26104 KB Output is correct
59 Correct 765 ms 26188 KB Output is correct
60 Correct 779 ms 26232 KB Output is correct
61 Correct 747 ms 26312 KB Output is correct
62 Correct 784 ms 26104 KB Output is correct
63 Correct 781 ms 26104 KB Output is correct
64 Correct 739 ms 25844 KB Output is correct
65 Correct 764 ms 26232 KB Output is correct
66 Correct 754 ms 26400 KB Output is correct
67 Correct 762 ms 26232 KB Output is correct
68 Correct 768 ms 26232 KB Output is correct
69 Correct 777 ms 26232 KB Output is correct
70 Correct 780 ms 26232 KB Output is correct
71 Runtime error 804 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
72 Halted 0 ms 0 KB -