Submission #939495

# Submission time Handle Problem Language Result Execution time Memory
939495 2024-03-06T12:04:45 Z vjudge1 Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 47128 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;
    for(int i=0;i<n;i++){
      if(t[i]=='0')c0++;
      else if(t[i]=='1')c1++;
      else c++;
    }
    if(c<=min(c1,c0)){
      cout<<stupid(t)<<"\n";
    }
    else if(c0<=c1){
      int ans=0;
      for(int mask=0;mask<(1<<c0);mask++){
        int f1=0;
        int cur=0;
        int c=0;
        for(int j=0;j<n;j++){
          if(t[j]=='1'){
            f1=f1*2+1;
          }
          else if(t[j]=='?'){
            f1=f1*2;
          }
          else{
            f1=f1*2;
            if(bit(mask,cur++)){
              f1++;
              c++;
            }
          }
        }
        int res=dp1[f1];
        if(c%2==1)ans-=res;
        else ans+=res;
      }
      cout<<ans<<"\n";
    }
    else{
      int ans=0;
      for(int mask=0;mask<(1<<c1);mask++){
        int f1=0;
        int cur=0;
        int c=0;
        for(int j=0;j<n;j++){
          if(t[j]=='0'){
            f1=f1*2;
          }
          else if(t[j]=='?'){
            f1=f1*2+1;
          }
          else{
            f1=f1*2;
            if(!bit(mask,cur)){
              f1++;
            }
            if(bit(mask,cur)){
              c++;
            }
            cur++;
          }
        }
        // cout<<f<<" "<<f1<<"\n";
        int res=dp0[f1];
        if(c%2==1)ans-=res;
        else ans+=res;
      }
      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 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2396 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 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2396 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 171 ms 6276 KB Output is correct
12 Correct 181 ms 5972 KB Output is correct
13 Correct 310 ms 5288 KB Output is correct
14 Correct 312 ms 5456 KB Output is correct
15 Correct 312 ms 6348 KB Output is correct
16 Correct 451 ms 5892 KB Output is correct
17 Correct 378 ms 5472 KB Output is correct
18 Correct 134 ms 7404 KB Output is correct
19 Correct 178 ms 4300 KB Output is correct
20 Correct 212 ms 6272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2396 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 171 ms 6276 KB Output is correct
12 Correct 181 ms 5972 KB Output is correct
13 Correct 310 ms 5288 KB Output is correct
14 Correct 312 ms 5456 KB Output is correct
15 Correct 312 ms 6348 KB Output is correct
16 Correct 451 ms 5892 KB Output is correct
17 Correct 378 ms 5472 KB Output is correct
18 Correct 134 ms 7404 KB Output is correct
19 Correct 178 ms 4300 KB Output is correct
20 Correct 212 ms 6272 KB Output is correct
21 Correct 250 ms 6804 KB Output is correct
22 Correct 245 ms 6992 KB Output is correct
23 Correct 540 ms 5824 KB Output is correct
24 Correct 567 ms 5544 KB Output is correct
25 Correct 394 ms 7504 KB Output is correct
26 Correct 798 ms 5988 KB Output is correct
27 Correct 637 ms 5932 KB Output is correct
28 Correct 140 ms 8528 KB Output is correct
29 Correct 203 ms 4532 KB Output is correct
30 Correct 259 ms 6892 KB Output is correct
31 Correct 652 ms 6604 KB Output is correct
32 Correct 1418 ms 6560 KB Output is correct
33 Correct 341 ms 5416 KB Output is correct
34 Correct 658 ms 5456 KB Output is correct
35 Correct 653 ms 6224 KB Output is correct
36 Correct 160 ms 4448 KB Output is correct
37 Correct 217 ms 6740 KB Output is correct
38 Correct 257 ms 4436 KB Output is correct
39 Correct 530 ms 5976 KB Output is correct
40 Correct 567 ms 5732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2396 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 56 ms 21236 KB Output is correct
12 Correct 50 ms 22516 KB Output is correct
13 Correct 89 ms 22304 KB Output is correct
14 Correct 331 ms 22348 KB Output is correct
15 Correct 94 ms 22516 KB Output is correct
16 Correct 197 ms 22304 KB Output is correct
17 Correct 163 ms 22556 KB Output is correct
18 Correct 47 ms 22516 KB Output is correct
19 Correct 50 ms 22340 KB Output is correct
20 Correct 56 ms 22548 KB Output is correct
21 Correct 181 ms 22744 KB Output is correct
22 Correct 314 ms 22256 KB Output is correct
23 Correct 69 ms 22632 KB Output is correct
24 Correct 154 ms 22348 KB Output is correct
25 Correct 141 ms 22360 KB Output is correct
26 Correct 48 ms 22344 KB Output is correct
27 Correct 61 ms 22368 KB Output is correct
28 Correct 55 ms 22292 KB Output is correct
29 Correct 95 ms 22348 KB Output is correct
30 Correct 135 ms 22496 KB Output is correct
31 Correct 74 ms 22304 KB Output is correct
32 Correct 204 ms 22300 KB Output is correct
33 Correct 160 ms 22516 KB Output is correct
34 Correct 47 ms 22260 KB Output is correct
35 Correct 120 ms 22288 KB Output is correct
36 Correct 126 ms 22340 KB Output is correct
37 Correct 126 ms 22344 KB Output is correct
38 Correct 134 ms 22288 KB Output is correct
39 Correct 116 ms 22364 KB Output is correct
40 Correct 122 ms 22508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2396 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 171 ms 6276 KB Output is correct
12 Correct 181 ms 5972 KB Output is correct
13 Correct 310 ms 5288 KB Output is correct
14 Correct 312 ms 5456 KB Output is correct
15 Correct 312 ms 6348 KB Output is correct
16 Correct 451 ms 5892 KB Output is correct
17 Correct 378 ms 5472 KB Output is correct
18 Correct 134 ms 7404 KB Output is correct
19 Correct 178 ms 4300 KB Output is correct
20 Correct 212 ms 6272 KB Output is correct
21 Correct 250 ms 6804 KB Output is correct
22 Correct 245 ms 6992 KB Output is correct
23 Correct 540 ms 5824 KB Output is correct
24 Correct 567 ms 5544 KB Output is correct
25 Correct 394 ms 7504 KB Output is correct
26 Correct 798 ms 5988 KB Output is correct
27 Correct 637 ms 5932 KB Output is correct
28 Correct 140 ms 8528 KB Output is correct
29 Correct 203 ms 4532 KB Output is correct
30 Correct 259 ms 6892 KB Output is correct
31 Correct 652 ms 6604 KB Output is correct
32 Correct 1418 ms 6560 KB Output is correct
33 Correct 341 ms 5416 KB Output is correct
34 Correct 658 ms 5456 KB Output is correct
35 Correct 653 ms 6224 KB Output is correct
36 Correct 160 ms 4448 KB Output is correct
37 Correct 217 ms 6740 KB Output is correct
38 Correct 257 ms 4436 KB Output is correct
39 Correct 530 ms 5976 KB Output is correct
40 Correct 567 ms 5732 KB Output is correct
41 Correct 56 ms 21236 KB Output is correct
42 Correct 50 ms 22516 KB Output is correct
43 Correct 89 ms 22304 KB Output is correct
44 Correct 331 ms 22348 KB Output is correct
45 Correct 94 ms 22516 KB Output is correct
46 Correct 197 ms 22304 KB Output is correct
47 Correct 163 ms 22556 KB Output is correct
48 Correct 47 ms 22516 KB Output is correct
49 Correct 50 ms 22340 KB Output is correct
50 Correct 56 ms 22548 KB Output is correct
51 Correct 181 ms 22744 KB Output is correct
52 Correct 314 ms 22256 KB Output is correct
53 Correct 69 ms 22632 KB Output is correct
54 Correct 154 ms 22348 KB Output is correct
55 Correct 141 ms 22360 KB Output is correct
56 Correct 48 ms 22344 KB Output is correct
57 Correct 61 ms 22368 KB Output is correct
58 Correct 55 ms 22292 KB Output is correct
59 Correct 95 ms 22348 KB Output is correct
60 Correct 135 ms 22496 KB Output is correct
61 Correct 74 ms 22304 KB Output is correct
62 Correct 204 ms 22300 KB Output is correct
63 Correct 160 ms 22516 KB Output is correct
64 Correct 47 ms 22260 KB Output is correct
65 Correct 120 ms 22288 KB Output is correct
66 Correct 126 ms 22340 KB Output is correct
67 Correct 126 ms 22344 KB Output is correct
68 Correct 134 ms 22288 KB Output is correct
69 Correct 116 ms 22364 KB Output is correct
70 Correct 122 ms 22508 KB Output is correct
71 Correct 458 ms 46688 KB Output is correct
72 Correct 416 ms 47128 KB Output is correct
73 Correct 1191 ms 45556 KB Output is correct
74 Execution timed out 2041 ms 30500 KB Time limit exceeded
75 Halted 0 ms 0 KB -