답안 #437214

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
437214 2021-06-26T04:54:35 Z cpp219 Snake Escaping (JOI18_snake_escaping) C++14
65 / 100
2000 ms 17712 KB
#pragma GCC optimization O2
#pragma GCC optimization "unroll-loop"
#pragma target ("avx2")

#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = (1 << 20) + 9;
const ll inf = 1e9 + 7;
char c;
string s;
ll a[N];
ll L,Q,dp[N],dpR[N];

bool bit(ll x,ll i){
    return (x >> i) & 1;
}

ll take,now,ans;

void fQ(ll pos){
    if (pos >= L){
        //cout<<now<<"x\n";
        ans += a[now]; return;
    }
    if (s[pos] == '1'){
        now = now*2 + 1; fQ(pos + 1); now = (now - 1)/2;
    }
    else if (s[pos] == '0'){
        now = now*2; fQ(pos + 1); now /= 2;
    }
    else{
        now = now*2; fQ(pos + 1); now /= 2;
        now = now*2 + 1; fQ(pos + 1); now = (now - 1)/2;
    }
}

void f1(ll pos){
    if (pos >= L){
        //cout<<now<<"x\n";
        if (take%2 == 0) ans += dp[now];
        else ans -= dp[now];
        return;
    }
    if (s[pos] == '1'){
        now = now*2; take++; f1(pos + 1); now /= 2; take--;
        now = now*2 + 1; f1(pos + 1); now = (now - 1)/2;
    }
    else if (s[pos] == '0'){
        now = now*2; f1(pos + 1); now /= 2;
    }
    else{
        now = now*2 + 1; f1(pos + 1); now = (now - 1)/2;
    }
}

void f0(ll pos){
    if (pos >= L){
        if (take%2 == 0) ans += dpR[now];
        else ans -= dpR[now];
        return;
    }
    if (s[pos] == '1'){
        now = now*2 + 1; f0(pos + 1); now = (now - 1)/2;
    }
    else if (s[pos] == '0'){
        now = now*2 + 1; take++; f0(pos + 1); now = (now - 1)/2; take--;
        now = now*2; f0(pos + 1); now /= 2;
    }
    else{
        now = now*2; f0(pos + 1); now /= 2;
    }
}

int main(){
    ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".INP","r")){
        freopen(task".INP","r",stdin);
        //freopen(task".OUT","w",stdout);
    }
    cin>>L>>Q;
    for (ll i = 0;i < (1 << L);i++){
        cin>>c; a[i] = c - '0';
        dp[i] += a[i]; dpR[i] += a[i];
    }

    for (ll i = 0;i < L;i++){
        for (ll mask = 0;mask < (1 << L);mask++)
            if (bit(mask,i)) dp[mask] += dp[mask ^ (1 << i)];
        for (ll mask = (1 << L) - 1;mask >= 0;mask--)
            if (!bit(mask,i)) dpR[mask] += dpR[mask ^ (1 << i)];
    }

    while(Q--){
        cin>>s; ll cnt1 = 0,cnt0 = 0,cntQ = 0; take = now = ans = 0;
        for (ll i = 0;i < L;i++){
            if (s[i] == '1') cnt1++;
            else if (s[i] == '0') cnt0++;
            else cntQ++;
        }
        if (cntQ <= 6) fQ(0);
        else if (cnt1 <= 6) f1(0);
        else f0(0);
        cout<<ans<<"\n";
    }
}

Compilation message

snake_escaping.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    1 | #pragma GCC optimization O2
      | 
snake_escaping.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
snake_escaping.cpp:3: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
    3 | #pragma target ("avx2")
      | 
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 873 ms 15160 KB Output is correct
12 Correct 725 ms 14816 KB Output is correct
13 Correct 515 ms 14040 KB Output is correct
14 Correct 533 ms 14004 KB Output is correct
15 Correct 1282 ms 15052 KB Output is correct
16 Correct 676 ms 14232 KB Output is correct
17 Correct 652 ms 14212 KB Output is correct
18 Correct 272 ms 16068 KB Output is correct
19 Correct 304 ms 13088 KB Output is correct
20 Correct 793 ms 14660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 873 ms 15160 KB Output is correct
12 Correct 725 ms 14816 KB Output is correct
13 Correct 515 ms 14040 KB Output is correct
14 Correct 533 ms 14004 KB Output is correct
15 Correct 1282 ms 15052 KB Output is correct
16 Correct 676 ms 14232 KB Output is correct
17 Correct 652 ms 14212 KB Output is correct
18 Correct 272 ms 16068 KB Output is correct
19 Correct 304 ms 13088 KB Output is correct
20 Correct 793 ms 14660 KB Output is correct
21 Execution timed out 2077 ms 17712 KB Time limit exceeded
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 112 ms 14912 KB Output is correct
12 Correct 132 ms 14892 KB Output is correct
13 Correct 186 ms 14792 KB Output is correct
14 Correct 260 ms 14764 KB Output is correct
15 Correct 164 ms 15004 KB Output is correct
16 Correct 273 ms 14908 KB Output is correct
17 Correct 258 ms 14932 KB Output is correct
18 Correct 95 ms 15076 KB Output is correct
19 Correct 102 ms 14744 KB Output is correct
20 Correct 121 ms 15020 KB Output is correct
21 Correct 188 ms 14788 KB Output is correct
22 Correct 280 ms 14868 KB Output is correct
23 Correct 169 ms 14824 KB Output is correct
24 Correct 260 ms 14772 KB Output is correct
25 Correct 241 ms 14860 KB Output is correct
26 Correct 103 ms 14644 KB Output is correct
27 Correct 113 ms 14908 KB Output is correct
28 Correct 101 ms 14756 KB Output is correct
29 Correct 180 ms 14768 KB Output is correct
30 Correct 252 ms 14836 KB Output is correct
31 Correct 134 ms 14696 KB Output is correct
32 Correct 277 ms 14852 KB Output is correct
33 Correct 251 ms 14764 KB Output is correct
34 Correct 89 ms 14724 KB Output is correct
35 Correct 196 ms 14768 KB Output is correct
36 Correct 256 ms 14844 KB Output is correct
37 Correct 194 ms 14784 KB Output is correct
38 Correct 241 ms 14808 KB Output is correct
39 Correct 201 ms 14788 KB Output is correct
40 Correct 207 ms 14948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 873 ms 15160 KB Output is correct
12 Correct 725 ms 14816 KB Output is correct
13 Correct 515 ms 14040 KB Output is correct
14 Correct 533 ms 14004 KB Output is correct
15 Correct 1282 ms 15052 KB Output is correct
16 Correct 676 ms 14232 KB Output is correct
17 Correct 652 ms 14212 KB Output is correct
18 Correct 272 ms 16068 KB Output is correct
19 Correct 304 ms 13088 KB Output is correct
20 Correct 793 ms 14660 KB Output is correct
21 Execution timed out 2077 ms 17712 KB Time limit exceeded
22 Halted 0 ms 0 KB -