Submission #939502

# Submission time Handle Problem Language Result Execution time Memory
939502 2024-03-06T12:22:20 Z vjudge1 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1740 ms 41464 KB
#include <bits/stdc++.h>

#pragma optimize("Ofast")
#pragma target("avx2")

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define y1 yy
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b) __gcd(a,b)
#define in insert
// #define int ll

const int MAX=1e5+15;
const int B=2e5;
const int N=104;
const int block=450;
const int maxB=MAX/B+10;
const ll inf=1e18;  
const int mod=1e9+7;
const int mod1=1e9+9;
const ld eps=1e-9;

int dx[8]={1,0,-1,0,1,-1,-1,1};
int dy[8]={0,1,0,-1,1,-1,1,-1};

int binpow(int a,int n){
  if(!n)return 1;
  if(n%2==1)return a*binpow(a,n-1);
  int k=binpow(a,n/2);
  return k*k;
}

int n,q;
string s;
int dp1[(1<<21)];
int dp0[(1<<21)];

int bit(int i,int j){
  return (i>>j)&1;
}

int stupid(string t){
  int ans=0;
  int c=0;
  for(int j=0;j<n;j++)if(t[j]=='?')c++;
  for(int i=0;i<(1<<c);i++){
    int cur=0;
    int f=0;
    for(int j=0;j<n;j++){
      if(t[j]!='?'){
        f=f*2+(t[j]-'0');
      }
      else{
        f=f*2+bit(i,cur++);
      }
    }
    ans+=s[f]-'0';
  }
  return ans;
}

void solve(){
  cin>>n>>q;
  cin>>s;
  for(int mask=0;mask<(1<<n);mask++)dp1[mask]=s[mask]-'0';
  for(int i=0;i<=n;i++){
    for(int mask=(1<<n)-1;mask>=0;mask--){
      if(!((mask>>i)&1)){
        dp1[mask]+=dp1[mask+(1<<i)];
      }
    }
  }
  for(int mask=0;mask<(1<<n);mask++)dp0[mask]=s[mask]-'0';
  for(int i=0;i<n;i++){
    for(int mask=0;mask<(1<<n);mask++){
      if((mask>>i)&1){
        dp0[mask]+=dp0[mask-(1<<i)];
      }
    }
  }
  while(q--){
    string t;
    cin>>t;
    int c0=0,c1=0,c=0;
    int m0=0,m1=0,m=0;
    int msk=0;
    for(int i=0;i<n;i++){
      m0*=2;
      m1*=2;
      m*=2;
      msk*=2;
      if(t[i]=='0')c0++,m0++;
      else if(t[i]=='1')c1++,m1++;
      else c++,m++;
    }
    if(c<=min(c1,c0)){
      cout<<stupid(t)<<"\n";
    }
    else if(c0<=c1){
      int ans=0;
      for(int mask=m0;;mask=((mask-1)&m0)){
        int c=__builtin_popcount(mask);
        if(c%2==1)ans-=dp1[mask|m1];
        else ans+=dp1[mask|m1];
        if(mask==0)break;
      }
      cout<<ans<<"\n";
    }
    else{
      int ans=0;
      for(int mask=m1;;mask=((mask-1)&m1)){
        int c=__builtin_popcount(mask);
        if((c1-c)%2==1)ans-=dp0[mask|m];
        else ans+=dp0[mask|m];
        if(mask==0)break;
      }
      cout<<ans<<"\n";
    }
  }
}

//1010
//0011

signed main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // prec();
  int t=1;
  // cin>>t;
  while(t--)solve();
}

Compilation message

snake_escaping.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
    3 | #pragma optimize("Ofast")
      | 
snake_escaping.cpp:4: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
    4 | #pragma target("avx2")
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2552 KB Output is correct
3 Correct 2 ms 2392 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2552 KB Output is correct
3 Correct 2 ms 2392 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 163 ms 6380 KB Output is correct
12 Correct 168 ms 6168 KB Output is correct
13 Correct 222 ms 5236 KB Output is correct
14 Correct 326 ms 5704 KB Output is correct
15 Correct 175 ms 6484 KB Output is correct
16 Correct 272 ms 5708 KB Output is correct
17 Correct 299 ms 5616 KB Output is correct
18 Correct 143 ms 7452 KB Output is correct
19 Correct 173 ms 4436 KB Output is correct
20 Correct 171 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2552 KB Output is correct
3 Correct 2 ms 2392 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 163 ms 6380 KB Output is correct
12 Correct 168 ms 6168 KB Output is correct
13 Correct 222 ms 5236 KB Output is correct
14 Correct 326 ms 5704 KB Output is correct
15 Correct 175 ms 6484 KB Output is correct
16 Correct 272 ms 5708 KB Output is correct
17 Correct 299 ms 5616 KB Output is correct
18 Correct 143 ms 7452 KB Output is correct
19 Correct 173 ms 4436 KB Output is correct
20 Correct 171 ms 6100 KB Output is correct
21 Correct 190 ms 6488 KB Output is correct
22 Correct 200 ms 6564 KB Output is correct
23 Correct 314 ms 5492 KB Output is correct
24 Correct 577 ms 5364 KB Output is correct
25 Correct 218 ms 7456 KB Output is correct
26 Correct 377 ms 5972 KB Output is correct
27 Correct 388 ms 5904 KB Output is correct
28 Correct 142 ms 8528 KB Output is correct
29 Correct 240 ms 4732 KB Output is correct
30 Correct 201 ms 6476 KB Output is correct
31 Correct 221 ms 6512 KB Output is correct
32 Correct 253 ms 6484 KB Output is correct
33 Correct 236 ms 5456 KB Output is correct
34 Correct 403 ms 5484 KB Output is correct
35 Correct 350 ms 6240 KB Output is correct
36 Correct 159 ms 4440 KB Output is correct
37 Correct 182 ms 6496 KB Output is correct
38 Correct 230 ms 4400 KB Output is correct
39 Correct 339 ms 5492 KB Output is correct
40 Correct 588 ms 5420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2552 KB Output is correct
3 Correct 2 ms 2392 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 54 ms 12036 KB Output is correct
12 Correct 51 ms 12056 KB Output is correct
13 Correct 77 ms 12040 KB Output is correct
14 Correct 70 ms 12032 KB Output is correct
15 Correct 54 ms 12308 KB Output is correct
16 Correct 108 ms 12144 KB Output is correct
17 Correct 94 ms 12228 KB Output is correct
18 Correct 46 ms 12388 KB Output is correct
19 Correct 56 ms 12092 KB Output is correct
20 Correct 51 ms 12296 KB Output is correct
21 Correct 71 ms 12128 KB Output is correct
22 Correct 72 ms 12192 KB Output is correct
23 Correct 56 ms 12064 KB Output is correct
24 Correct 105 ms 12060 KB Output is correct
25 Correct 87 ms 12148 KB Output is correct
26 Correct 48 ms 12040 KB Output is correct
27 Correct 50 ms 12276 KB Output is correct
28 Correct 62 ms 12048 KB Output is correct
29 Correct 71 ms 12132 KB Output is correct
30 Correct 126 ms 12112 KB Output is correct
31 Correct 63 ms 12116 KB Output is correct
32 Correct 90 ms 12260 KB Output is correct
33 Correct 85 ms 12128 KB Output is correct
34 Correct 75 ms 12032 KB Output is correct
35 Correct 78 ms 12036 KB Output is correct
36 Correct 71 ms 12132 KB Output is correct
37 Correct 72 ms 12036 KB Output is correct
38 Correct 72 ms 12204 KB Output is correct
39 Correct 75 ms 12092 KB Output is correct
40 Correct 84 ms 12028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2552 KB Output is correct
3 Correct 2 ms 2392 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 163 ms 6380 KB Output is correct
12 Correct 168 ms 6168 KB Output is correct
13 Correct 222 ms 5236 KB Output is correct
14 Correct 326 ms 5704 KB Output is correct
15 Correct 175 ms 6484 KB Output is correct
16 Correct 272 ms 5708 KB Output is correct
17 Correct 299 ms 5616 KB Output is correct
18 Correct 143 ms 7452 KB Output is correct
19 Correct 173 ms 4436 KB Output is correct
20 Correct 171 ms 6100 KB Output is correct
21 Correct 190 ms 6488 KB Output is correct
22 Correct 200 ms 6564 KB Output is correct
23 Correct 314 ms 5492 KB Output is correct
24 Correct 577 ms 5364 KB Output is correct
25 Correct 218 ms 7456 KB Output is correct
26 Correct 377 ms 5972 KB Output is correct
27 Correct 388 ms 5904 KB Output is correct
28 Correct 142 ms 8528 KB Output is correct
29 Correct 240 ms 4732 KB Output is correct
30 Correct 201 ms 6476 KB Output is correct
31 Correct 221 ms 6512 KB Output is correct
32 Correct 253 ms 6484 KB Output is correct
33 Correct 236 ms 5456 KB Output is correct
34 Correct 403 ms 5484 KB Output is correct
35 Correct 350 ms 6240 KB Output is correct
36 Correct 159 ms 4440 KB Output is correct
37 Correct 182 ms 6496 KB Output is correct
38 Correct 230 ms 4400 KB Output is correct
39 Correct 339 ms 5492 KB Output is correct
40 Correct 588 ms 5420 KB Output is correct
41 Correct 54 ms 12036 KB Output is correct
42 Correct 51 ms 12056 KB Output is correct
43 Correct 77 ms 12040 KB Output is correct
44 Correct 70 ms 12032 KB Output is correct
45 Correct 54 ms 12308 KB Output is correct
46 Correct 108 ms 12144 KB Output is correct
47 Correct 94 ms 12228 KB Output is correct
48 Correct 46 ms 12388 KB Output is correct
49 Correct 56 ms 12092 KB Output is correct
50 Correct 51 ms 12296 KB Output is correct
51 Correct 71 ms 12128 KB Output is correct
52 Correct 72 ms 12192 KB Output is correct
53 Correct 56 ms 12064 KB Output is correct
54 Correct 105 ms 12060 KB Output is correct
55 Correct 87 ms 12148 KB Output is correct
56 Correct 48 ms 12040 KB Output is correct
57 Correct 50 ms 12276 KB Output is correct
58 Correct 62 ms 12048 KB Output is correct
59 Correct 71 ms 12132 KB Output is correct
60 Correct 126 ms 12112 KB Output is correct
61 Correct 63 ms 12116 KB Output is correct
62 Correct 90 ms 12260 KB Output is correct
63 Correct 85 ms 12128 KB Output is correct
64 Correct 75 ms 12032 KB Output is correct
65 Correct 78 ms 12036 KB Output is correct
66 Correct 71 ms 12132 KB Output is correct
67 Correct 72 ms 12036 KB Output is correct
68 Correct 72 ms 12204 KB Output is correct
69 Correct 75 ms 12092 KB Output is correct
70 Correct 84 ms 12028 KB Output is correct
71 Correct 371 ms 16820 KB Output is correct
72 Correct 311 ms 16992 KB Output is correct
73 Correct 811 ms 15588 KB Output is correct
74 Correct 616 ms 28408 KB Output is correct
75 Correct 319 ms 39436 KB Output is correct
76 Correct 904 ms 37908 KB Output is correct
77 Correct 773 ms 37724 KB Output is correct
78 Correct 222 ms 41464 KB Output is correct
79 Correct 338 ms 35500 KB Output is correct
80 Correct 307 ms 38656 KB Output is correct
81 Correct 391 ms 38644 KB Output is correct
82 Correct 563 ms 37432 KB Output is correct
83 Correct 429 ms 36632 KB Output is correct
84 Correct 1206 ms 37612 KB Output is correct
85 Correct 927 ms 37552 KB Output is correct
86 Correct 280 ms 35372 KB Output is correct
87 Correct 291 ms 38388 KB Output is correct
88 Correct 370 ms 35644 KB Output is correct
89 Correct 706 ms 37540 KB Output is correct
90 Correct 1740 ms 37680 KB Output is correct
91 Correct 452 ms 36828 KB Output is correct
92 Correct 983 ms 37828 KB Output is correct
93 Correct 826 ms 37624 KB Output is correct
94 Correct 254 ms 35524 KB Output is correct
95 Correct 598 ms 37620 KB Output is correct
96 Correct 619 ms 37544 KB Output is correct
97 Correct 665 ms 37620 KB Output is correct
98 Correct 673 ms 37620 KB Output is correct
99 Correct 695 ms 37436 KB Output is correct
100 Correct 638 ms 37804 KB Output is correct