Submission #66817

# Submission time Handle Problem Language Result Execution time Memory
66817 2018-08-12T11:01:51 Z hamzqq9 Snake Escaping (JOI18_snake_escaping) C++14
12 / 100
2000 ms 45704 KB
#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define umax(x,y) x=max(x,y)
#define umin(x,y) x=min(x,y)
#define ll long long
#define ii pair<int,int>
#define iii pair<int,ii>
#define sz(x) ((int) x.size())
#define orta ((bas+son)>>1)
#define all(x) x.begin(),x.end()
#define dbgs(x) cerr<<(#x)<<" --> "<<(x)<<" "
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<endl;getchar()
#define pw(x) (1<<(x))
#define inf 100000000000000
#define MOD 1000000007
#define N 100005
#define MAX 10000006
#define L 13
#define R 7
#define LOG 22
using namespace std;

unsigned short int ans[pw(L)][2189];
char val[pw(20)+3];
short int po3[12];
int l,q,n;
int res;
short int value;
char s[pw(20)+3];
int po2[27];
int sz;
pair<char,bitset<L> > stck[pw(L)+5];
bitset<L> bs,temp;

int get_pos_of_2(int value) {

	int cur=0;

	while(value) {

		if(value%3==2) return cur;

		value/=3;

		cur++;

	}

	return -1;

}

int get_val(int bas,int son) {

	int cur=0;

	for(int i=bas;i<=son;i++) {

		cur+=po3[son-i]*(s[i]=='?'?2:(s[i]-'0'));

	}

	return cur;

}

int get_2_rep(int val) {

	int res=0;

	int curpw=1;

	while(val) {

		res+=curpw*(val%3);

		curpw*=2;

		val/=3;

	}

	return res;

}

void back_track(char bit,bitset<L> val) {

	stck[sz++]={bit,val};

	while(sz) {

		sz--;

		char bit=stck[sz].st;
		temp=stck[sz].nd;

		if(bit==-1) {

			res+=ans[(unsigned short)temp.to_ulong()][value];

			continue ;

		}

		if(s[bit]!='?') {
		
			temp[L-1-bit]=(s[bit]=='1');

			stck[sz++]={bit-1,temp};
		
		}
		else {

			stck[sz++]={bit-1,temp};

			temp[L-1-bit]=1;

			stck[sz++]={bit-1,temp};
			

		}

	}

} 

void pre_calc() {

	for(int i=0;i<po3[R];i++) {

		char pos=get_pos_of_2(i);

		short int temp=-1;

		if(pos==-1) temp=get_2_rep(i);

		for(int j=0;j<po2[L];j++) {

			if(pos==-1) {

				ans[j][i]=val[temp+po2[R]*j];

			}
			else {

				ans[j][i]=ans[j][i-po3[pos]]+ans[j][i-po3[pos]*2];

			}

		}

	}

}

int main() {

//	freopen("input.txt","r",stdin);

	scanf("%d %d",&l,&q);

	po3[0]=1;

	for(int i=1;i<=10;i++) po3[i]=po3[i-1]*3;

	for(int i=0;i<25;i++) po2[i]=pw(i);
 
	n=po2[l];

	scanf("%s",s);

	for(int i=0;i<n;i++) {

		val[i]=s[i]-'0';

	}

	pre_calc();

	while(q--) {

		scanf("%s",s+20-l);

		for(int i=0;i<20-l;i++) s[i]='0';

		res=0;

		value=get_val(L,19);

		back_track(L-1,bs);

		printf("%d\n",res);

	}

}

Compilation message

snake_escaping.cpp: In function 'void back_track(char, std::bitset<13>)':
snake_escaping.cpp:109:11: warning: array subscript has type 'char' [-Wchar-subscripts]
   if(s[bit]!='?') {
           ^
snake_escaping.cpp:111:24: warning: array subscript has type 'char' [-Wchar-subscripts]
    temp[L-1-bit]=(s[bit]=='1');
                        ^
snake_escaping.cpp: In function 'void pre_calc()':
snake_escaping.cpp:150:31: warning: array subscript has type 'char' [-Wchar-subscripts]
     ans[j][i]=ans[j][i-po3[pos]]+ans[j][i-po3[pos]*2];
                               ^
snake_escaping.cpp:150:50: warning: array subscript has type 'char' [-Wchar-subscripts]
     ans[j][i]=ans[j][i-po3[pos]]+ans[j][i-po3[pos]*2];
                                                  ^
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:164:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&l,&q);
  ~~~~~^~~~~~~~~~~~~~~
snake_escaping.cpp:174:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s",s);
  ~~~~~^~~~~~~~
snake_escaping.cpp:186:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s",s+20-l);
   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 314 ms 35448 KB Output is correct
2 Correct 298 ms 35568 KB Output is correct
3 Correct 282 ms 35568 KB Output is correct
4 Correct 302 ms 35684 KB Output is correct
5 Correct 321 ms 35684 KB Output is correct
6 Correct 315 ms 35688 KB Output is correct
7 Correct 277 ms 35688 KB Output is correct
8 Correct 308 ms 35748 KB Output is correct
9 Correct 386 ms 35748 KB Output is correct
10 Correct 326 ms 35824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 35448 KB Output is correct
2 Correct 298 ms 35568 KB Output is correct
3 Correct 282 ms 35568 KB Output is correct
4 Correct 302 ms 35684 KB Output is correct
5 Correct 321 ms 35684 KB Output is correct
6 Correct 315 ms 35688 KB Output is correct
7 Correct 277 ms 35688 KB Output is correct
8 Correct 308 ms 35748 KB Output is correct
9 Correct 386 ms 35748 KB Output is correct
10 Correct 326 ms 35824 KB Output is correct
11 Correct 797 ms 40828 KB Output is correct
12 Correct 835 ms 40828 KB Output is correct
13 Correct 731 ms 40828 KB Output is correct
14 Correct 879 ms 41748 KB Output is correct
15 Correct 917 ms 42640 KB Output is correct
16 Correct 718 ms 42640 KB Output is correct
17 Correct 768 ms 42640 KB Output is correct
18 Correct 1006 ms 43612 KB Output is correct
19 Correct 597 ms 43612 KB Output is correct
20 Correct 806 ms 43612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 35448 KB Output is correct
2 Correct 298 ms 35568 KB Output is correct
3 Correct 282 ms 35568 KB Output is correct
4 Correct 302 ms 35684 KB Output is correct
5 Correct 321 ms 35684 KB Output is correct
6 Correct 315 ms 35688 KB Output is correct
7 Correct 277 ms 35688 KB Output is correct
8 Correct 308 ms 35748 KB Output is correct
9 Correct 386 ms 35748 KB Output is correct
10 Correct 326 ms 35824 KB Output is correct
11 Correct 797 ms 40828 KB Output is correct
12 Correct 835 ms 40828 KB Output is correct
13 Correct 731 ms 40828 KB Output is correct
14 Correct 879 ms 41748 KB Output is correct
15 Correct 917 ms 42640 KB Output is correct
16 Correct 718 ms 42640 KB Output is correct
17 Correct 768 ms 42640 KB Output is correct
18 Correct 1006 ms 43612 KB Output is correct
19 Correct 597 ms 43612 KB Output is correct
20 Correct 806 ms 43612 KB Output is correct
21 Correct 1159 ms 43612 KB Output is correct
22 Correct 1272 ms 44328 KB Output is correct
23 Correct 996 ms 44328 KB Output is correct
24 Correct 1044 ms 44328 KB Output is correct
25 Correct 1523 ms 45704 KB Output is correct
26 Correct 1044 ms 45704 KB Output is correct
27 Correct 980 ms 45704 KB Output is correct
28 Execution timed out 2076 ms 45704 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 314 ms 35448 KB Output is correct
2 Correct 298 ms 35568 KB Output is correct
3 Correct 282 ms 35568 KB Output is correct
4 Correct 302 ms 35684 KB Output is correct
5 Correct 321 ms 35684 KB Output is correct
6 Correct 315 ms 35688 KB Output is correct
7 Correct 277 ms 35688 KB Output is correct
8 Correct 308 ms 35748 KB Output is correct
9 Correct 386 ms 35748 KB Output is correct
10 Correct 326 ms 35824 KB Output is correct
11 Correct 587 ms 45704 KB Output is correct
12 Correct 777 ms 45704 KB Output is correct
13 Correct 449 ms 45704 KB Output is correct
14 Correct 443 ms 45704 KB Output is correct
15 Correct 1867 ms 45704 KB Output is correct
16 Correct 550 ms 45704 KB Output is correct
17 Correct 543 ms 45704 KB Output is correct
18 Execution timed out 2055 ms 45704 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 314 ms 35448 KB Output is correct
2 Correct 298 ms 35568 KB Output is correct
3 Correct 282 ms 35568 KB Output is correct
4 Correct 302 ms 35684 KB Output is correct
5 Correct 321 ms 35684 KB Output is correct
6 Correct 315 ms 35688 KB Output is correct
7 Correct 277 ms 35688 KB Output is correct
8 Correct 308 ms 35748 KB Output is correct
9 Correct 386 ms 35748 KB Output is correct
10 Correct 326 ms 35824 KB Output is correct
11 Correct 797 ms 40828 KB Output is correct
12 Correct 835 ms 40828 KB Output is correct
13 Correct 731 ms 40828 KB Output is correct
14 Correct 879 ms 41748 KB Output is correct
15 Correct 917 ms 42640 KB Output is correct
16 Correct 718 ms 42640 KB Output is correct
17 Correct 768 ms 42640 KB Output is correct
18 Correct 1006 ms 43612 KB Output is correct
19 Correct 597 ms 43612 KB Output is correct
20 Correct 806 ms 43612 KB Output is correct
21 Correct 1159 ms 43612 KB Output is correct
22 Correct 1272 ms 44328 KB Output is correct
23 Correct 996 ms 44328 KB Output is correct
24 Correct 1044 ms 44328 KB Output is correct
25 Correct 1523 ms 45704 KB Output is correct
26 Correct 1044 ms 45704 KB Output is correct
27 Correct 980 ms 45704 KB Output is correct
28 Execution timed out 2076 ms 45704 KB Time limit exceeded
29 Halted 0 ms 0 KB -