Submission #1177716

#TimeUsernameProblemLanguageResultExecution timeMemory
1177716InvMODSnake Escaping (JOI18_snake_escaping)C++20
100 / 100
621 ms20908 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;




void solve()
{
    int L,q; cin >> L >> q;

    vector<vector<int>> dp(2, vector<int>((1 << L), 0));

    vector<int> a((1 << L));
    FOR(i, 0, (1 << L) - 1){
        char c; cin >> c;

        int v = c - '0';
        a[i] = dp[0][i] = dp[1][i] = v;
    }

    // bruh toi bi ngu dp sos :))
    FOR(i, 0, L - 1){
        FOR(mask, 0, (1 << L) - 1){
            dp[mask >> i & 1][mask] += dp[mask >> i & 1][mask ^ (1 << i)];
        }
    }

    // dp[0][i]: count of x that i & x = i (subdad) <(")
    // dp[1][i]: count of x that i & x = x (submask)

    while(q--){
        string s; cin >> s;

        int mask_zero = 0, mask_one = 0, mask_ques = 0;
        FOR(i, 0, L - 1){
            (s[i] == '?' ? mask_ques : (s[i] == '0' ? mask_zero : mask_one)) |= (1 << (L - 1 - i));
        }

        // choose the minimum mask to optimize time
        if(__builtin_popcount(mask_ques) <= L / 3){
            int ans = 0;
            for(int mask = mask_ques; mask >= 0; mask = ((mask - 1) & mask_ques)){
                ans += a[mask_one | mask];
                if(!mask) break;
            }
            cout << ans << "\n";
        }
        else if(__builtin_popcount(mask_one) <= L / 3){
            int ans = 0;

            // pie in sos dp (we need to calculate all mask that has mask one and submask of mask_ques)
            for(int mask = mask_one; mask >= 0; mask = (mask - 1) & mask_one){
                int parity = __builtin_popcount(mask_one) - __builtin_popcount(mask);
                if(parity & 1){
                    ans = ans - dp[1][mask | mask_ques];
                }
                else ans = ans + dp[1][mask | mask_ques];
                if(!mask) break;
            }

            cout << ans << "\n";
        }
        else{
            int ans = 0;

            // we need to have subdad of mask one and no mask zero

            for(int mask = mask_zero; mask >= 0; mask = (mask - 1) & mask_zero){
                int parity = __builtin_popcount(mask);
                if(parity & 1){
                    ans = ans - dp[0][mask | mask_one];
                }
                else{
                    ans = ans + dp[0][mask | mask_one];
                }
                if(!mask) break;
            }

            cout << ans << "\n";
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; //cin >> t;
    while(t--) solve();
    return 0;
}

Compilation message (stderr)

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...