답안 #991622

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
991622 2024-06-02T17:04:58 Z GrindMachine Snake Escaping (JOI18_snake_escaping) C++17
5 / 100
2000 ms 18264 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

/*



*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

void solve(int test_case)
{
    int n,q; cin >> n >> q;
    string s; cin >> s;
    int B = min(n,12);

    vector<int> pow3(n+5);
    pow3[0] = 1;
    rep1(i,n) pow3[i] = pow3[i-1]*3;

    vector<pii> queries(q);
    rep(id,q){
        string t; cin >> t;
        int first_t = 0, last_t = 0;
        rep(i,sz(t)){
            ll x = 0;
            if(t[i] == '0') x = 0;
            if(t[i] == '1') x = 1;
            if(t[i] == '?') x = 2;
            if(i < B) first_t = first_t*3+x;
            else last_t = last_t*3+x;
        }

        queries[id] = {first_t,last_t};
    }

    auto get_mask = [&](ll x){
        int mask = 0;
        rep(bit,n-B){
            int b = 0;
            if(x&(1<<bit)) b = 1;
            mask += pow3[bit]*b;
        }

        return mask;
    };

    auto get_lsq = [&](ll x){
        int pos = 0;
        while(x){
            int b = x%3;
            x /= 3;
            if(b == 2) return pos;
            pos++; 
        }
    
        return -1;        
    };

    auto ok = [&](int mask1, int mask2){
        rev(b,B-1,0){
            int b1 = 0, b2 = 0;
            b1 = mask1%3;
            mask1 /= 3;
            if(mask2&(1<<(B-b-1))) b2 = 1;

            if(b1 == 2 or b1 == b2){

            }
            else{
                return false;
            }
        }

        return true;
    };

    vector<int> ans(q);
    vector<int> dp(1<<(n-B));

    rep(mask1,1<<B){
        fill(all(dp),0);
        
        rep(i,1<<n){
            int firstB = i>>(n-B);
            int lastB = i^(firstB<<(n-B));
            if(firstB != mask1) conts;
            dp[get_mask(lastB)] += s[i]-'0';
        }

        rep(mask2,pow3[n-B]){
            int lsq = get_lsq(mask2);
            if(lsq == -1) conts;
            rep(x,2){
                int mask3 = mask2-pow3[lsq]*2+pow3[lsq]*x;
                dp[mask2] += dp[mask3];
            }
        }

        rep(id,q){
            if(ok(queries[id].ff,mask1)){
                ans[id] += dp[queries[id].ss];
            }
        }
    }

    rep(i,q) cout << ans[i] << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 348 KB Output is correct
2 Correct 16 ms 468 KB Output is correct
3 Correct 14 ms 348 KB Output is correct
4 Correct 17 ms 456 KB Output is correct
5 Correct 19 ms 348 KB Output is correct
6 Correct 16 ms 508 KB Output is correct
7 Correct 16 ms 476 KB Output is correct
8 Correct 21 ms 348 KB Output is correct
9 Correct 11 ms 348 KB Output is correct
10 Correct 16 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 348 KB Output is correct
2 Correct 16 ms 468 KB Output is correct
3 Correct 14 ms 348 KB Output is correct
4 Correct 17 ms 456 KB Output is correct
5 Correct 19 ms 348 KB Output is correct
6 Correct 16 ms 508 KB Output is correct
7 Correct 16 ms 476 KB Output is correct
8 Correct 21 ms 348 KB Output is correct
9 Correct 11 ms 348 KB Output is correct
10 Correct 16 ms 348 KB Output is correct
11 Execution timed out 2058 ms 18264 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 348 KB Output is correct
2 Correct 16 ms 468 KB Output is correct
3 Correct 14 ms 348 KB Output is correct
4 Correct 17 ms 456 KB Output is correct
5 Correct 19 ms 348 KB Output is correct
6 Correct 16 ms 508 KB Output is correct
7 Correct 16 ms 476 KB Output is correct
8 Correct 21 ms 348 KB Output is correct
9 Correct 11 ms 348 KB Output is correct
10 Correct 16 ms 348 KB Output is correct
11 Execution timed out 2058 ms 18264 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 348 KB Output is correct
2 Correct 16 ms 468 KB Output is correct
3 Correct 14 ms 348 KB Output is correct
4 Correct 17 ms 456 KB Output is correct
5 Correct 19 ms 348 KB Output is correct
6 Correct 16 ms 508 KB Output is correct
7 Correct 16 ms 476 KB Output is correct
8 Correct 21 ms 348 KB Output is correct
9 Correct 11 ms 348 KB Output is correct
10 Correct 16 ms 348 KB Output is correct
11 Execution timed out 2101 ms 4348 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 348 KB Output is correct
2 Correct 16 ms 468 KB Output is correct
3 Correct 14 ms 348 KB Output is correct
4 Correct 17 ms 456 KB Output is correct
5 Correct 19 ms 348 KB Output is correct
6 Correct 16 ms 508 KB Output is correct
7 Correct 16 ms 476 KB Output is correct
8 Correct 21 ms 348 KB Output is correct
9 Correct 11 ms 348 KB Output is correct
10 Correct 16 ms 348 KB Output is correct
11 Execution timed out 2058 ms 18264 KB Time limit exceeded
12 Halted 0 ms 0 KB -