Submission #1113368

# Submission time Handle Problem Language Result Execution time Memory
1113368 2024-11-16T13:03:46 Z emad234 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
926 ms 48360 KB
#include "bits/stdc++.h"
#define F first
#define S second
#define ll long long
#define pii pair<ll,int>
const int mxN = (1 << 20) + 5;
const int mod = 1e9 + 7;
using namespace std;
int sos[mxN][2][2];
signed main(){
 ios_base::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);
  int n,q;
  cin >>n>>q;
  string s;
  cin>>s;
  for(int i = 0;i < n;i++){
    for(int mask = 0;mask < (1 << n);mask++){
      if(!i){
        sos[mask][0][0] = s[mask] - '0';
        if((mask &1)) sos[mask][0][0] += s[mask - 1] - '0';
        sos[mask][0][1] = s[mask] - '0';
        if((mask &1) == 0) sos[mask][0][1] += s[mask + 1] - '0';
        continue;
      }
      sos[mask][i &1][0] = sos[mask][(i - 1)&1][0];
      sos[mask][i &1][1] = sos[mask][(i - 1)&1][1];
      if(mask >> i & 1) sos[mask][(i)&1][0] += sos[mask - (1 << i)][(i - 1)&1][0];
      else sos[mask][i &1][1] += sos[mask + (1 << i)][(i - 1)&1][1];
    }
  }
  while(q--){
    string a;
    cin >>a;
    reverse(a.begin(),a.end());
    int Qcnt = 0, Ocnt = 0, Icnt = 0;
    for(auto x : a){
      if(x == '?') Qcnt++;
      else if(x == '1') Icnt++;
      else Ocnt++;
    }
    int ans = 0;
    if(min({Qcnt,Ocnt,Icnt}) == Qcnt){
      // printf("? small\n");
      int m1 = 0,m2 = 0,m3 = 0;
      int i = 0;
      for(auto x : a){
        if(x == '1') m1 += (1 << i);
        if(x == '?') m2 += (1 << i);
        i++;
      }
      m3 = m2;
      while(m3 > 0){
        ans += s[m3 + m1] - '0';
        m3 = (m3 - 1) & m2;
      }
      ans += s[m1] - '0';
    }else if(min({Qcnt,Ocnt,Icnt}) == Icnt){
      // printf("1 small\n");
      int m1 = 0,m2 = 0,m3 = 0;
      int i = 0;
      for(auto x : a){
        if(x == '?') m1 += (1 << i);
        if(x == '1') m2 += (1 << i);
        i++;
      }
      m3 = m2;
      while(m3 > 0){
        if(((Icnt + Qcnt) &1) == (__builtin_popcount(m3 + m1) & 1)) ans += sos[m3 + m1][(n - 1) & 1][0];
        else ans -= sos[m3 + m1][(n - 1) & 1][0];
        m3 = (m3 - 1) & m2;
      }
      if(((Icnt + Qcnt) & 1) == (__builtin_popcount(m1) & 1)) ans += sos[m1][(n - 1) & 1][0];
      else ans -= sos[m1][(n - 1) & 1][0];
    }else{
      // printf("0 small\n");
      int m1 = 0,m2 = 0,m3 = 0;
      int i = 0;
      for(auto x : a){
        if(x == '1') m1 += (1 << i);
        if(x == '0') m2 += (1 << i);
        i++;
      }
      m3 = m2;
      while(m3 > 0){
        if((Icnt & 1) == (__builtin_popcount(m3 + m1) &1)) ans += sos[m3 + m1][(n - 1) & 1][1];
        else ans -= sos[m3 + m1][(n - 1) & 1][1];
        m3 = (m3 - 1) & m2;
      }
      if((Icnt & 1) == (__builtin_popcount(m1) &1)) ans += sos[m1][(n - 1) & 1][1];
      else ans -= sos[m1][(n - 1) & 1][1];
    }
    cout<<ans<<'\n';
  }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 516 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 516 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 171 ms 4384 KB Output is correct
12 Correct 189 ms 3948 KB Output is correct
13 Correct 212 ms 3400 KB Output is correct
14 Correct 195 ms 3384 KB Output is correct
15 Correct 214 ms 4424 KB Output is correct
16 Correct 238 ms 5196 KB Output is correct
17 Correct 232 ms 3404 KB Output is correct
18 Correct 104 ms 5292 KB Output is correct
19 Correct 146 ms 2376 KB Output is correct
20 Correct 190 ms 3904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 516 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 171 ms 4384 KB Output is correct
12 Correct 189 ms 3948 KB Output is correct
13 Correct 212 ms 3400 KB Output is correct
14 Correct 195 ms 3384 KB Output is correct
15 Correct 214 ms 4424 KB Output is correct
16 Correct 238 ms 5196 KB Output is correct
17 Correct 232 ms 3404 KB Output is correct
18 Correct 104 ms 5292 KB Output is correct
19 Correct 146 ms 2376 KB Output is correct
20 Correct 190 ms 3904 KB Output is correct
21 Correct 184 ms 4424 KB Output is correct
22 Correct 227 ms 4588 KB Output is correct
23 Correct 257 ms 3664 KB Output is correct
24 Correct 247 ms 3476 KB Output is correct
25 Correct 250 ms 6788 KB Output is correct
26 Correct 311 ms 5448 KB Output is correct
27 Correct 283 ms 3908 KB Output is correct
28 Correct 123 ms 6472 KB Output is correct
29 Correct 221 ms 2376 KB Output is correct
30 Correct 254 ms 4708 KB Output is correct
31 Correct 278 ms 4424 KB Output is correct
32 Correct 324 ms 4424 KB Output is correct
33 Correct 232 ms 3400 KB Output is correct
34 Correct 269 ms 4176 KB Output is correct
35 Correct 304 ms 4380 KB Output is correct
36 Correct 152 ms 6288 KB Output is correct
37 Correct 255 ms 7168 KB Output is correct
38 Correct 262 ms 2376 KB Output is correct
39 Correct 271 ms 3656 KB Output is correct
40 Correct 258 ms 3400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 516 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 56 ms 19040 KB Output is correct
12 Correct 57 ms 19180 KB Output is correct
13 Correct 65 ms 19176 KB Output is correct
14 Correct 104 ms 19172 KB Output is correct
15 Correct 84 ms 19176 KB Output is correct
16 Correct 77 ms 19172 KB Output is correct
17 Correct 76 ms 19176 KB Output is correct
18 Correct 51 ms 19292 KB Output is correct
19 Correct 59 ms 20436 KB Output is correct
20 Correct 55 ms 20592 KB Output is correct
21 Correct 68 ms 19176 KB Output is correct
22 Correct 79 ms 20692 KB Output is correct
23 Correct 53 ms 19048 KB Output is correct
24 Correct 83 ms 21068 KB Output is correct
25 Correct 72 ms 20456 KB Output is correct
26 Correct 53 ms 20316 KB Output is correct
27 Correct 59 ms 21272 KB Output is correct
28 Correct 63 ms 20968 KB Output is correct
29 Correct 64 ms 20592 KB Output is correct
30 Correct 58 ms 21224 KB Output is correct
31 Correct 52 ms 21032 KB Output is correct
32 Correct 79 ms 20712 KB Output is correct
33 Correct 63 ms 20672 KB Output is correct
34 Correct 50 ms 20196 KB Output is correct
35 Correct 72 ms 19176 KB Output is correct
36 Correct 56 ms 19040 KB Output is correct
37 Correct 63 ms 21068 KB Output is correct
38 Correct 83 ms 19112 KB Output is correct
39 Correct 62 ms 19040 KB Output is correct
40 Correct 66 ms 20448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 516 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 171 ms 4384 KB Output is correct
12 Correct 189 ms 3948 KB Output is correct
13 Correct 212 ms 3400 KB Output is correct
14 Correct 195 ms 3384 KB Output is correct
15 Correct 214 ms 4424 KB Output is correct
16 Correct 238 ms 5196 KB Output is correct
17 Correct 232 ms 3404 KB Output is correct
18 Correct 104 ms 5292 KB Output is correct
19 Correct 146 ms 2376 KB Output is correct
20 Correct 190 ms 3904 KB Output is correct
21 Correct 184 ms 4424 KB Output is correct
22 Correct 227 ms 4588 KB Output is correct
23 Correct 257 ms 3664 KB Output is correct
24 Correct 247 ms 3476 KB Output is correct
25 Correct 250 ms 6788 KB Output is correct
26 Correct 311 ms 5448 KB Output is correct
27 Correct 283 ms 3908 KB Output is correct
28 Correct 123 ms 6472 KB Output is correct
29 Correct 221 ms 2376 KB Output is correct
30 Correct 254 ms 4708 KB Output is correct
31 Correct 278 ms 4424 KB Output is correct
32 Correct 324 ms 4424 KB Output is correct
33 Correct 232 ms 3400 KB Output is correct
34 Correct 269 ms 4176 KB Output is correct
35 Correct 304 ms 4380 KB Output is correct
36 Correct 152 ms 6288 KB Output is correct
37 Correct 255 ms 7168 KB Output is correct
38 Correct 262 ms 2376 KB Output is correct
39 Correct 271 ms 3656 KB Output is correct
40 Correct 258 ms 3400 KB Output is correct
41 Correct 56 ms 19040 KB Output is correct
42 Correct 57 ms 19180 KB Output is correct
43 Correct 65 ms 19176 KB Output is correct
44 Correct 104 ms 19172 KB Output is correct
45 Correct 84 ms 19176 KB Output is correct
46 Correct 77 ms 19172 KB Output is correct
47 Correct 76 ms 19176 KB Output is correct
48 Correct 51 ms 19292 KB Output is correct
49 Correct 59 ms 20436 KB Output is correct
50 Correct 55 ms 20592 KB Output is correct
51 Correct 68 ms 19176 KB Output is correct
52 Correct 79 ms 20692 KB Output is correct
53 Correct 53 ms 19048 KB Output is correct
54 Correct 83 ms 21068 KB Output is correct
55 Correct 72 ms 20456 KB Output is correct
56 Correct 53 ms 20316 KB Output is correct
57 Correct 59 ms 21272 KB Output is correct
58 Correct 63 ms 20968 KB Output is correct
59 Correct 64 ms 20592 KB Output is correct
60 Correct 58 ms 21224 KB Output is correct
61 Correct 52 ms 21032 KB Output is correct
62 Correct 79 ms 20712 KB Output is correct
63 Correct 63 ms 20672 KB Output is correct
64 Correct 50 ms 20196 KB Output is correct
65 Correct 72 ms 19176 KB Output is correct
66 Correct 56 ms 19040 KB Output is correct
67 Correct 63 ms 21068 KB Output is correct
68 Correct 83 ms 19112 KB Output is correct
69 Correct 62 ms 19040 KB Output is correct
70 Correct 66 ms 20448 KB Output is correct
71 Correct 466 ms 24040 KB Output is correct
72 Correct 520 ms 35016 KB Output is correct
73 Correct 509 ms 34536 KB Output is correct
74 Correct 825 ms 34280 KB Output is correct
75 Correct 434 ms 46364 KB Output is correct
76 Correct 741 ms 44776 KB Output is correct
77 Correct 717 ms 44592 KB Output is correct
78 Correct 240 ms 48360 KB Output is correct
79 Correct 417 ms 42468 KB Output is correct
80 Correct 499 ms 45544 KB Output is correct
81 Correct 633 ms 45536 KB Output is correct
82 Correct 863 ms 44500 KB Output is correct
83 Correct 438 ms 43600 KB Output is correct
84 Correct 692 ms 44384 KB Output is correct
85 Correct 748 ms 44320 KB Output is correct
86 Correct 243 ms 42524 KB Output is correct
87 Correct 478 ms 44624 KB Output is correct
88 Correct 431 ms 41448 KB Output is correct
89 Correct 606 ms 43100 KB Output is correct
90 Correct 546 ms 44520 KB Output is correct
91 Correct 414 ms 43664 KB Output is correct
92 Correct 926 ms 44764 KB Output is correct
93 Correct 869 ms 43832 KB Output is correct
94 Correct 240 ms 42472 KB Output is correct
95 Correct 653 ms 44484 KB Output is correct
96 Correct 666 ms 44624 KB Output is correct
97 Correct 540 ms 44628 KB Output is correct
98 Correct 549 ms 44548 KB Output is correct
99 Correct 596 ms 43564 KB Output is correct
100 Correct 678 ms 44520 KB Output is correct