Submission #465429

# Submission time Handle Problem Language Result Execution time Memory
465429 2021-08-16T03:21:09 Z czhang2718 Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1051 ms 43360 KB
#include<bits/stdc++.h>
using namespace std;

#define nl '\n'

int l, q;
string a;
int dp1[1<<20];
int dp0[1<<20];
int cnt[2];
int bits[1<<20];

string bin(int i){
  string s;
  for(int j=0; j<l; j++){
      if(i&(1<<j)) s+='1';
      else s+='0';
    }
    return s;
}

int main(){
  cin.tie(0)->sync_with_stdio(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> l >> q;
  cin >> a;

  for(int i=0; i<(1<<l); i++){
    dp1[i]=a[i]-'0';
    dp0[i]=a[((1<<l)-1)^i]-'0';
  }

  for(int i=0; i<l; i++){
    for(int j=0; j<(1<<l); j++){
      if(!(j&(1<<i))) dp1[j]+=dp1[j^(1<<i)];
      if(!(j&(1<<i))) dp0[j]+=dp0[j^(1<<i)];
    }
  }
  for(int i=0; i<(1<<l); i++){
    for(int j=0; j<l; j++){
      if(i&(1<<j)) bits[i]++;
    }
  }

  string s;
  while(q--){
    cin >> s;
    reverse(s.begin(), s.end());
    cnt[0]=cnt[1]=cnt[2]=0;
    int m0=0;
    int m1=0;
    int m2=0;
    for(int i=0; i<l; i++){
      if(s[i]=='?') cnt[2]++, m2^=(1<<i);
      else if(s[i]=='1') cnt[1]++, m1^=(1<<i);
      else cnt[0]++, m0^=(1<<i);
    }
    int ans;
    if(cnt[0]<cnt[1] && cnt[0]<cnt[2]){
      // fix 1s, iterate over submasks of 0s
      ans=dp1[m1];
      for(int s=m0; s; s=(s-1)&m0){
        ans+=(bits[s]&1?-1:1)*dp1[m1|s];
      }
    }
    else if(cnt[1]<cnt[2]){
      ans=dp0[m0];
      for(int s=m1; s; s=(s-1)&m1){
        ans+=(bits[s]&1?-1:1)*dp0[m0|s];
      }
    }
    else{
      ans=a[m1]-'0';
      for(int s=m2; s; s=(s-1)&m2){
        ans+=a[m1|s]-'0';
      }
      if(!cnt[2]) ans=a[m1]-'0';
    }

    cout << ans << nl;
  }
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:51:24: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   51 |     cnt[0]=cnt[1]=cnt[2]=0;
      |                   ~~~~~^
snake_escaping.cpp:10:5: note: while referencing 'cnt'
   10 | int cnt[2];
      |     ^~~
snake_escaping.cpp:56:26: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   56 |       if(s[i]=='?') cnt[2]++, m2^=(1<<i);
      |                     ~~~~~^
snake_escaping.cpp:10:5: note: while referencing 'cnt'
   10 | int cnt[2];
      |     ^~~
snake_escaping.cpp:56:26: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   56 |       if(s[i]=='?') cnt[2]++, m2^=(1<<i);
      |                     ~~~~~^
snake_escaping.cpp:10:5: note: while referencing 'cnt'
   10 | int cnt[2];
      |     ^~~
snake_escaping.cpp:61:37: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   61 |     if(cnt[0]<cnt[1] && cnt[0]<cnt[2]){
      |                                ~~~~~^
snake_escaping.cpp:10:5: note: while referencing 'cnt'
   10 | int cnt[2];
      |     ^~~
snake_escaping.cpp:68:25: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   68 |     else if(cnt[1]<cnt[2]){
      |                    ~~~~~^
snake_escaping.cpp:10:5: note: while referencing 'cnt'
   10 | int cnt[2];
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 262 ms 15088 KB Output is correct
12 Correct 254 ms 14776 KB Output is correct
13 Correct 269 ms 14020 KB Output is correct
14 Correct 262 ms 14108 KB Output is correct
15 Correct 272 ms 15072 KB Output is correct
16 Correct 274 ms 14276 KB Output is correct
17 Correct 285 ms 14148 KB Output is correct
18 Correct 197 ms 16024 KB Output is correct
19 Correct 239 ms 13020 KB Output is correct
20 Correct 257 ms 14812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 262 ms 15088 KB Output is correct
12 Correct 254 ms 14776 KB Output is correct
13 Correct 269 ms 14020 KB Output is correct
14 Correct 262 ms 14108 KB Output is correct
15 Correct 272 ms 15072 KB Output is correct
16 Correct 274 ms 14276 KB Output is correct
17 Correct 285 ms 14148 KB Output is correct
18 Correct 197 ms 16024 KB Output is correct
19 Correct 239 ms 13020 KB Output is correct
20 Correct 257 ms 14812 KB Output is correct
21 Correct 269 ms 18072 KB Output is correct
22 Correct 280 ms 18316 KB Output is correct
23 Correct 336 ms 17228 KB Output is correct
24 Correct 303 ms 17092 KB Output is correct
25 Correct 298 ms 19052 KB Output is correct
26 Correct 371 ms 17592 KB Output is correct
27 Correct 344 ms 17584 KB Output is correct
28 Correct 206 ms 20164 KB Output is correct
29 Correct 264 ms 16072 KB Output is correct
30 Correct 287 ms 18244 KB Output is correct
31 Correct 334 ms 18120 KB Output is correct
32 Correct 329 ms 18072 KB Output is correct
33 Correct 274 ms 17104 KB Output is correct
34 Correct 338 ms 17176 KB Output is correct
35 Correct 326 ms 17592 KB Output is correct
36 Correct 223 ms 16052 KB Output is correct
37 Correct 274 ms 18076 KB Output is correct
38 Correct 277 ms 16140 KB Output is correct
39 Correct 302 ms 17244 KB Output is correct
40 Correct 313 ms 17044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 126 ms 16068 KB Output is correct
12 Correct 129 ms 16028 KB Output is correct
13 Correct 127 ms 15940 KB Output is correct
14 Correct 148 ms 15896 KB Output is correct
15 Correct 119 ms 16068 KB Output is correct
16 Correct 143 ms 15944 KB Output is correct
17 Correct 134 ms 15980 KB Output is correct
18 Correct 113 ms 16140 KB Output is correct
19 Correct 117 ms 15804 KB Output is correct
20 Correct 123 ms 15996 KB Output is correct
21 Correct 129 ms 15972 KB Output is correct
22 Correct 147 ms 15984 KB Output is correct
23 Correct 120 ms 15952 KB Output is correct
24 Correct 133 ms 16048 KB Output is correct
25 Correct 132 ms 15972 KB Output is correct
26 Correct 116 ms 16072 KB Output is correct
27 Correct 119 ms 15972 KB Output is correct
28 Correct 126 ms 15816 KB Output is correct
29 Correct 123 ms 15932 KB Output is correct
30 Correct 130 ms 15976 KB Output is correct
31 Correct 119 ms 15932 KB Output is correct
32 Correct 157 ms 16056 KB Output is correct
33 Correct 136 ms 15948 KB Output is correct
34 Correct 114 ms 15928 KB Output is correct
35 Correct 134 ms 15972 KB Output is correct
36 Correct 132 ms 16076 KB Output is correct
37 Correct 132 ms 15932 KB Output is correct
38 Correct 133 ms 15968 KB Output is correct
39 Correct 133 ms 15964 KB Output is correct
40 Correct 132 ms 16100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 262 ms 15088 KB Output is correct
12 Correct 254 ms 14776 KB Output is correct
13 Correct 269 ms 14020 KB Output is correct
14 Correct 262 ms 14108 KB Output is correct
15 Correct 272 ms 15072 KB Output is correct
16 Correct 274 ms 14276 KB Output is correct
17 Correct 285 ms 14148 KB Output is correct
18 Correct 197 ms 16024 KB Output is correct
19 Correct 239 ms 13020 KB Output is correct
20 Correct 257 ms 14812 KB Output is correct
21 Correct 269 ms 18072 KB Output is correct
22 Correct 280 ms 18316 KB Output is correct
23 Correct 336 ms 17228 KB Output is correct
24 Correct 303 ms 17092 KB Output is correct
25 Correct 298 ms 19052 KB Output is correct
26 Correct 371 ms 17592 KB Output is correct
27 Correct 344 ms 17584 KB Output is correct
28 Correct 206 ms 20164 KB Output is correct
29 Correct 264 ms 16072 KB Output is correct
30 Correct 287 ms 18244 KB Output is correct
31 Correct 334 ms 18120 KB Output is correct
32 Correct 329 ms 18072 KB Output is correct
33 Correct 274 ms 17104 KB Output is correct
34 Correct 338 ms 17176 KB Output is correct
35 Correct 326 ms 17592 KB Output is correct
36 Correct 223 ms 16052 KB Output is correct
37 Correct 274 ms 18076 KB Output is correct
38 Correct 277 ms 16140 KB Output is correct
39 Correct 302 ms 17244 KB Output is correct
40 Correct 313 ms 17044 KB Output is correct
41 Correct 126 ms 16068 KB Output is correct
42 Correct 129 ms 16028 KB Output is correct
43 Correct 127 ms 15940 KB Output is correct
44 Correct 148 ms 15896 KB Output is correct
45 Correct 119 ms 16068 KB Output is correct
46 Correct 143 ms 15944 KB Output is correct
47 Correct 134 ms 15980 KB Output is correct
48 Correct 113 ms 16140 KB Output is correct
49 Correct 117 ms 15804 KB Output is correct
50 Correct 123 ms 15996 KB Output is correct
51 Correct 129 ms 15972 KB Output is correct
52 Correct 147 ms 15984 KB Output is correct
53 Correct 120 ms 15952 KB Output is correct
54 Correct 133 ms 16048 KB Output is correct
55 Correct 132 ms 15972 KB Output is correct
56 Correct 116 ms 16072 KB Output is correct
57 Correct 119 ms 15972 KB Output is correct
58 Correct 126 ms 15816 KB Output is correct
59 Correct 123 ms 15932 KB Output is correct
60 Correct 130 ms 15976 KB Output is correct
61 Correct 119 ms 15932 KB Output is correct
62 Correct 157 ms 16056 KB Output is correct
63 Correct 136 ms 15948 KB Output is correct
64 Correct 114 ms 15928 KB Output is correct
65 Correct 134 ms 15972 KB Output is correct
66 Correct 132 ms 16076 KB Output is correct
67 Correct 132 ms 15932 KB Output is correct
68 Correct 133 ms 15968 KB Output is correct
69 Correct 133 ms 15964 KB Output is correct
70 Correct 132 ms 16100 KB Output is correct
71 Correct 469 ms 40324 KB Output is correct
72 Correct 507 ms 40420 KB Output is correct
73 Correct 539 ms 38936 KB Output is correct
74 Correct 1051 ms 39444 KB Output is correct
75 Correct 519 ms 41372 KB Output is correct
76 Correct 971 ms 39556 KB Output is correct
77 Correct 802 ms 39492 KB Output is correct
78 Correct 336 ms 43360 KB Output is correct
79 Correct 471 ms 37276 KB Output is correct
80 Correct 500 ms 40456 KB Output is correct
81 Correct 700 ms 40500 KB Output is correct
82 Correct 799 ms 39284 KB Output is correct
83 Correct 460 ms 38516 KB Output is correct
84 Correct 745 ms 39500 KB Output is correct
85 Correct 770 ms 39656 KB Output is correct
86 Correct 342 ms 37352 KB Output is correct
87 Correct 504 ms 40320 KB Output is correct
88 Correct 440 ms 37524 KB Output is correct
89 Correct 542 ms 39068 KB Output is correct
90 Correct 603 ms 39352 KB Output is correct
91 Correct 452 ms 38536 KB Output is correct
92 Correct 900 ms 39600 KB Output is correct
93 Correct 799 ms 39692 KB Output is correct
94 Correct 332 ms 37316 KB Output is correct
95 Correct 689 ms 39404 KB Output is correct
96 Correct 712 ms 39524 KB Output is correct
97 Correct 686 ms 39528 KB Output is correct
98 Correct 700 ms 39440 KB Output is correct
99 Correct 705 ms 39376 KB Output is correct
100 Correct 686 ms 39404 KB Output is correct