Submission #41282

# Submission time Handle Problem Language Result Execution time Memory
41282 2018-02-15T11:18:42 Z Kerim Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 65536 KB
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx") 
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
#define BLOK 13
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int n,q;
const int N=(1<<20)+5;
const int M=1594330;
int arr[N];
int dp[N*2],pw[22],ans[N];
char s[N],t[22];
vector<PII>adj[22];
bitset<N>ok[130];
int two[M];
void rec(int pos,int mask,int bit,int last){
	if(pos==min(n,BLOK)){
		adj[bit].pb(mp(mask,last));
		return;
	}
	rec(pos+1,mask*3,bit,last);
	rec(pos+1,mask*3+1,bit,last);
	rec(pos+1,mask*3+2,bit+1,min(n,BLOK)-pos-1);
}
//~ int two(int x){
	//~ int bit=0,res=0;
	//~ while(x>=1){
		//~ assert(x%3<2);
		//~ if(x%3)
			//~ res+=(1<<bit);
		//~ x/=3;bit++;
	//~ }
	//~ return res;
//~ }
void f(int l,int r,int mask,int ind){
	if(l==r){
		ok[mask][ind]=1;
		//~ query[mask].pb(mp(suf,ind));
		return;
	}
	if(t[l]=='0')
		f(l+1,r,mask*2,ind);
	if(t[l]=='1')
		f(l+1,r,mask*2+1,ind);
	if(t[l]=='?'){
		f(l+1,r,mask*2,ind);
		f(l+1,r,mask*2+1,ind);
	}	
}
void pre(int pos,int uc,int iki){
	if(pos==13){
		two[uc]=iki;
		return;
	}
	for(int i=0;i<3;i++)
		pre(pos+1,uc*3+i,iki*2+i);
}
int main(){
    //~ freopen("file.in", "r", stdin);
    pre(0,0,0);
    scanf("%d%d",&n,&q);
    scanf("%s",s);
    pw[0]=1;
    for(int i=1;i<=BLOK;i++)
		pw[i]=pw[i-1]*3;
    if(n<=BLOK){
		rec(0,0,0,-1);
		tr(it,adj[0])
			dp[it->ff]=s[two[it->ff]]-'0';
		for(int j=1;j<=n;j++)
			tr(it,adj[j])
				dp[it->ff]=dp[it->ff-pw[it->ss]]+dp[it->ff-2*pw[it->ss]];
		for(int i=1;i<=q;i++){
			scanf("%s",t);
			int base=0;
			for(int j=0;j<n;j++){
				base=base*3;
				if(t[j]=='1')
					base++;
				if(t[j]=='?')
					base+=2;
			}
			printf("%d\n",dp[base]);
		}
	}
	else{
		rec(0,0,0,-1);
		for(int i=1;i<=q;i++){
			scanf("%s",t);
			int base=0;
			for(int j=n-BLOK;j<n;j++){
				base=base*3;
				if(t[j]=='1')
					base++;
				if(t[j]=='?')
					base+=2;
			}
			f(0,n-BLOK,0,i);arr[i]=base;
		}
		int lek=(1<<BLOK);
		for(int mask=0;mask<(1<<(n-BLOK));mask++){
			tr(it,adj[0])
				dp[it->ff]=s[lek*mask+two[it->ff]]-'0';
			for(int i=1;i<=BLOK;i++)
				tr(it,adj[i])
					dp[it->ff]=dp[it->ff-pw[it->ss]]+dp[it->ff-2*pw[it->ss]];
			for(int i=1;i<=q;i++)
				if(ok[mask][i])
					ans[i]+=dp[arr[i]];
		}
		for(int i=1;i<=q;i++)
			printf("%d\n",ans[i]);
	}
	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:81:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&q);
                        ^
snake_escaping.cpp:82:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s",s);
                  ^
snake_escaping.cpp:94:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%s",t);
                 ^
snake_escaping.cpp:109:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%s",t);
                 ^
# Verdict Execution time Memory Grader output
1 Correct 23 ms 7416 KB Output is correct
2 Correct 18 ms 7532 KB Output is correct
3 Correct 18 ms 7744 KB Output is correct
4 Correct 17 ms 7744 KB Output is correct
5 Correct 18 ms 7756 KB Output is correct
6 Correct 18 ms 7756 KB Output is correct
7 Correct 22 ms 7756 KB Output is correct
8 Correct 21 ms 7756 KB Output is correct
9 Correct 21 ms 7824 KB Output is correct
10 Correct 18 ms 7824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 7416 KB Output is correct
2 Correct 18 ms 7532 KB Output is correct
3 Correct 18 ms 7744 KB Output is correct
4 Correct 17 ms 7744 KB Output is correct
5 Correct 18 ms 7756 KB Output is correct
6 Correct 18 ms 7756 KB Output is correct
7 Correct 22 ms 7756 KB Output is correct
8 Correct 21 ms 7756 KB Output is correct
9 Correct 21 ms 7824 KB Output is correct
10 Correct 18 ms 7824 KB Output is correct
11 Correct 280 ms 22628 KB Output is correct
12 Correct 242 ms 32880 KB Output is correct
13 Correct 261 ms 42984 KB Output is correct
14 Correct 263 ms 53840 KB Output is correct
15 Correct 254 ms 65536 KB Output is correct
16 Correct 285 ms 65536 KB Output is correct
17 Correct 289 ms 65536 KB Output is correct
18 Correct 231 ms 65536 KB Output is correct
19 Correct 274 ms 65536 KB Output is correct
20 Correct 286 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 7416 KB Output is correct
2 Correct 18 ms 7532 KB Output is correct
3 Correct 18 ms 7744 KB Output is correct
4 Correct 17 ms 7744 KB Output is correct
5 Correct 18 ms 7756 KB Output is correct
6 Correct 18 ms 7756 KB Output is correct
7 Correct 22 ms 7756 KB Output is correct
8 Correct 21 ms 7756 KB Output is correct
9 Correct 21 ms 7824 KB Output is correct
10 Correct 18 ms 7824 KB Output is correct
11 Correct 280 ms 22628 KB Output is correct
12 Correct 242 ms 32880 KB Output is correct
13 Correct 261 ms 42984 KB Output is correct
14 Correct 263 ms 53840 KB Output is correct
15 Correct 254 ms 65536 KB Output is correct
16 Correct 285 ms 65536 KB Output is correct
17 Correct 289 ms 65536 KB Output is correct
18 Correct 231 ms 65536 KB Output is correct
19 Correct 274 ms 65536 KB Output is correct
20 Correct 286 ms 65536 KB Output is correct
21 Correct 397 ms 65536 KB Output is correct
22 Correct 305 ms 65536 KB Output is correct
23 Correct 388 ms 65536 KB Output is correct
24 Correct 397 ms 65536 KB Output is correct
25 Correct 347 ms 65536 KB Output is correct
26 Correct 443 ms 65536 KB Output is correct
27 Correct 467 ms 65536 KB Output is correct
28 Correct 297 ms 65536 KB Output is correct
29 Correct 354 ms 65536 KB Output is correct
30 Correct 388 ms 65536 KB Output is correct
31 Correct 408 ms 65536 KB Output is correct
32 Correct 426 ms 65536 KB Output is correct
33 Correct 421 ms 65536 KB Output is correct
34 Correct 406 ms 65536 KB Output is correct
35 Correct 449 ms 65536 KB Output is correct
36 Correct 276 ms 65536 KB Output is correct
37 Correct 303 ms 65536 KB Output is correct
38 Correct 388 ms 65536 KB Output is correct
39 Correct 434 ms 65536 KB Output is correct
40 Correct 400 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 7416 KB Output is correct
2 Correct 18 ms 7532 KB Output is correct
3 Correct 18 ms 7744 KB Output is correct
4 Correct 17 ms 7744 KB Output is correct
5 Correct 18 ms 7756 KB Output is correct
6 Correct 18 ms 7756 KB Output is correct
7 Correct 22 ms 7756 KB Output is correct
8 Correct 21 ms 7756 KB Output is correct
9 Correct 21 ms 7824 KB Output is correct
10 Correct 18 ms 7824 KB Output is correct
11 Correct 584 ms 65536 KB Output is correct
12 Correct 588 ms 65536 KB Output is correct
13 Correct 547 ms 65536 KB Output is correct
14 Correct 578 ms 65536 KB Output is correct
15 Correct 599 ms 65536 KB Output is correct
16 Correct 568 ms 65536 KB Output is correct
17 Correct 565 ms 65536 KB Output is correct
18 Correct 580 ms 65536 KB Output is correct
19 Correct 554 ms 65536 KB Output is correct
20 Correct 581 ms 65536 KB Output is correct
21 Correct 589 ms 65536 KB Output is correct
22 Correct 586 ms 65536 KB Output is correct
23 Correct 583 ms 65536 KB Output is correct
24 Correct 573 ms 65536 KB Output is correct
25 Correct 579 ms 65536 KB Output is correct
26 Correct 567 ms 65536 KB Output is correct
27 Correct 606 ms 65536 KB Output is correct
28 Correct 567 ms 65536 KB Output is correct
29 Correct 625 ms 65536 KB Output is correct
30 Correct 626 ms 65536 KB Output is correct
31 Correct 598 ms 65536 KB Output is correct
32 Correct 602 ms 65536 KB Output is correct
33 Correct 590 ms 65536 KB Output is correct
34 Correct 524 ms 65536 KB Output is correct
35 Correct 577 ms 65536 KB Output is correct
36 Correct 579 ms 65536 KB Output is correct
37 Correct 625 ms 65536 KB Output is correct
38 Correct 588 ms 65536 KB Output is correct
39 Correct 584 ms 65536 KB Output is correct
40 Correct 606 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 7416 KB Output is correct
2 Correct 18 ms 7532 KB Output is correct
3 Correct 18 ms 7744 KB Output is correct
4 Correct 17 ms 7744 KB Output is correct
5 Correct 18 ms 7756 KB Output is correct
6 Correct 18 ms 7756 KB Output is correct
7 Correct 22 ms 7756 KB Output is correct
8 Correct 21 ms 7756 KB Output is correct
9 Correct 21 ms 7824 KB Output is correct
10 Correct 18 ms 7824 KB Output is correct
11 Correct 280 ms 22628 KB Output is correct
12 Correct 242 ms 32880 KB Output is correct
13 Correct 261 ms 42984 KB Output is correct
14 Correct 263 ms 53840 KB Output is correct
15 Correct 254 ms 65536 KB Output is correct
16 Correct 285 ms 65536 KB Output is correct
17 Correct 289 ms 65536 KB Output is correct
18 Correct 231 ms 65536 KB Output is correct
19 Correct 274 ms 65536 KB Output is correct
20 Correct 286 ms 65536 KB Output is correct
21 Correct 397 ms 65536 KB Output is correct
22 Correct 305 ms 65536 KB Output is correct
23 Correct 388 ms 65536 KB Output is correct
24 Correct 397 ms 65536 KB Output is correct
25 Correct 347 ms 65536 KB Output is correct
26 Correct 443 ms 65536 KB Output is correct
27 Correct 467 ms 65536 KB Output is correct
28 Correct 297 ms 65536 KB Output is correct
29 Correct 354 ms 65536 KB Output is correct
30 Correct 388 ms 65536 KB Output is correct
31 Correct 408 ms 65536 KB Output is correct
32 Correct 426 ms 65536 KB Output is correct
33 Correct 421 ms 65536 KB Output is correct
34 Correct 406 ms 65536 KB Output is correct
35 Correct 449 ms 65536 KB Output is correct
36 Correct 276 ms 65536 KB Output is correct
37 Correct 303 ms 65536 KB Output is correct
38 Correct 388 ms 65536 KB Output is correct
39 Correct 434 ms 65536 KB Output is correct
40 Correct 400 ms 65536 KB Output is correct
41 Correct 584 ms 65536 KB Output is correct
42 Correct 588 ms 65536 KB Output is correct
43 Correct 547 ms 65536 KB Output is correct
44 Correct 578 ms 65536 KB Output is correct
45 Correct 599 ms 65536 KB Output is correct
46 Correct 568 ms 65536 KB Output is correct
47 Correct 565 ms 65536 KB Output is correct
48 Correct 580 ms 65536 KB Output is correct
49 Correct 554 ms 65536 KB Output is correct
50 Correct 581 ms 65536 KB Output is correct
51 Correct 589 ms 65536 KB Output is correct
52 Correct 586 ms 65536 KB Output is correct
53 Correct 583 ms 65536 KB Output is correct
54 Correct 573 ms 65536 KB Output is correct
55 Correct 579 ms 65536 KB Output is correct
56 Correct 567 ms 65536 KB Output is correct
57 Correct 606 ms 65536 KB Output is correct
58 Correct 567 ms 65536 KB Output is correct
59 Correct 625 ms 65536 KB Output is correct
60 Correct 626 ms 65536 KB Output is correct
61 Correct 598 ms 65536 KB Output is correct
62 Correct 602 ms 65536 KB Output is correct
63 Correct 590 ms 65536 KB Output is correct
64 Correct 524 ms 65536 KB Output is correct
65 Correct 577 ms 65536 KB Output is correct
66 Correct 579 ms 65536 KB Output is correct
67 Correct 625 ms 65536 KB Output is correct
68 Correct 588 ms 65536 KB Output is correct
69 Correct 584 ms 65536 KB Output is correct
70 Correct 606 ms 65536 KB Output is correct
71 Correct 1869 ms 65536 KB Output is correct
72 Correct 1661 ms 65536 KB Output is correct
73 Correct 1679 ms 65536 KB Output is correct
74 Correct 1887 ms 65536 KB Output is correct
75 Execution timed out 2033 ms 65536 KB Time limit exceeded
76 Halted 0 ms 0 KB -