답안 #954392

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
954392 2024-03-27T18:36:52 Z KLPP Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1703 ms 60148 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
lld val[2000000];
lld super[2000000];
lld sub[2000000];
string s;
vector<int> cnt0;
vector<int> cnt1;
vector<int> cnt2;
void solve() {
	int n,q;
	cin>>n>>q;
	cin>>s;
	rep(i,0,(1<<n)){
		val[i]=s[i]-'0';
		super[i]=s[i]-'0';
		sub[i]=s[i]-'0';
	}
	rep(i,0,n){
		rep(msk,0,(1<<n)){
			if((msk&(1<<i))){
				sub[msk]+=sub[msk-(1<<i)];
			}else{
				super[msk]+=super[msk+(1<<i)];
			}
		}
	}
	//rep(msk,0,(1<<n))cout<<sub[msk]<<endl;
	while(q--){
		cnt0.clear();
		cnt1.clear();
		cnt2.clear();
		cin>>s;
		int curmsk=0;
		rep(i,0,n){
			if(s[n-1-i]=='0')cnt0.push_back(i);
			if(s[n-1-i]=='1')cnt1.push_back(i),curmsk+=(1<<i);
			if(s[n-1-i]=='?')cnt2.push_back(i);
		}
		if((int)cnt2.size()<=7){
			lld ans=0;
			rep(msk,0,(1<<cnt2.size())){
				int genms=curmsk;
				rep(j,0,(int)cnt2.size()){
					if((msk&(1<<j))){
						genms+=(1<<cnt2[j]);
					}
				}
				ans+=val[genms];
				//cout<<"G"<<genms<<endl;
			}
			cout<<ans<<"\n";
		}else{
			if((int)cnt1.size()<=6){
				//few 1's
				trav(a,cnt2)curmsk+=(1<<a);
				lld ans=0;
				rep(msk,0,(1<<cnt1.size())){
					int genms=curmsk;
					int mlt=1;
					rep(j,0,(int)cnt1.size()){
						if((msk&(1<<j))){
							mlt*=-1;
							genms-=(1<<cnt1[j]);
						}
					}
					ans+=mlt*sub[genms];
					//cout<<"G "<<genms<<" "<<sub[genms]<<endl;
				}
				cout<<ans<<"\n";
			}else{
				lld ans=0;
				rep(msk,0,(1<<cnt0.size())){
					int genms=curmsk;
					int mlt=1;
					rep(j,0,(int)cnt0.size()){
						if((msk&(1<<j))){
							mlt*=-1;
							genms+=(1<<cnt0[j]);
						}
					}
					ans+=mlt*super[genms];
					//cout<<"G "<<genms<<" "<<super[genms]<<endl;
				}
				cout<<ans<<"\n";
			}
		}
	}
}


int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt=1;
	//cin>>tt;
	while(tt--){
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4568 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4580 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4568 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4580 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 406 ms 19248 KB Output is correct
12 Correct 526 ms 18940 KB Output is correct
13 Correct 263 ms 18372 KB Output is correct
14 Correct 240 ms 18088 KB Output is correct
15 Correct 702 ms 19100 KB Output is correct
16 Correct 344 ms 18256 KB Output is correct
17 Correct 348 ms 18344 KB Output is correct
18 Correct 166 ms 20304 KB Output is correct
19 Correct 183 ms 17220 KB Output is correct
20 Correct 534 ms 18772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4568 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4580 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 406 ms 19248 KB Output is correct
12 Correct 526 ms 18940 KB Output is correct
13 Correct 263 ms 18372 KB Output is correct
14 Correct 240 ms 18088 KB Output is correct
15 Correct 702 ms 19100 KB Output is correct
16 Correct 344 ms 18256 KB Output is correct
17 Correct 348 ms 18344 KB Output is correct
18 Correct 166 ms 20304 KB Output is correct
19 Correct 183 ms 17220 KB Output is correct
20 Correct 534 ms 18772 KB Output is correct
21 Correct 1377 ms 22380 KB Output is correct
22 Correct 693 ms 22276 KB Output is correct
23 Correct 396 ms 21296 KB Output is correct
24 Correct 330 ms 21112 KB Output is correct
25 Correct 267 ms 23272 KB Output is correct
26 Correct 542 ms 21700 KB Output is correct
27 Correct 514 ms 22168 KB Output is correct
28 Correct 170 ms 24344 KB Output is correct
29 Correct 234 ms 20100 KB Output is correct
30 Correct 702 ms 22364 KB Output is correct
31 Correct 755 ms 22288 KB Output is correct
32 Correct 449 ms 22100 KB Output is correct
33 Correct 287 ms 21172 KB Output is correct
34 Correct 336 ms 21332 KB Output is correct
35 Correct 506 ms 21584 KB Output is correct
36 Correct 233 ms 20208 KB Output is correct
37 Correct 723 ms 22356 KB Output is correct
38 Correct 275 ms 20312 KB Output is correct
39 Correct 377 ms 21304 KB Output is correct
40 Correct 338 ms 21244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4568 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4580 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 51 ms 32648 KB Output is correct
12 Correct 52 ms 32752 KB Output is correct
13 Correct 81 ms 32536 KB Output is correct
14 Correct 122 ms 32756 KB Output is correct
15 Correct 50 ms 32752 KB Output is correct
16 Correct 87 ms 32748 KB Output is correct
17 Correct 99 ms 32752 KB Output is correct
18 Correct 48 ms 32792 KB Output is correct
19 Correct 48 ms 32504 KB Output is correct
20 Correct 71 ms 32796 KB Output is correct
21 Correct 65 ms 32752 KB Output is correct
22 Correct 124 ms 32500 KB Output is correct
23 Correct 64 ms 32500 KB Output is correct
24 Correct 81 ms 32552 KB Output is correct
25 Correct 84 ms 32760 KB Output is correct
26 Correct 41 ms 32500 KB Output is correct
27 Correct 47 ms 32796 KB Output is correct
28 Correct 47 ms 32496 KB Output is correct
29 Correct 81 ms 32536 KB Output is correct
30 Correct 86 ms 33008 KB Output is correct
31 Correct 59 ms 32496 KB Output is correct
32 Correct 91 ms 32752 KB Output is correct
33 Correct 82 ms 32752 KB Output is correct
34 Correct 40 ms 32756 KB Output is correct
35 Correct 77 ms 32752 KB Output is correct
36 Correct 70 ms 32756 KB Output is correct
37 Correct 79 ms 32536 KB Output is correct
38 Correct 72 ms 32604 KB Output is correct
39 Correct 78 ms 32756 KB Output is correct
40 Correct 72 ms 32552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4568 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4580 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 406 ms 19248 KB Output is correct
12 Correct 526 ms 18940 KB Output is correct
13 Correct 263 ms 18372 KB Output is correct
14 Correct 240 ms 18088 KB Output is correct
15 Correct 702 ms 19100 KB Output is correct
16 Correct 344 ms 18256 KB Output is correct
17 Correct 348 ms 18344 KB Output is correct
18 Correct 166 ms 20304 KB Output is correct
19 Correct 183 ms 17220 KB Output is correct
20 Correct 534 ms 18772 KB Output is correct
21 Correct 1377 ms 22380 KB Output is correct
22 Correct 693 ms 22276 KB Output is correct
23 Correct 396 ms 21296 KB Output is correct
24 Correct 330 ms 21112 KB Output is correct
25 Correct 267 ms 23272 KB Output is correct
26 Correct 542 ms 21700 KB Output is correct
27 Correct 514 ms 22168 KB Output is correct
28 Correct 170 ms 24344 KB Output is correct
29 Correct 234 ms 20100 KB Output is correct
30 Correct 702 ms 22364 KB Output is correct
31 Correct 755 ms 22288 KB Output is correct
32 Correct 449 ms 22100 KB Output is correct
33 Correct 287 ms 21172 KB Output is correct
34 Correct 336 ms 21332 KB Output is correct
35 Correct 506 ms 21584 KB Output is correct
36 Correct 233 ms 20208 KB Output is correct
37 Correct 723 ms 22356 KB Output is correct
38 Correct 275 ms 20312 KB Output is correct
39 Correct 377 ms 21304 KB Output is correct
40 Correct 338 ms 21244 KB Output is correct
41 Correct 51 ms 32648 KB Output is correct
42 Correct 52 ms 32752 KB Output is correct
43 Correct 81 ms 32536 KB Output is correct
44 Correct 122 ms 32756 KB Output is correct
45 Correct 50 ms 32752 KB Output is correct
46 Correct 87 ms 32748 KB Output is correct
47 Correct 99 ms 32752 KB Output is correct
48 Correct 48 ms 32792 KB Output is correct
49 Correct 48 ms 32504 KB Output is correct
50 Correct 71 ms 32796 KB Output is correct
51 Correct 65 ms 32752 KB Output is correct
52 Correct 124 ms 32500 KB Output is correct
53 Correct 64 ms 32500 KB Output is correct
54 Correct 81 ms 32552 KB Output is correct
55 Correct 84 ms 32760 KB Output is correct
56 Correct 41 ms 32500 KB Output is correct
57 Correct 47 ms 32796 KB Output is correct
58 Correct 47 ms 32496 KB Output is correct
59 Correct 81 ms 32536 KB Output is correct
60 Correct 86 ms 33008 KB Output is correct
61 Correct 59 ms 32496 KB Output is correct
62 Correct 91 ms 32752 KB Output is correct
63 Correct 82 ms 32752 KB Output is correct
64 Correct 40 ms 32756 KB Output is correct
65 Correct 77 ms 32752 KB Output is correct
66 Correct 70 ms 32756 KB Output is correct
67 Correct 79 ms 32536 KB Output is correct
68 Correct 72 ms 32604 KB Output is correct
69 Correct 78 ms 32756 KB Output is correct
70 Correct 72 ms 32552 KB Output is correct
71 Correct 352 ms 56872 KB Output is correct
72 Correct 477 ms 57076 KB Output is correct
73 Correct 781 ms 55596 KB Output is correct
74 Correct 1703 ms 55772 KB Output is correct
75 Correct 447 ms 57908 KB Output is correct
76 Correct 1135 ms 56340 KB Output is correct
77 Correct 1155 ms 56208 KB Output is correct
78 Correct 256 ms 60148 KB Output is correct
79 Correct 338 ms 54028 KB Output is correct
80 Correct 549 ms 57072 KB Output is correct
81 Correct 591 ms 57068 KB Output is correct
82 Correct 1699 ms 55876 KB Output is correct
83 Correct 459 ms 55300 KB Output is correct
84 Correct 1137 ms 56024 KB Output is correct
85 Correct 1205 ms 56396 KB Output is correct
86 Correct 222 ms 54000 KB Output is correct
87 Correct 319 ms 57020 KB Output is correct
88 Correct 351 ms 54112 KB Output is correct
89 Correct 734 ms 55516 KB Output is correct
90 Correct 938 ms 56020 KB Output is correct
91 Correct 460 ms 55284 KB Output is correct
92 Correct 1330 ms 56352 KB Output is correct
93 Correct 1073 ms 56088 KB Output is correct
94 Correct 221 ms 52492 KB Output is correct
95 Correct 833 ms 56064 KB Output is correct
96 Correct 917 ms 56104 KB Output is correct
97 Correct 839 ms 56024 KB Output is correct
98 Correct 833 ms 56076 KB Output is correct
99 Correct 947 ms 55984 KB Output is correct
100 Correct 944 ms 56308 KB Output is correct