Submission #45520

# Submission time Handle Problem Language Result Execution time Memory
45520 2018-04-14T23:01:53 Z reality Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
991 ms 65536 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 380 KB Output is correct
2 Correct 2 ms 436 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 2 ms 536 KB Output is correct
8 Correct 2 ms 536 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 436 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 2 ms 536 KB Output is correct
8 Correct 2 ms 536 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 290 ms 4660 KB Output is correct
12 Correct 301 ms 15060 KB Output is correct
13 Correct 322 ms 25108 KB Output is correct
14 Correct 344 ms 36124 KB Output is correct
15 Correct 306 ms 47752 KB Output is correct
16 Correct 379 ms 57780 KB Output is correct
17 Correct 335 ms 65536 KB Output is correct
18 Correct 246 ms 65536 KB Output is correct
19 Correct 292 ms 65536 KB Output is correct
20 Correct 387 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 436 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 2 ms 536 KB Output is correct
8 Correct 2 ms 536 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 290 ms 4660 KB Output is correct
12 Correct 301 ms 15060 KB Output is correct
13 Correct 322 ms 25108 KB Output is correct
14 Correct 344 ms 36124 KB Output is correct
15 Correct 306 ms 47752 KB Output is correct
16 Correct 379 ms 57780 KB Output is correct
17 Correct 335 ms 65536 KB Output is correct
18 Correct 246 ms 65536 KB Output is correct
19 Correct 292 ms 65536 KB Output is correct
20 Correct 387 ms 65536 KB Output is correct
21 Correct 404 ms 65536 KB Output is correct
22 Correct 588 ms 65536 KB Output is correct
23 Correct 443 ms 65536 KB Output is correct
24 Correct 466 ms 65536 KB Output is correct
25 Correct 389 ms 65536 KB Output is correct
26 Correct 435 ms 65536 KB Output is correct
27 Correct 418 ms 65536 KB Output is correct
28 Correct 275 ms 65536 KB Output is correct
29 Correct 340 ms 65536 KB Output is correct
30 Correct 373 ms 65536 KB Output is correct
31 Correct 402 ms 65536 KB Output is correct
32 Correct 423 ms 65536 KB Output is correct
33 Correct 367 ms 65536 KB Output is correct
34 Correct 408 ms 65536 KB Output is correct
35 Correct 410 ms 65536 KB Output is correct
36 Correct 272 ms 65536 KB Output is correct
37 Correct 339 ms 65536 KB Output is correct
38 Correct 363 ms 65536 KB Output is correct
39 Correct 389 ms 65536 KB Output is correct
40 Correct 414 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 436 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 2 ms 536 KB Output is correct
8 Correct 2 ms 536 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 79 ms 65536 KB Output is correct
12 Correct 76 ms 65536 KB Output is correct
13 Correct 85 ms 65536 KB Output is correct
14 Correct 97 ms 65536 KB Output is correct
15 Correct 79 ms 65536 KB Output is correct
16 Correct 98 ms 65536 KB Output is correct
17 Correct 92 ms 65536 KB Output is correct
18 Correct 67 ms 65536 KB Output is correct
19 Correct 73 ms 65536 KB Output is correct
20 Correct 80 ms 65536 KB Output is correct
21 Correct 81 ms 65536 KB Output is correct
22 Correct 93 ms 65536 KB Output is correct
23 Correct 77 ms 65536 KB Output is correct
24 Correct 117 ms 65536 KB Output is correct
25 Correct 88 ms 65536 KB Output is correct
26 Correct 68 ms 65536 KB Output is correct
27 Correct 72 ms 65536 KB Output is correct
28 Correct 73 ms 65536 KB Output is correct
29 Correct 79 ms 65536 KB Output is correct
30 Correct 80 ms 65536 KB Output is correct
31 Correct 74 ms 65536 KB Output is correct
32 Correct 97 ms 65536 KB Output is correct
33 Correct 88 ms 65536 KB Output is correct
34 Correct 64 ms 65536 KB Output is correct
35 Correct 89 ms 65536 KB Output is correct
36 Correct 88 ms 65536 KB Output is correct
37 Correct 87 ms 65536 KB Output is correct
38 Correct 96 ms 65536 KB Output is correct
39 Correct 85 ms 65536 KB Output is correct
40 Correct 106 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 436 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 2 ms 536 KB Output is correct
8 Correct 2 ms 536 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 290 ms 4660 KB Output is correct
12 Correct 301 ms 15060 KB Output is correct
13 Correct 322 ms 25108 KB Output is correct
14 Correct 344 ms 36124 KB Output is correct
15 Correct 306 ms 47752 KB Output is correct
16 Correct 379 ms 57780 KB Output is correct
17 Correct 335 ms 65536 KB Output is correct
18 Correct 246 ms 65536 KB Output is correct
19 Correct 292 ms 65536 KB Output is correct
20 Correct 387 ms 65536 KB Output is correct
21 Correct 404 ms 65536 KB Output is correct
22 Correct 588 ms 65536 KB Output is correct
23 Correct 443 ms 65536 KB Output is correct
24 Correct 466 ms 65536 KB Output is correct
25 Correct 389 ms 65536 KB Output is correct
26 Correct 435 ms 65536 KB Output is correct
27 Correct 418 ms 65536 KB Output is correct
28 Correct 275 ms 65536 KB Output is correct
29 Correct 340 ms 65536 KB Output is correct
30 Correct 373 ms 65536 KB Output is correct
31 Correct 402 ms 65536 KB Output is correct
32 Correct 423 ms 65536 KB Output is correct
33 Correct 367 ms 65536 KB Output is correct
34 Correct 408 ms 65536 KB Output is correct
35 Correct 410 ms 65536 KB Output is correct
36 Correct 272 ms 65536 KB Output is correct
37 Correct 339 ms 65536 KB Output is correct
38 Correct 363 ms 65536 KB Output is correct
39 Correct 389 ms 65536 KB Output is correct
40 Correct 414 ms 65536 KB Output is correct
41 Correct 79 ms 65536 KB Output is correct
42 Correct 76 ms 65536 KB Output is correct
43 Correct 85 ms 65536 KB Output is correct
44 Correct 97 ms 65536 KB Output is correct
45 Correct 79 ms 65536 KB Output is correct
46 Correct 98 ms 65536 KB Output is correct
47 Correct 92 ms 65536 KB Output is correct
48 Correct 67 ms 65536 KB Output is correct
49 Correct 73 ms 65536 KB Output is correct
50 Correct 80 ms 65536 KB Output is correct
51 Correct 81 ms 65536 KB Output is correct
52 Correct 93 ms 65536 KB Output is correct
53 Correct 77 ms 65536 KB Output is correct
54 Correct 117 ms 65536 KB Output is correct
55 Correct 88 ms 65536 KB Output is correct
56 Correct 68 ms 65536 KB Output is correct
57 Correct 72 ms 65536 KB Output is correct
58 Correct 73 ms 65536 KB Output is correct
59 Correct 79 ms 65536 KB Output is correct
60 Correct 80 ms 65536 KB Output is correct
61 Correct 74 ms 65536 KB Output is correct
62 Correct 97 ms 65536 KB Output is correct
63 Correct 88 ms 65536 KB Output is correct
64 Correct 64 ms 65536 KB Output is correct
65 Correct 89 ms 65536 KB Output is correct
66 Correct 88 ms 65536 KB Output is correct
67 Correct 87 ms 65536 KB Output is correct
68 Correct 96 ms 65536 KB Output is correct
69 Correct 85 ms 65536 KB Output is correct
70 Correct 106 ms 65536 KB Output is correct
71 Correct 671 ms 65536 KB Output is correct
72 Correct 617 ms 65536 KB Output is correct
73 Correct 695 ms 65536 KB Output is correct
74 Correct 914 ms 65536 KB Output is correct
75 Correct 554 ms 65536 KB Output is correct
76 Correct 954 ms 65536 KB Output is correct
77 Correct 889 ms 65536 KB Output is correct
78 Correct 431 ms 65536 KB Output is correct
79 Correct 513 ms 65536 KB Output is correct
80 Correct 600 ms 65536 KB Output is correct
81 Correct 702 ms 65536 KB Output is correct
82 Correct 850 ms 65536 KB Output is correct
83 Correct 589 ms 65536 KB Output is correct
84 Correct 868 ms 65536 KB Output is correct
85 Correct 873 ms 65536 KB Output is correct
86 Correct 366 ms 65536 KB Output is correct
87 Correct 560 ms 65536 KB Output is correct
88 Correct 538 ms 65536 KB Output is correct
89 Correct 656 ms 65536 KB Output is correct
90 Correct 663 ms 65536 KB Output is correct
91 Correct 581 ms 65536 KB Output is correct
92 Correct 991 ms 65536 KB Output is correct
93 Correct 866 ms 65536 KB Output is correct
94 Correct 361 ms 65536 KB Output is correct
95 Correct 760 ms 65536 KB Output is correct
96 Correct 762 ms 65536 KB Output is correct
97 Correct 861 ms 65536 KB Output is correct
98 Correct 813 ms 65536 KB Output is correct
99 Correct 759 ms 65536 KB Output is correct
100 Correct 739 ms 65536 KB Output is correct