Submission #405248

#TimeUsernameProblemLanguageResultExecution timeMemory
405248balbitPacking Biscuits (IOI20_biscuits)C++14
100 / 100
19 ms1352 KiB
#include "biscuits.h"
#include <bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
#define pii pair<int, int>
#define f first
#define s second

#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)((x).size())
#define pb push_back

#define REP(i,n) for (int i = 0; i<n; ++i)

#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x){ cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S && ...y){ cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT



const int maxn = 3e5+5;

ll cnt[62][62]; // number of constraints from these ones
ll lm[62]; // what's my constraint?

ll c2(ll x, vector<ll> a) {
    while(SZ(a) < 61) a.pb(0);
    int K = 61;
    ll sm = 0;
    memset(cnt, 0, sizeof cnt);
    memset(lm, 0, sizeof lm);
    for (int i = 0; i<K; ++i) {
        sm += (1ll<<i) * a[i];
        ll ho = sm / x;
        lm[i] = ho;
    }
    cnt[K-1][K-1] = 1;
    ll re = 0;
    for (int i = 0; i<=200000; ++i) { bool ok = 1;
        for (int j = 0; j<K; ++j) {
            if ((i & ((1ll<<(j+1))-1)) > lm[j]) {
                ok = 0; break;
            }
        }
        re += ok;
    }
    return re;
}

long long count_tastiness(long long x, std::vector<long long> a) {
    while(SZ(a) < 61) a.pb(0);
    int K = 61;
    ll sm = 0;
    memset(cnt, 0, sizeof cnt);
    memset(lm, 0, sizeof lm);
    for (int i = 0; i<K; ++i) {
        sm += (1ll<<i) * a[i];
        ll ho = sm / x;
        lm[i] = min((1ll<<(i+1))-1, ho);
    }
    cnt[K-1][K-1] = 1;
    ll re = 0;


    for (int i = K-1; i>=0; --i) {
        ll msk = (1ll<<(i+1)) - 1;
        for (int frm = K-1; frm >= i; --frm) {
            if (cnt[i][frm]) {
//                bug(i, frm, cnt[i][frm]);
                ll dis = lm[frm] & msk;
                if (dis > lm[i]) {
                    cnt[i][i] += cnt[i][frm];
                }else{
                    if (dis == 0) re += cnt[i][frm];
                    else{
                        if (dis >= (1ll<<i)) {
                            // can choose to not take
                            (i?cnt[i-1][i-1]:re) += cnt[i][frm];
                        }
                        {
                            // need to be restrained
                            (i?cnt[i-1][frm]:re) += cnt[i][frm];
                        }
                    }
                }
            }
        }
    }
    return re;

}

#ifdef BALBIT
signed main(){
    REP(ccc, 1000) {
        ll x = 1;
        vector<int> a;
        REP(i,3) a.pb(rand()%3);
        ll A = count_tastiness(x,a);
        ll B = c2(x,a);
        if (A!=B) {
            bug(A,B);
            bug(x);
            for (int y : a) cout<<y<<' ';
            cout<<endl;
            assert(0);

        }
    }
}
#endif // BALBIT

#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...