답안 #844729

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
844729 2023-09-05T18:32:49 Z damwuan Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1387 ms 55640 KB




#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=(1<<20)+5;
const ll offset=1e9;
const ll block_sz=317;
const ll inf=1e18;
const ll mod=1e9+7;
int l,q,n,dp[maxn],dp1[maxn],f[maxn],ans;
string ss,s;
vector<int> L;
void solve0()
{
    reverse(s.begin(),s.end());
    L.clear();
    int ori=0;
    for1(i,0,s.size()-1)
    {
        if (s[i]=='0') L.pb(i);
        if (s[i]=='1') ori+=(1<<i);
    }
    int g=L.size(),ori2;
    ans=0;
    for1(mask1,0,(1<<g)-1)
    {
        ori2=ori;
        for1(j,0,g-1)
        {
            if ((mask1>>j)&1) ori2+=(1<<L[j]);
        }
        if (__builtin_popcount(mask1)%2==0)
        {
            //cerr<< ori2<<'\n';
            ans+=dp1[ori2];
        }
        else ans-=dp1[ori2];
    }
    cout << ans<<'\n';
}
void solve1()
{
    reverse(s.begin(),s.end());
    L.clear();
    int ori=0;
    for1(i,0,s.size()-1)
    {
        if (s[i]=='1') L.pb(i);
        if (s[i]=='1' || s[i]=='?') ori+=(1<<i);
    }
    int g=L.size(),ori2;
    ans=0;
    for1(mask1,0,(1<<g)-1)
    {
        ori2=ori;
        for1(j,0,g-1)
        {
            if ((mask1>>j)&1) ori2^=(1<<L[j]);
        }
        if (__builtin_popcount(mask1)%2==0)
        {
            //cerr<< ori2<<'\n';
            ans+=dp[ori2];
        }
        else ans-=dp[ori2];
    }
    cout << ans<<'\n';
}
void solve()
{

    reverse(s.begin(),s.end());
    L.clear();
    int ori=0;
    for1(i,0,s.size()-1)
    {
        if (s[i]=='?') L.pb(i);
        if (s[i]=='1') ori+=(1<<i);
    }
    int g=L.size(),ori2;
    ans=0;
    for1(mask1,0,(1<<g)-1)
    {
        ori2=ori;
        for1(j,0,g-1)
        {
            if ((mask1>>j)&1) ori2+=(1<<L[j]);
        }
        ans+=f[ori2];
    }
    cout << ans<<'\n';
}

void sol()
{
    cin >> l>> q>>ss;
    n=1LL<<l;
    for1(i,0,n-1)
    {
        f[i]=dp[i]=dp1[i]=(ss[i]-'0');
    }
    for1(i,0,l-1)
    {
        for1(mask,0,n-1)
        {
            if ((mask>>i)&1)
            {
                dp[mask]+=dp[mask^(1<<i)];
            }
            else
            {
                dp1[mask]+=dp1[mask^(1<<i)];
            }
        }
    }
    /*
    dp la tong mask con
    dp1 la tong mask cha
    */
//    cout << dp1[3];
    while (q--)
    {
        cin >> s;
        int cnt1=0,cnt0=0;
        for1(i,0,s.size()-1)
        {
            if (s[i]=='0') cnt0++;
            if (s[i]=='1') cnt1++;
        }
        if (cnt1<7)
        {
            solve1();
        }
        else if (cnt0<7)
        {
            solve0();
        }
        else
        {
            solve();
        }
    }


}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("GROUPDIV.inp","r",stdin);
//    freopen("GROUPDIV.out","w",stdout);

    int t=1;//cin >> t;
    while (t--)
    {
        sol();
    }
}
/*

3 1
12345678
?11
*/





Compilation message

snake_escaping.cpp: In function 'void solve0()':
snake_escaping.cpp:11:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define for1(i,j,k) for(int i=j;i<=k;i++)
......
   37 |     for1(i,0,s.size()-1)
      |          ~~~~~~~~~~~~~~           
snake_escaping.cpp:37:5: note: in expansion of macro 'for1'
   37 |     for1(i,0,s.size()-1)
      |     ^~~~
snake_escaping.cpp: In function 'void solve1()':
snake_escaping.cpp:11:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define for1(i,j,k) for(int i=j;i<=k;i++)
......
   65 |     for1(i,0,s.size()-1)
      |          ~~~~~~~~~~~~~~           
snake_escaping.cpp:65:5: note: in expansion of macro 'for1'
   65 |     for1(i,0,s.size()-1)
      |     ^~~~
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:11:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define for1(i,j,k) for(int i=j;i<=k;i++)
......
   94 |     for1(i,0,s.size()-1)
      |          ~~~~~~~~~~~~~~           
snake_escaping.cpp:94:5: note: in expansion of macro 'for1'
   94 |     for1(i,0,s.size()-1)
      |     ^~~~
snake_escaping.cpp: In function 'void sol()':
snake_escaping.cpp:11:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define for1(i,j,k) for(int i=j;i<=k;i++)
......
  144 |         for1(i,0,s.size()-1)
      |              ~~~~~~~~~~~~~~       
snake_escaping.cpp:144:9: note: in expansion of macro 'for1'
  144 |         for1(i,0,s.size()-1)
      |         ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 452 ms 19284 KB Output is correct
12 Correct 240 ms 18928 KB Output is correct
13 Correct 284 ms 18256 KB Output is correct
14 Correct 275 ms 18256 KB Output is correct
15 Correct 248 ms 19120 KB Output is correct
16 Correct 390 ms 18516 KB Output is correct
17 Correct 402 ms 18600 KB Output is correct
18 Correct 152 ms 20296 KB Output is correct
19 Correct 467 ms 17116 KB Output is correct
20 Correct 433 ms 18796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 452 ms 19284 KB Output is correct
12 Correct 240 ms 18928 KB Output is correct
13 Correct 284 ms 18256 KB Output is correct
14 Correct 275 ms 18256 KB Output is correct
15 Correct 248 ms 19120 KB Output is correct
16 Correct 390 ms 18516 KB Output is correct
17 Correct 402 ms 18600 KB Output is correct
18 Correct 152 ms 20296 KB Output is correct
19 Correct 467 ms 17116 KB Output is correct
20 Correct 433 ms 18796 KB Output is correct
21 Correct 861 ms 22492 KB Output is correct
22 Correct 267 ms 22356 KB Output is correct
23 Correct 454 ms 21540 KB Output is correct
24 Correct 380 ms 21332 KB Output is correct
25 Correct 302 ms 23124 KB Output is correct
26 Correct 525 ms 21936 KB Output is correct
27 Correct 500 ms 21844 KB Output is correct
28 Correct 164 ms 24296 KB Output is correct
29 Correct 857 ms 20348 KB Output is correct
30 Correct 432 ms 22468 KB Output is correct
31 Correct 422 ms 22212 KB Output is correct
32 Correct 380 ms 22356 KB Output is correct
33 Correct 285 ms 21336 KB Output is correct
34 Correct 518 ms 21448 KB Output is correct
35 Correct 494 ms 21584 KB Output is correct
36 Correct 152 ms 20328 KB Output is correct
37 Correct 247 ms 22352 KB Output is correct
38 Correct 674 ms 20172 KB Output is correct
39 Correct 923 ms 21368 KB Output is correct
40 Correct 532 ms 21328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 41 ms 28400 KB Output is correct
12 Correct 42 ms 28452 KB Output is correct
13 Correct 57 ms 28440 KB Output is correct
14 Correct 80 ms 28400 KB Output is correct
15 Correct 54 ms 28400 KB Output is correct
16 Correct 82 ms 28268 KB Output is correct
17 Correct 78 ms 28404 KB Output is correct
18 Correct 44 ms 28608 KB Output is correct
19 Correct 43 ms 28280 KB Output is correct
20 Correct 42 ms 28404 KB Output is correct
21 Correct 55 ms 28404 KB Output is correct
22 Correct 74 ms 28420 KB Output is correct
23 Correct 48 ms 28400 KB Output is correct
24 Correct 93 ms 28256 KB Output is correct
25 Correct 86 ms 28416 KB Output is correct
26 Correct 36 ms 28148 KB Output is correct
27 Correct 40 ms 28456 KB Output is correct
28 Correct 47 ms 28152 KB Output is correct
29 Correct 57 ms 28404 KB Output is correct
30 Correct 73 ms 28276 KB Output is correct
31 Correct 48 ms 28348 KB Output is correct
32 Correct 91 ms 28404 KB Output is correct
33 Correct 83 ms 28408 KB Output is correct
34 Correct 34 ms 28148 KB Output is correct
35 Correct 74 ms 28436 KB Output is correct
36 Correct 72 ms 28468 KB Output is correct
37 Correct 76 ms 28256 KB Output is correct
38 Correct 70 ms 28436 KB Output is correct
39 Correct 68 ms 28268 KB Output is correct
40 Correct 68 ms 28404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 452 ms 19284 KB Output is correct
12 Correct 240 ms 18928 KB Output is correct
13 Correct 284 ms 18256 KB Output is correct
14 Correct 275 ms 18256 KB Output is correct
15 Correct 248 ms 19120 KB Output is correct
16 Correct 390 ms 18516 KB Output is correct
17 Correct 402 ms 18600 KB Output is correct
18 Correct 152 ms 20296 KB Output is correct
19 Correct 467 ms 17116 KB Output is correct
20 Correct 433 ms 18796 KB Output is correct
21 Correct 861 ms 22492 KB Output is correct
22 Correct 267 ms 22356 KB Output is correct
23 Correct 454 ms 21540 KB Output is correct
24 Correct 380 ms 21332 KB Output is correct
25 Correct 302 ms 23124 KB Output is correct
26 Correct 525 ms 21936 KB Output is correct
27 Correct 500 ms 21844 KB Output is correct
28 Correct 164 ms 24296 KB Output is correct
29 Correct 857 ms 20348 KB Output is correct
30 Correct 432 ms 22468 KB Output is correct
31 Correct 422 ms 22212 KB Output is correct
32 Correct 380 ms 22356 KB Output is correct
33 Correct 285 ms 21336 KB Output is correct
34 Correct 518 ms 21448 KB Output is correct
35 Correct 494 ms 21584 KB Output is correct
36 Correct 152 ms 20328 KB Output is correct
37 Correct 247 ms 22352 KB Output is correct
38 Correct 674 ms 20172 KB Output is correct
39 Correct 923 ms 21368 KB Output is correct
40 Correct 532 ms 21328 KB Output is correct
41 Correct 41 ms 28400 KB Output is correct
42 Correct 42 ms 28452 KB Output is correct
43 Correct 57 ms 28440 KB Output is correct
44 Correct 80 ms 28400 KB Output is correct
45 Correct 54 ms 28400 KB Output is correct
46 Correct 82 ms 28268 KB Output is correct
47 Correct 78 ms 28404 KB Output is correct
48 Correct 44 ms 28608 KB Output is correct
49 Correct 43 ms 28280 KB Output is correct
50 Correct 42 ms 28404 KB Output is correct
51 Correct 55 ms 28404 KB Output is correct
52 Correct 74 ms 28420 KB Output is correct
53 Correct 48 ms 28400 KB Output is correct
54 Correct 93 ms 28256 KB Output is correct
55 Correct 86 ms 28416 KB Output is correct
56 Correct 36 ms 28148 KB Output is correct
57 Correct 40 ms 28456 KB Output is correct
58 Correct 47 ms 28152 KB Output is correct
59 Correct 57 ms 28404 KB Output is correct
60 Correct 73 ms 28276 KB Output is correct
61 Correct 48 ms 28348 KB Output is correct
62 Correct 91 ms 28404 KB Output is correct
63 Correct 83 ms 28408 KB Output is correct
64 Correct 34 ms 28148 KB Output is correct
65 Correct 74 ms 28436 KB Output is correct
66 Correct 72 ms 28468 KB Output is correct
67 Correct 76 ms 28256 KB Output is correct
68 Correct 70 ms 28436 KB Output is correct
69 Correct 68 ms 28268 KB Output is correct
70 Correct 68 ms 28404 KB Output is correct
71 Correct 287 ms 52724 KB Output is correct
72 Correct 403 ms 52848 KB Output is correct
73 Correct 699 ms 51312 KB Output is correct
74 Correct 1089 ms 51692 KB Output is correct
75 Correct 483 ms 53752 KB Output is correct
76 Correct 1179 ms 51916 KB Output is correct
77 Correct 1170 ms 52020 KB Output is correct
78 Correct 212 ms 55640 KB Output is correct
79 Correct 388 ms 49648 KB Output is correct
80 Correct 374 ms 52724 KB Output is correct
81 Correct 679 ms 52732 KB Output is correct
82 Correct 994 ms 51708 KB Output is correct
83 Correct 439 ms 50908 KB Output is correct
84 Correct 1387 ms 51984 KB Output is correct
85 Correct 1202 ms 51808 KB Output is correct
86 Correct 194 ms 49704 KB Output is correct
87 Correct 387 ms 52724 KB Output is correct
88 Correct 496 ms 49752 KB Output is correct
89 Correct 629 ms 51564 KB Output is correct
90 Correct 900 ms 52016 KB Output is correct
91 Correct 501 ms 51104 KB Output is correct
92 Correct 1083 ms 52276 KB Output is correct
93 Correct 1237 ms 52268 KB Output is correct
94 Correct 196 ms 49652 KB Output is correct
95 Correct 926 ms 51836 KB Output is correct
96 Correct 939 ms 52048 KB Output is correct
97 Correct 924 ms 52208 KB Output is correct
98 Correct 936 ms 51912 KB Output is correct
99 Correct 947 ms 51952 KB Output is correct
100 Correct 949 ms 51668 KB Output is correct