Submission #96088

# Submission time Handle Problem Language Result Execution time Memory
96088 2019-02-05T17:38:56 Z MvC Snake Escaping (JOI18_snake_escaping) C++11
100 / 100
878 ms 43152 KB
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
int D0[1 << 20];
int D1[1 << 20];
char str[1 << 20];
int coef[1 << 20];
char s[64];
int main(void)
{
    int n,q;
    scanf("%d%d%s",&n,&q,str);
    const int N = 1 << n;
    for (int i = 0;i < N;++i)
    	str[i] -= '0';
    for (int i = 0;i < N;++i)
    	coef[i] = __builtin_popcount(i) & 1 ? -1 : 1;
    for (int i = 0;i < N;++i)
    	D0[i] = D1[i] = str[i];
    for (int i = 0;i < n;++i)
    	for (int mask = 0;mask < N;++mask)
    		if ((mask >> i) & 1)
    			D0[mask ^ (1 << i)] += D0[mask];
    		else
    			D1[mask ^ (1 << i)] += D1[mask];
   	while (q --) {
    	scanf("%s",s);
    	reverse(s,s + n);
    	int a = 0,b = 0,c = 0;
    	int la = 0,lb = 0,lc = 0;
    	for (int i = 0;i < n;++i) {
    		if (s[i] == '0')
    			a |= 1 << i,++la;
    		else
    		if (s[i] == '1')
    			b |= 1 << i,++lb;
    		else
    			c |= 1 << i,++lc;
    	}
    	int ans = 0;
    	if (la <= lb && la <= lc) {
    		for (int mask = a;mask >= 0;mask = !mask ? -1 : ((mask - 1) & a))
    			ans += coef[mask] * D0[(N - 1) ^ mask ^ c];
    	} else
    	if (lb <= la && lb <= lc) {
    		for (int mask = b;mask >= 0;mask = !mask ? -1 : ((mask - 1) & b))
    			ans += coef[mask] * D1[mask | c];
    	} else {
    		for (int mask = c;mask >= 0;mask = !mask ? -1 : ((mask - 1) & c))
    			ans += str[mask | b];
    	}
    	ans = abs(ans);
    	printf("%d\n",ans);
    }
    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:32:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
     for (int i = 0;i < n;++i)
     ^~~
snake_escaping.cpp:38:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
     while (q --) {
     ^~~~~
snake_escaping.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%s",&n,&q,str);
     ~~~~~^~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:39:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
      scanf("%s",s);
      ~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 408 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 408 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 225 ms 15140 KB Output is correct
12 Correct 234 ms 14844 KB Output is correct
13 Correct 259 ms 13936 KB Output is correct
14 Correct 255 ms 14072 KB Output is correct
15 Correct 242 ms 15088 KB Output is correct
16 Correct 266 ms 14208 KB Output is correct
17 Correct 269 ms 14424 KB Output is correct
18 Correct 213 ms 16092 KB Output is correct
19 Correct 220 ms 13144 KB Output is correct
20 Correct 241 ms 14756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 408 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 225 ms 15140 KB Output is correct
12 Correct 234 ms 14844 KB Output is correct
13 Correct 259 ms 13936 KB Output is correct
14 Correct 255 ms 14072 KB Output is correct
15 Correct 242 ms 15088 KB Output is correct
16 Correct 266 ms 14208 KB Output is correct
17 Correct 269 ms 14424 KB Output is correct
18 Correct 213 ms 16092 KB Output is correct
19 Correct 220 ms 13144 KB Output is correct
20 Correct 241 ms 14756 KB Output is correct
21 Correct 288 ms 18228 KB Output is correct
22 Correct 285 ms 18424 KB Output is correct
23 Correct 310 ms 17416 KB Output is correct
24 Correct 324 ms 17312 KB Output is correct
25 Correct 294 ms 19192 KB Output is correct
26 Correct 338 ms 17604 KB Output is correct
27 Correct 324 ms 17624 KB Output is correct
28 Correct 213 ms 20088 KB Output is correct
29 Correct 276 ms 16120 KB Output is correct
30 Correct 287 ms 18168 KB Output is correct
31 Correct 308 ms 18168 KB Output is correct
32 Correct 329 ms 18196 KB Output is correct
33 Correct 289 ms 17144 KB Output is correct
34 Correct 334 ms 17112 KB Output is correct
35 Correct 337 ms 17632 KB Output is correct
36 Correct 209 ms 16248 KB Output is correct
37 Correct 275 ms 18144 KB Output is correct
38 Correct 281 ms 16248 KB Output is correct
39 Correct 308 ms 17396 KB Output is correct
40 Correct 331 ms 17120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 408 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 67 ms 15992 KB Output is correct
12 Correct 70 ms 16080 KB Output is correct
13 Correct 72 ms 15864 KB Output is correct
14 Correct 77 ms 15904 KB Output is correct
15 Correct 64 ms 15992 KB Output is correct
16 Correct 81 ms 15992 KB Output is correct
17 Correct 83 ms 15864 KB Output is correct
18 Correct 62 ms 16128 KB Output is correct
19 Correct 61 ms 15864 KB Output is correct
20 Correct 62 ms 15992 KB Output is correct
21 Correct 72 ms 15924 KB Output is correct
22 Correct 72 ms 15900 KB Output is correct
23 Correct 64 ms 15992 KB Output is correct
24 Correct 77 ms 16120 KB Output is correct
25 Correct 74 ms 15992 KB Output is correct
26 Correct 59 ms 15996 KB Output is correct
27 Correct 67 ms 15992 KB Output is correct
28 Correct 61 ms 15864 KB Output is correct
29 Correct 111 ms 15992 KB Output is correct
30 Correct 69 ms 15924 KB Output is correct
31 Correct 63 ms 15904 KB Output is correct
32 Correct 83 ms 15992 KB Output is correct
33 Correct 79 ms 15992 KB Output is correct
34 Correct 56 ms 15840 KB Output is correct
35 Correct 70 ms 15864 KB Output is correct
36 Correct 72 ms 15944 KB Output is correct
37 Correct 70 ms 16092 KB Output is correct
38 Correct 79 ms 15864 KB Output is correct
39 Correct 79 ms 16068 KB Output is correct
40 Correct 77 ms 15864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 408 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 225 ms 15140 KB Output is correct
12 Correct 234 ms 14844 KB Output is correct
13 Correct 259 ms 13936 KB Output is correct
14 Correct 255 ms 14072 KB Output is correct
15 Correct 242 ms 15088 KB Output is correct
16 Correct 266 ms 14208 KB Output is correct
17 Correct 269 ms 14424 KB Output is correct
18 Correct 213 ms 16092 KB Output is correct
19 Correct 220 ms 13144 KB Output is correct
20 Correct 241 ms 14756 KB Output is correct
21 Correct 288 ms 18228 KB Output is correct
22 Correct 285 ms 18424 KB Output is correct
23 Correct 310 ms 17416 KB Output is correct
24 Correct 324 ms 17312 KB Output is correct
25 Correct 294 ms 19192 KB Output is correct
26 Correct 338 ms 17604 KB Output is correct
27 Correct 324 ms 17624 KB Output is correct
28 Correct 213 ms 20088 KB Output is correct
29 Correct 276 ms 16120 KB Output is correct
30 Correct 287 ms 18168 KB Output is correct
31 Correct 308 ms 18168 KB Output is correct
32 Correct 329 ms 18196 KB Output is correct
33 Correct 289 ms 17144 KB Output is correct
34 Correct 334 ms 17112 KB Output is correct
35 Correct 337 ms 17632 KB Output is correct
36 Correct 209 ms 16248 KB Output is correct
37 Correct 275 ms 18144 KB Output is correct
38 Correct 281 ms 16248 KB Output is correct
39 Correct 308 ms 17396 KB Output is correct
40 Correct 331 ms 17120 KB Output is correct
41 Correct 67 ms 15992 KB Output is correct
42 Correct 70 ms 16080 KB Output is correct
43 Correct 72 ms 15864 KB Output is correct
44 Correct 77 ms 15904 KB Output is correct
45 Correct 64 ms 15992 KB Output is correct
46 Correct 81 ms 15992 KB Output is correct
47 Correct 83 ms 15864 KB Output is correct
48 Correct 62 ms 16128 KB Output is correct
49 Correct 61 ms 15864 KB Output is correct
50 Correct 62 ms 15992 KB Output is correct
51 Correct 72 ms 15924 KB Output is correct
52 Correct 72 ms 15900 KB Output is correct
53 Correct 64 ms 15992 KB Output is correct
54 Correct 77 ms 16120 KB Output is correct
55 Correct 74 ms 15992 KB Output is correct
56 Correct 59 ms 15996 KB Output is correct
57 Correct 67 ms 15992 KB Output is correct
58 Correct 61 ms 15864 KB Output is correct
59 Correct 111 ms 15992 KB Output is correct
60 Correct 69 ms 15924 KB Output is correct
61 Correct 63 ms 15904 KB Output is correct
62 Correct 83 ms 15992 KB Output is correct
63 Correct 79 ms 15992 KB Output is correct
64 Correct 56 ms 15840 KB Output is correct
65 Correct 70 ms 15864 KB Output is correct
66 Correct 72 ms 15944 KB Output is correct
67 Correct 70 ms 16092 KB Output is correct
68 Correct 79 ms 15864 KB Output is correct
69 Correct 79 ms 16068 KB Output is correct
70 Correct 77 ms 15864 KB Output is correct
71 Correct 494 ms 40104 KB Output is correct
72 Correct 503 ms 40312 KB Output is correct
73 Correct 568 ms 38864 KB Output is correct
74 Correct 651 ms 39300 KB Output is correct
75 Correct 430 ms 41088 KB Output is correct
76 Correct 796 ms 39596 KB Output is correct
77 Correct 732 ms 39436 KB Output is correct
78 Correct 311 ms 43152 KB Output is correct
79 Correct 434 ms 37224 KB Output is correct
80 Correct 522 ms 40396 KB Output is correct
81 Correct 586 ms 40336 KB Output is correct
82 Correct 621 ms 39160 KB Output is correct
83 Correct 434 ms 38388 KB Output is correct
84 Correct 688 ms 39252 KB Output is correct
85 Correct 705 ms 39512 KB Output is correct
86 Correct 289 ms 37360 KB Output is correct
87 Correct 441 ms 40184 KB Output is correct
88 Correct 474 ms 37180 KB Output is correct
89 Correct 547 ms 38908 KB Output is correct
90 Correct 531 ms 39292 KB Output is correct
91 Correct 436 ms 38468 KB Output is correct
92 Correct 878 ms 39568 KB Output is correct
93 Correct 742 ms 39388 KB Output is correct
94 Correct 292 ms 37276 KB Output is correct
95 Correct 704 ms 39360 KB Output is correct
96 Correct 707 ms 39336 KB Output is correct
97 Correct 706 ms 39280 KB Output is correct
98 Correct 701 ms 39712 KB Output is correct
99 Correct 693 ms 39332 KB Output is correct
100 Correct 690 ms 39284 KB Output is correct