답안 #766707

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
766707 2023-06-26T04:58:21 Z nishkarsh Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
583 ms 47408 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pcc pair<char,char>
#define vi vector <int>
#define vl vector <ll>
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define pd(x) printf("%d",x)
#define plld(x) printf("%lld",x)
#define pds(x) printf("%d ",x)
#define pllds(x) printf("%lld ",x)
#define pdn(x) printf("%d\n",x)
#define plldn(x) printf("%lld\n",x)
using namespace std;
ll powmod(ll base,ll exponent,ll mod){
	ll ans=1;
	if(base<0) base+=mod;
	while(exponent){
		if(exponent&1)ans=(ans*base)%mod;
		base=(base*base)%mod;
		exponent/=2;
	}
	return ans;
}
ll gcd(ll a, ll b){
	if(b==0) return a;
	else return gcd(b,a%b);
}
const int INF = 2e9;
const ll INFLL = 4e18;
const int upperlimit = 5e3+1;
const int mod = 998244353;
const int N = 20;
int poison[1<<N];
int zer_sum[1<<N];
int one_sum[1<<N];
int pop_cnt[1<<N];
void subset_sum(int *a, int n){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < (1<<n); j += (1<<(i+1))){
			for(int k = j; k < j + (1<<i); k++){
				a[k+(1<<i)] += a[k];
			}
		}
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int l,q;
	string pos;
	cin >> l >> q >> pos;
	for(int i = 0; i < (1<<l); i++){
		poison[i] = pos[i]-'0';
		one_sum[i] = poison[i];
		zer_sum[(1<<l)-1-i] = poison[i];
		pop_cnt[i] = pop_cnt[i>>1] + (i&1);
	}
	subset_sum(one_sum,l);
	subset_sum(zer_sum,l);
	int zer_mask,one_mask,q_mask,zer,one,ques,ans;
	while(q--){
		zer = one = ques = zer_mask = one_mask = q_mask = ans = 0;
		cin >> pos;
		for(int i = 0; i < l; i++){
			if(pos[i] == '?'){
				ques++;
				q_mask |= (1<<(l-i-1));
			}
			else if(pos[i] == '0'){
				zer++;
				zer_mask |= (1<<(l-i-1));
			}
			else{
				one++;
				one_mask |= (1<<(l-i-1));
			}
		}
		if((ques <= zer) && (ques <= one)){
			for(int mask = q_mask; mask > 0; mask = (mask-1)&q_mask){
				ans += poison[mask^one_mask];
			}
			ans += poison[one_mask];
		}
		else if (zer <= one){
			for(int mask = zer_mask; mask > 0; mask = (mask-1)&zer_mask){
				if(pop_cnt[zer_mask^mask]&1) ans -= zer_sum[q_mask^mask];
				else ans += zer_sum[q_mask^mask];
			}
			if(pop_cnt[zer_mask]&1) ans -= zer_sum[q_mask];
			else ans += zer_sum[q_mask];
		}
		else{
			for(int mask = one_mask; mask > 0; mask = (mask-1)&one_mask){
				if(pop_cnt[one_mask^mask]&1) ans -= one_sum[q_mask^mask];
				else ans += one_sum[q_mask^mask];
			}
			if(pop_cnt[one_mask]&1) ans -= one_sum[q_mask];
			else ans += one_sum[q_mask];
		}
		cout << ans << "\n";
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 166 ms 15120 KB Output is correct
12 Correct 171 ms 14804 KB Output is correct
13 Correct 194 ms 14028 KB Output is correct
14 Correct 191 ms 14064 KB Output is correct
15 Correct 185 ms 15084 KB Output is correct
16 Correct 202 ms 14192 KB Output is correct
17 Correct 208 ms 14232 KB Output is correct
18 Correct 134 ms 16080 KB Output is correct
19 Correct 162 ms 13124 KB Output is correct
20 Correct 185 ms 14720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 166 ms 15120 KB Output is correct
12 Correct 171 ms 14804 KB Output is correct
13 Correct 194 ms 14028 KB Output is correct
14 Correct 191 ms 14064 KB Output is correct
15 Correct 185 ms 15084 KB Output is correct
16 Correct 202 ms 14192 KB Output is correct
17 Correct 208 ms 14232 KB Output is correct
18 Correct 134 ms 16080 KB Output is correct
19 Correct 162 ms 13124 KB Output is correct
20 Correct 185 ms 14720 KB Output is correct
21 Correct 181 ms 18204 KB Output is correct
22 Correct 194 ms 18280 KB Output is correct
23 Correct 218 ms 17344 KB Output is correct
24 Correct 221 ms 17164 KB Output is correct
25 Correct 215 ms 19148 KB Output is correct
26 Correct 241 ms 17648 KB Output is correct
27 Correct 233 ms 17628 KB Output is correct
28 Correct 146 ms 20120 KB Output is correct
29 Correct 178 ms 16176 KB Output is correct
30 Correct 199 ms 18288 KB Output is correct
31 Correct 238 ms 18100 KB Output is correct
32 Correct 226 ms 18192 KB Output is correct
33 Correct 198 ms 17136 KB Output is correct
34 Correct 222 ms 17188 KB Output is correct
35 Correct 243 ms 17584 KB Output is correct
36 Correct 159 ms 16204 KB Output is correct
37 Correct 186 ms 18264 KB Output is correct
38 Correct 205 ms 16152 KB Output is correct
39 Correct 228 ms 17364 KB Output is correct
40 Correct 220 ms 17220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 45 ms 20176 KB Output is correct
12 Correct 41 ms 20196 KB Output is correct
13 Correct 36 ms 20084 KB Output is correct
14 Correct 43 ms 20088 KB Output is correct
15 Correct 35 ms 20140 KB Output is correct
16 Correct 44 ms 20096 KB Output is correct
17 Correct 50 ms 20088 KB Output is correct
18 Correct 31 ms 20308 KB Output is correct
19 Correct 34 ms 19936 KB Output is correct
20 Correct 41 ms 20176 KB Output is correct
21 Correct 37 ms 20184 KB Output is correct
22 Correct 42 ms 20076 KB Output is correct
23 Correct 34 ms 20076 KB Output is correct
24 Correct 44 ms 20040 KB Output is correct
25 Correct 47 ms 20132 KB Output is correct
26 Correct 35 ms 20072 KB Output is correct
27 Correct 43 ms 20168 KB Output is correct
28 Correct 34 ms 20012 KB Output is correct
29 Correct 37 ms 20056 KB Output is correct
30 Correct 41 ms 20132 KB Output is correct
31 Correct 33 ms 20080 KB Output is correct
32 Correct 50 ms 20056 KB Output is correct
33 Correct 49 ms 20120 KB Output is correct
34 Correct 28 ms 20008 KB Output is correct
35 Correct 40 ms 20116 KB Output is correct
36 Correct 56 ms 20124 KB Output is correct
37 Correct 40 ms 20136 KB Output is correct
38 Correct 39 ms 20056 KB Output is correct
39 Correct 40 ms 20056 KB Output is correct
40 Correct 42 ms 20060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 166 ms 15120 KB Output is correct
12 Correct 171 ms 14804 KB Output is correct
13 Correct 194 ms 14028 KB Output is correct
14 Correct 191 ms 14064 KB Output is correct
15 Correct 185 ms 15084 KB Output is correct
16 Correct 202 ms 14192 KB Output is correct
17 Correct 208 ms 14232 KB Output is correct
18 Correct 134 ms 16080 KB Output is correct
19 Correct 162 ms 13124 KB Output is correct
20 Correct 185 ms 14720 KB Output is correct
21 Correct 181 ms 18204 KB Output is correct
22 Correct 194 ms 18280 KB Output is correct
23 Correct 218 ms 17344 KB Output is correct
24 Correct 221 ms 17164 KB Output is correct
25 Correct 215 ms 19148 KB Output is correct
26 Correct 241 ms 17648 KB Output is correct
27 Correct 233 ms 17628 KB Output is correct
28 Correct 146 ms 20120 KB Output is correct
29 Correct 178 ms 16176 KB Output is correct
30 Correct 199 ms 18288 KB Output is correct
31 Correct 238 ms 18100 KB Output is correct
32 Correct 226 ms 18192 KB Output is correct
33 Correct 198 ms 17136 KB Output is correct
34 Correct 222 ms 17188 KB Output is correct
35 Correct 243 ms 17584 KB Output is correct
36 Correct 159 ms 16204 KB Output is correct
37 Correct 186 ms 18264 KB Output is correct
38 Correct 205 ms 16152 KB Output is correct
39 Correct 228 ms 17364 KB Output is correct
40 Correct 220 ms 17220 KB Output is correct
41 Correct 45 ms 20176 KB Output is correct
42 Correct 41 ms 20196 KB Output is correct
43 Correct 36 ms 20084 KB Output is correct
44 Correct 43 ms 20088 KB Output is correct
45 Correct 35 ms 20140 KB Output is correct
46 Correct 44 ms 20096 KB Output is correct
47 Correct 50 ms 20088 KB Output is correct
48 Correct 31 ms 20308 KB Output is correct
49 Correct 34 ms 19936 KB Output is correct
50 Correct 41 ms 20176 KB Output is correct
51 Correct 37 ms 20184 KB Output is correct
52 Correct 42 ms 20076 KB Output is correct
53 Correct 34 ms 20076 KB Output is correct
54 Correct 44 ms 20040 KB Output is correct
55 Correct 47 ms 20132 KB Output is correct
56 Correct 35 ms 20072 KB Output is correct
57 Correct 43 ms 20168 KB Output is correct
58 Correct 34 ms 20012 KB Output is correct
59 Correct 37 ms 20056 KB Output is correct
60 Correct 41 ms 20132 KB Output is correct
61 Correct 33 ms 20080 KB Output is correct
62 Correct 50 ms 20056 KB Output is correct
63 Correct 49 ms 20120 KB Output is correct
64 Correct 28 ms 20008 KB Output is correct
65 Correct 40 ms 20116 KB Output is correct
66 Correct 56 ms 20124 KB Output is correct
67 Correct 40 ms 20136 KB Output is correct
68 Correct 39 ms 20056 KB Output is correct
69 Correct 40 ms 20056 KB Output is correct
70 Correct 42 ms 20060 KB Output is correct
71 Correct 261 ms 44492 KB Output is correct
72 Correct 288 ms 44536 KB Output is correct
73 Correct 342 ms 43108 KB Output is correct
74 Correct 459 ms 43380 KB Output is correct
75 Correct 291 ms 45428 KB Output is correct
76 Correct 504 ms 43676 KB Output is correct
77 Correct 487 ms 43564 KB Output is correct
78 Correct 192 ms 47408 KB Output is correct
79 Correct 261 ms 41464 KB Output is correct
80 Correct 268 ms 44600 KB Output is correct
81 Correct 359 ms 44436 KB Output is correct
82 Correct 457 ms 43408 KB Output is correct
83 Correct 281 ms 42608 KB Output is correct
84 Correct 463 ms 43408 KB Output is correct
85 Correct 560 ms 43764 KB Output is correct
86 Correct 177 ms 41420 KB Output is correct
87 Correct 282 ms 44512 KB Output is correct
88 Correct 260 ms 41372 KB Output is correct
89 Correct 368 ms 43068 KB Output is correct
90 Correct 370 ms 43472 KB Output is correct
91 Correct 270 ms 42560 KB Output is correct
92 Correct 583 ms 43872 KB Output is correct
93 Correct 489 ms 43616 KB Output is correct
94 Correct 186 ms 41308 KB Output is correct
95 Correct 440 ms 43532 KB Output is correct
96 Correct 511 ms 43548 KB Output is correct
97 Correct 411 ms 43508 KB Output is correct
98 Correct 441 ms 43460 KB Output is correct
99 Correct 438 ms 43536 KB Output is correct
100 Correct 466 ms 43516 KB Output is correct