제출 #287839

#제출 시각아이디문제언어결과실행 시간메모리
287839MatheusLealVSnake Escaping (JOI18_snake_escaping)C++17
22 / 100
453 ms26360 KiB
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using namespace std;
typedef pair<int, int> pii;
#define N 350010
 
int n, q;
string t;
int ans[1600000], pow3[30], cost[1600000];
int get_mask(int mask, int i){
	return (mask/pow3[i]) % 3;
}

bool vis[N];
void gen(int mask){
	if(vis[mask]) return;
	vis[mask] = 1;
	for(int i = 0; i < n; i++){
		int value = get_mask(mask, i);
		if(value == 2){
			int L = mask, R = mask;
			L += -2*pow3[i] + pow3[i];
			R += -2*pow3[i];
			gen(L);
			gen(R);
			cost[mask] += cost[L] + cost[R];
			break;
		}
	}
}
int32_t main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>n>>q;
	cin>>t;
	pow3[0] = 1;
	for(int i = 1; i < 30; i++) pow3[i] = (3*pow3[i-1]);
	for(int i = 0; i < (1<<n); i++){
		int mask = 0;
		for(int j= 0; j < n; j++){
			if(i & (1<<j)) mask += pow3[j];
		}
		cost[mask] += t[i]-'0';
	}
		for(int i = 1 ; i < pow3[n] ; i ++){
			for(int j = 0 ; j < n ; j ++){
				if(get_mask(i,j) == 2){
					int u = i - pow3[j]*2 , v = u + pow3[j];
					cost[i] += cost[u] + cost[v];
					break ; 
				}
			}
		}
	while(q--){
		string s;
		int mask = 0;
		cin>>s;
		reverse(all(s));
		for(int i = 0; i < n; i++){
			if(s[i] == '1') mask += pow3[i];
			else if(s[i] == '?') mask += 2*pow3[i];
		}
		// gen(mask);
		cout<<cost[mask]<<"\n";
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...