Submission #431862

# Submission time Handle Problem Language Result Execution time Memory
431862 2021-06-17T16:23:49 Z hackerbhaiya Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1288 ms 43308 KB
#include<bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization("unroll-loops")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC optimize("no-stack-protector")
#define ll long long
// #define ll ll
#define f(i,a,b) for(ll i=a;i<b;i++)
// #define mod 1000000007
#define mod 998244353
#define mp make_pair
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define ff first
#define ss second
#define rf(i,a,b) for(ll i=a;i>=b;i--)
#define sc(a) scanf("%lld",&a)
#define pf prllf
#define sz(a) (ll)(a.size())
#define psf push_front
#define ppf pop_front
#define ppb pop_back
#define pb push_back
#define pq priority_queue
#define all(s) s.begin(),s.end()
#define sp(a) setprecision(a)
#define rz resize
#define ld long double
#define inf (ll)1e18
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define eb emplace_back
const double pi = acos(-1);
// ll binpow(ll a, ll b){ll res=1;while(b!=0){if(b&1)res*=a;a*=a;b>>=1;}return res;}
ll binpow(ll a, ll b, ll md){ll res=1;while(b!=0){if(b&1)res*=a,res%=md;a*=a,a%=md;b>>=1;}return res%md;}
 
using namespace std; 

int n,q;
string s;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // freopen("gift1.in","r",stdin);
    // freopen("gift1.out","w",stdout);
// #ifndef ONLINE_JUDGE
//     freopen("input.txt","r",stdin);
//     freopen("output.txt","w",stdout);
// #endif
    int z=1;
    // cin>>z;
    while(z--)
    {     
        cin>>n>>q>>s;
        int sup[1<<n|1],sub[1<<n|1],cnt[1<<n|1];
        for(int mask=0;mask<(1<<n);mask++)
            sub[mask]=sup[mask]=(s[mask]-'0'),cnt[mask]=__builtin_popcount(mask);
        f(i,0,n)
        {
            for(int mask=0;mask<(1<<n);mask++)
            {
                if(mask&(1<<i))
                    sub[mask]+=sub[mask^(1<<i)];
                else
                    sup[mask]+=sup[mask^(1<<i)];
            }
        }
        while(q--)
        {
            string str;
            cin>>str;
            reverse(all(str));
            int mask1=0,mask2=0,mask3=0;
            f(i,0,n)
            {
                if(str[i]=='?')
                    mask1|=(1<<i);
                else if(str[i]=='0')
                    mask2|=(1<<i);
                else
                    mask3|=(1<<i);
            }
            int ans=0;
            if(cnt[mask1]<=cnt[mask2] && cnt[mask1]<=cnt[mask3])
            {
                for(int submask=mask1;submask>0;submask=((submask-1)&mask1))
                    ans+=(s[mask3|submask]-'0');
                ans+=(s[mask3]-'0');
            }
            else if(cnt[mask2]<=cnt[mask3] && cnt[mask2]<=cnt[mask1])
            {
                for(int submask=mask2;submask>0;submask=(mask2&(submask-1)))
                    ans+=(((cnt[submask]&1)?-1:1)*sup[submask|mask3]);
                ans+=sup[mask3];
            }
            else
            {
                for(int submask=mask3;submask>0;submask=(mask3&(submask-1)))
                    ans+=(((cnt[submask]&1)?-1:1)*sub[submask|mask1]);
                ans+=sub[mask1];
            }
            cout<<abs(ans)<<"\n";
        }
    }       
}   
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 324 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 320 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 324 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 320 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 239 ms 15160 KB Output is correct
12 Correct 264 ms 14724 KB Output is correct
13 Correct 303 ms 13952 KB Output is correct
14 Correct 273 ms 14080 KB Output is correct
15 Correct 267 ms 15012 KB Output is correct
16 Correct 329 ms 14148 KB Output is correct
17 Correct 318 ms 14180 KB Output is correct
18 Correct 198 ms 16036 KB Output is correct
19 Correct 235 ms 12996 KB Output is correct
20 Correct 315 ms 14644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 324 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 320 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 239 ms 15160 KB Output is correct
12 Correct 264 ms 14724 KB Output is correct
13 Correct 303 ms 13952 KB Output is correct
14 Correct 273 ms 14080 KB Output is correct
15 Correct 267 ms 15012 KB Output is correct
16 Correct 329 ms 14148 KB Output is correct
17 Correct 318 ms 14180 KB Output is correct
18 Correct 198 ms 16036 KB Output is correct
19 Correct 235 ms 12996 KB Output is correct
20 Correct 315 ms 14644 KB Output is correct
21 Correct 306 ms 18108 KB Output is correct
22 Correct 286 ms 18296 KB Output is correct
23 Correct 317 ms 17272 KB Output is correct
24 Correct 324 ms 17052 KB Output is correct
25 Correct 303 ms 19140 KB Output is correct
26 Correct 358 ms 17588 KB Output is correct
27 Correct 351 ms 17536 KB Output is correct
28 Correct 212 ms 20124 KB Output is correct
29 Correct 266 ms 16060 KB Output is correct
30 Correct 291 ms 18316 KB Output is correct
31 Correct 331 ms 18244 KB Output is correct
32 Correct 346 ms 18112 KB Output is correct
33 Correct 282 ms 17044 KB Output is correct
34 Correct 332 ms 17116 KB Output is correct
35 Correct 348 ms 17632 KB Output is correct
36 Correct 215 ms 16068 KB Output is correct
37 Correct 276 ms 18132 KB Output is correct
38 Correct 284 ms 16096 KB Output is correct
39 Correct 314 ms 17220 KB Output is correct
40 Correct 325 ms 17188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 324 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 320 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 73 ms 16028 KB Output is correct
12 Correct 66 ms 16008 KB Output is correct
13 Correct 66 ms 15892 KB Output is correct
14 Correct 109 ms 15972 KB Output is correct
15 Correct 64 ms 16068 KB Output is correct
16 Correct 90 ms 15952 KB Output is correct
17 Correct 80 ms 15944 KB Output is correct
18 Correct 49 ms 16100 KB Output is correct
19 Correct 59 ms 15844 KB Output is correct
20 Correct 61 ms 16100 KB Output is correct
21 Correct 70 ms 16068 KB Output is correct
22 Correct 76 ms 15932 KB Output is correct
23 Correct 57 ms 15940 KB Output is correct
24 Correct 79 ms 15972 KB Output is correct
25 Correct 82 ms 15944 KB Output is correct
26 Correct 46 ms 15844 KB Output is correct
27 Correct 58 ms 16036 KB Output is correct
28 Correct 57 ms 15808 KB Output is correct
29 Correct 70 ms 15892 KB Output is correct
30 Correct 66 ms 15940 KB Output is correct
31 Correct 58 ms 15932 KB Output is correct
32 Correct 85 ms 15936 KB Output is correct
33 Correct 82 ms 16024 KB Output is correct
34 Correct 49 ms 15904 KB Output is correct
35 Correct 77 ms 15972 KB Output is correct
36 Correct 77 ms 15940 KB Output is correct
37 Correct 74 ms 16000 KB Output is correct
38 Correct 70 ms 15972 KB Output is correct
39 Correct 69 ms 15932 KB Output is correct
40 Correct 92 ms 16028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 324 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 320 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 239 ms 15160 KB Output is correct
12 Correct 264 ms 14724 KB Output is correct
13 Correct 303 ms 13952 KB Output is correct
14 Correct 273 ms 14080 KB Output is correct
15 Correct 267 ms 15012 KB Output is correct
16 Correct 329 ms 14148 KB Output is correct
17 Correct 318 ms 14180 KB Output is correct
18 Correct 198 ms 16036 KB Output is correct
19 Correct 235 ms 12996 KB Output is correct
20 Correct 315 ms 14644 KB Output is correct
21 Correct 306 ms 18108 KB Output is correct
22 Correct 286 ms 18296 KB Output is correct
23 Correct 317 ms 17272 KB Output is correct
24 Correct 324 ms 17052 KB Output is correct
25 Correct 303 ms 19140 KB Output is correct
26 Correct 358 ms 17588 KB Output is correct
27 Correct 351 ms 17536 KB Output is correct
28 Correct 212 ms 20124 KB Output is correct
29 Correct 266 ms 16060 KB Output is correct
30 Correct 291 ms 18316 KB Output is correct
31 Correct 331 ms 18244 KB Output is correct
32 Correct 346 ms 18112 KB Output is correct
33 Correct 282 ms 17044 KB Output is correct
34 Correct 332 ms 17116 KB Output is correct
35 Correct 348 ms 17632 KB Output is correct
36 Correct 215 ms 16068 KB Output is correct
37 Correct 276 ms 18132 KB Output is correct
38 Correct 284 ms 16096 KB Output is correct
39 Correct 314 ms 17220 KB Output is correct
40 Correct 325 ms 17188 KB Output is correct
41 Correct 73 ms 16028 KB Output is correct
42 Correct 66 ms 16008 KB Output is correct
43 Correct 66 ms 15892 KB Output is correct
44 Correct 109 ms 15972 KB Output is correct
45 Correct 64 ms 16068 KB Output is correct
46 Correct 90 ms 15952 KB Output is correct
47 Correct 80 ms 15944 KB Output is correct
48 Correct 49 ms 16100 KB Output is correct
49 Correct 59 ms 15844 KB Output is correct
50 Correct 61 ms 16100 KB Output is correct
51 Correct 70 ms 16068 KB Output is correct
52 Correct 76 ms 15932 KB Output is correct
53 Correct 57 ms 15940 KB Output is correct
54 Correct 79 ms 15972 KB Output is correct
55 Correct 82 ms 15944 KB Output is correct
56 Correct 46 ms 15844 KB Output is correct
57 Correct 58 ms 16036 KB Output is correct
58 Correct 57 ms 15808 KB Output is correct
59 Correct 70 ms 15892 KB Output is correct
60 Correct 66 ms 15940 KB Output is correct
61 Correct 58 ms 15932 KB Output is correct
62 Correct 85 ms 15936 KB Output is correct
63 Correct 82 ms 16024 KB Output is correct
64 Correct 49 ms 15904 KB Output is correct
65 Correct 77 ms 15972 KB Output is correct
66 Correct 77 ms 15940 KB Output is correct
67 Correct 74 ms 16000 KB Output is correct
68 Correct 70 ms 15972 KB Output is correct
69 Correct 69 ms 15932 KB Output is correct
70 Correct 92 ms 16028 KB Output is correct
71 Correct 508 ms 40344 KB Output is correct
72 Correct 563 ms 40420 KB Output is correct
73 Correct 674 ms 39028 KB Output is correct
74 Correct 1004 ms 39376 KB Output is correct
75 Correct 609 ms 41372 KB Output is correct
76 Correct 1129 ms 39640 KB Output is correct
77 Correct 1043 ms 39580 KB Output is correct
78 Correct 348 ms 43308 KB Output is correct
79 Correct 478 ms 37236 KB Output is correct
80 Correct 548 ms 40564 KB Output is correct
81 Correct 913 ms 40372 KB Output is correct
82 Correct 1288 ms 39412 KB Output is correct
83 Correct 537 ms 38480 KB Output is correct
84 Correct 953 ms 39260 KB Output is correct
85 Correct 1068 ms 39552 KB Output is correct
86 Correct 303 ms 37356 KB Output is correct
87 Correct 527 ms 40300 KB Output is correct
88 Correct 504 ms 37260 KB Output is correct
89 Correct 659 ms 39008 KB Output is correct
90 Correct 645 ms 39436 KB Output is correct
91 Correct 485 ms 38452 KB Output is correct
92 Correct 1168 ms 39668 KB Output is correct
93 Correct 981 ms 39624 KB Output is correct
94 Correct 342 ms 37308 KB Output is correct
95 Correct 802 ms 39452 KB Output is correct
96 Correct 826 ms 39516 KB Output is correct
97 Correct 861 ms 39412 KB Output is correct
98 Correct 906 ms 39428 KB Output is correct
99 Correct 899 ms 39672 KB Output is correct
100 Correct 855 ms 39508 KB Output is correct