답안 #331997

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331997 2020-12-01T05:02:14 Z arnold518 Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1507 ms 59884 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXL = 20;
const int MAXN = (1<<MAXL);
const int MAXQ = 1e6;

int L, Q, N;
char SS[MAXN+10], TT[MAXL+10];
int S[MAXN+10], T[MAXL+10];
int dp1[MAXN+10][2], dp2[MAXN+10][2];
int P1[MAXN+10], P2[MAXN+10];

int main()
{
	scanf("%d%d", &L, &Q);
	scanf(" %s", SS);
	N=(1<<L);
	for(int i=0; i<N; i++)
	{
		S[i]=SS[i]-'0';
	}

	for(int i=0; i<(1<<L); i++)
	{
		dp1[i][0]=S[i];
	}

	for(int i=1; i<=L; i++)
	{
		for(int j=0; j<(1<<L); j++)
		{
			dp1[j][i&1]=dp1[j][i-1&1];
			if(j&(1<<(i-1))) dp1[j][i&1]+=dp1[j^(1<<(i-1))][i-1&1];
		}
	}
	for(int i=0; i<(1<<L); i++) P1[i]=dp1[i][L&1];

	for(int i=0; i<(1<<L); i++)
	{
		dp2[i][0]=S[i^((1<<L)-1)];
	}

	for(int i=1; i<=L; i++)
	{
		for(int j=0; j<(1<<L); j++)
		{
			dp2[j][i&1]=dp2[j][i-1&1];
			if(j&(1<<(i-1))) dp2[j][i&1]+=dp2[j^(1<<(i-1))][i-1&1];
		}
	}

	for(int i=0; i<(1<<L); i++) P2[i]=dp2[i^((1<<L)-1)][L&1];

	while(Q--)
	{
		scanf(" %s", TT);
		int cnt0=0, cnt1=0, cnt2=0;
		for(int i=0; i<L; i++)
		{
			if(TT[L-1-i]=='?') T[i]=-1, cnt2++;
			else if(TT[L-1-i]=='0') T[i]=0, cnt0++;
			else if(TT[L-1-i]=='1') T[i]=1, cnt1++;
		}
		int val=min({cnt2, cnt0, cnt1});

		int ans=0;
		if(val==cnt2)
		{
			int m1=0, m2=0;
			for(int i=0; i<L; i++)
			{
				if(T[i]!=-1) m1|=(T[i]<<i);
				else m2|=(1<<i);
			}
			for(int i=m2; ; i=m2&(i-1))
			{
				ans+=S[i|m1];
				if(i==0) break;
			}
		}
		else if(val==cnt1)
		{
			int m0=0, m1=0;
			for(int i=0; i<L; i++)
			{
				if(T[i]==0) m0|=(1<<i);
				if(T[i]==1) m1|=(1<<i);
			}
			for(int i=m1; ; i=m1&(i-1))
			{
				int mask=i|m0;
				mask=mask^((1<<L)-1);
				if(__builtin_popcount(i)%2) ans-=P1[mask];
				else ans+=P1[mask];
				if(i==0) break; 
			}
		}
		else
		{
			int m0=0, m1=0;
			for(int i=0; i<L; i++)
			{
				if(T[i]==0) m0|=(1<<i);
				if(T[i]==1) m1|=(1<<i);
			}
			for(int i=m0; ; i=m0&(i-1))
			{
				int mask=i|m1;
				if(__builtin_popcount(i)%2) ans-=P2[mask];
				else ans+=P2[mask];
				if(i==0) break; 
			}
		}
		printf("%d\n", ans);
	}
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:37:24: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   37 |    dp1[j][i&1]=dp1[j][i-1&1];
      |                       ~^~
snake_escaping.cpp:38:53: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   38 |    if(j&(1<<(i-1))) dp1[j][i&1]+=dp1[j^(1<<(i-1))][i-1&1];
      |                                                    ~^~
snake_escaping.cpp:52:24: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   52 |    dp2[j][i&1]=dp2[j][i-1&1];
      |                       ~^~
snake_escaping.cpp:53:53: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   53 |    if(j&(1<<(i-1))) dp2[j][i&1]+=dp2[j^(1<<(i-1))][i-1&1];
      |                                                    ~^~
snake_escaping.cpp:20:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   20 |  scanf("%d%d", &L, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   21 |  scanf(" %s", SS);
      |  ~~~~~^~~~~~~~~~~
snake_escaping.cpp:61:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   61 |   scanf(" %s", TT);
      |   ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 301 ms 15212 KB Output is correct
12 Correct 340 ms 14860 KB Output is correct
13 Correct 363 ms 14060 KB Output is correct
14 Correct 343 ms 14188 KB Output is correct
15 Correct 356 ms 15212 KB Output is correct
16 Correct 387 ms 14572 KB Output is correct
17 Correct 382 ms 14344 KB Output is correct
18 Correct 273 ms 16108 KB Output is correct
19 Correct 312 ms 13292 KB Output is correct
20 Correct 318 ms 14828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 301 ms 15212 KB Output is correct
12 Correct 340 ms 14860 KB Output is correct
13 Correct 363 ms 14060 KB Output is correct
14 Correct 343 ms 14188 KB Output is correct
15 Correct 356 ms 15212 KB Output is correct
16 Correct 387 ms 14572 KB Output is correct
17 Correct 382 ms 14344 KB Output is correct
18 Correct 273 ms 16108 KB Output is correct
19 Correct 312 ms 13292 KB Output is correct
20 Correct 318 ms 14828 KB Output is correct
21 Correct 332 ms 16348 KB Output is correct
22 Correct 403 ms 16492 KB Output is correct
23 Correct 443 ms 15596 KB Output is correct
24 Correct 431 ms 15596 KB Output is correct
25 Correct 403 ms 17516 KB Output is correct
26 Correct 462 ms 15980 KB Output is correct
27 Correct 453 ms 16236 KB Output is correct
28 Correct 288 ms 18668 KB Output is correct
29 Correct 330 ms 14572 KB Output is correct
30 Correct 360 ms 16876 KB Output is correct
31 Correct 458 ms 16748 KB Output is correct
32 Correct 487 ms 17004 KB Output is correct
33 Correct 368 ms 15996 KB Output is correct
34 Correct 452 ms 15852 KB Output is correct
35 Correct 458 ms 16364 KB Output is correct
36 Correct 275 ms 15084 KB Output is correct
37 Correct 374 ms 17260 KB Output is correct
38 Correct 346 ms 15084 KB Output is correct
39 Correct 426 ms 16364 KB Output is correct
40 Correct 425 ms 16236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 107 ms 32364 KB Output is correct
12 Correct 116 ms 32492 KB Output is correct
13 Correct 123 ms 32364 KB Output is correct
14 Correct 143 ms 32364 KB Output is correct
15 Correct 110 ms 32492 KB Output is correct
16 Correct 154 ms 32364 KB Output is correct
17 Correct 138 ms 32364 KB Output is correct
18 Correct 97 ms 32492 KB Output is correct
19 Correct 106 ms 32256 KB Output is correct
20 Correct 105 ms 32364 KB Output is correct
21 Correct 128 ms 32364 KB Output is correct
22 Correct 137 ms 32364 KB Output is correct
23 Correct 109 ms 32236 KB Output is correct
24 Correct 136 ms 32492 KB Output is correct
25 Correct 137 ms 32364 KB Output is correct
26 Correct 100 ms 32236 KB Output is correct
27 Correct 115 ms 32364 KB Output is correct
28 Correct 104 ms 32236 KB Output is correct
29 Correct 118 ms 32364 KB Output is correct
30 Correct 124 ms 32364 KB Output is correct
31 Correct 114 ms 32260 KB Output is correct
32 Correct 149 ms 32364 KB Output is correct
33 Correct 138 ms 32364 KB Output is correct
34 Correct 99 ms 32236 KB Output is correct
35 Correct 133 ms 32492 KB Output is correct
36 Correct 130 ms 32364 KB Output is correct
37 Correct 129 ms 32364 KB Output is correct
38 Correct 128 ms 32364 KB Output is correct
39 Correct 128 ms 32364 KB Output is correct
40 Correct 130 ms 32364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 301 ms 15212 KB Output is correct
12 Correct 340 ms 14860 KB Output is correct
13 Correct 363 ms 14060 KB Output is correct
14 Correct 343 ms 14188 KB Output is correct
15 Correct 356 ms 15212 KB Output is correct
16 Correct 387 ms 14572 KB Output is correct
17 Correct 382 ms 14344 KB Output is correct
18 Correct 273 ms 16108 KB Output is correct
19 Correct 312 ms 13292 KB Output is correct
20 Correct 318 ms 14828 KB Output is correct
21 Correct 332 ms 16348 KB Output is correct
22 Correct 403 ms 16492 KB Output is correct
23 Correct 443 ms 15596 KB Output is correct
24 Correct 431 ms 15596 KB Output is correct
25 Correct 403 ms 17516 KB Output is correct
26 Correct 462 ms 15980 KB Output is correct
27 Correct 453 ms 16236 KB Output is correct
28 Correct 288 ms 18668 KB Output is correct
29 Correct 330 ms 14572 KB Output is correct
30 Correct 360 ms 16876 KB Output is correct
31 Correct 458 ms 16748 KB Output is correct
32 Correct 487 ms 17004 KB Output is correct
33 Correct 368 ms 15996 KB Output is correct
34 Correct 452 ms 15852 KB Output is correct
35 Correct 458 ms 16364 KB Output is correct
36 Correct 275 ms 15084 KB Output is correct
37 Correct 374 ms 17260 KB Output is correct
38 Correct 346 ms 15084 KB Output is correct
39 Correct 426 ms 16364 KB Output is correct
40 Correct 425 ms 16236 KB Output is correct
41 Correct 107 ms 32364 KB Output is correct
42 Correct 116 ms 32492 KB Output is correct
43 Correct 123 ms 32364 KB Output is correct
44 Correct 143 ms 32364 KB Output is correct
45 Correct 110 ms 32492 KB Output is correct
46 Correct 154 ms 32364 KB Output is correct
47 Correct 138 ms 32364 KB Output is correct
48 Correct 97 ms 32492 KB Output is correct
49 Correct 106 ms 32256 KB Output is correct
50 Correct 105 ms 32364 KB Output is correct
51 Correct 128 ms 32364 KB Output is correct
52 Correct 137 ms 32364 KB Output is correct
53 Correct 109 ms 32236 KB Output is correct
54 Correct 136 ms 32492 KB Output is correct
55 Correct 137 ms 32364 KB Output is correct
56 Correct 100 ms 32236 KB Output is correct
57 Correct 115 ms 32364 KB Output is correct
58 Correct 104 ms 32236 KB Output is correct
59 Correct 118 ms 32364 KB Output is correct
60 Correct 124 ms 32364 KB Output is correct
61 Correct 114 ms 32260 KB Output is correct
62 Correct 149 ms 32364 KB Output is correct
63 Correct 138 ms 32364 KB Output is correct
64 Correct 99 ms 32236 KB Output is correct
65 Correct 133 ms 32492 KB Output is correct
66 Correct 130 ms 32364 KB Output is correct
67 Correct 129 ms 32364 KB Output is correct
68 Correct 128 ms 32364 KB Output is correct
69 Correct 128 ms 32364 KB Output is correct
70 Correct 130 ms 32364 KB Output is correct
71 Correct 574 ms 56684 KB Output is correct
72 Correct 704 ms 56812 KB Output is correct
73 Correct 845 ms 55276 KB Output is correct
74 Correct 1287 ms 55788 KB Output is correct
75 Correct 692 ms 57708 KB Output is correct
76 Correct 1507 ms 56044 KB Output is correct
77 Correct 1242 ms 55916 KB Output is correct
78 Correct 433 ms 59884 KB Output is correct
79 Correct 577 ms 53628 KB Output is correct
80 Correct 601 ms 56812 KB Output is correct
81 Correct 981 ms 56684 KB Output is correct
82 Correct 1255 ms 55660 KB Output is correct
83 Correct 643 ms 54892 KB Output is correct
84 Correct 1218 ms 55788 KB Output is correct
85 Correct 1248 ms 55916 KB Output is correct
86 Correct 412 ms 53868 KB Output is correct
87 Correct 692 ms 56812 KB Output is correct
88 Correct 604 ms 53868 KB Output is correct
89 Correct 819 ms 55404 KB Output is correct
90 Correct 875 ms 55788 KB Output is correct
91 Correct 656 ms 54892 KB Output is correct
92 Correct 1422 ms 56220 KB Output is correct
93 Correct 1236 ms 56044 KB Output is correct
94 Correct 431 ms 53740 KB Output is correct
95 Correct 1047 ms 55840 KB Output is correct
96 Correct 1041 ms 55944 KB Output is correct
97 Correct 1045 ms 55916 KB Output is correct
98 Correct 1056 ms 55916 KB Output is correct
99 Correct 1044 ms 55788 KB Output is correct
100 Correct 1027 ms 55788 KB Output is correct