Submission #923667

#TimeUsernameProblemLanguageResultExecution timeMemory
923667sleepntsheepBrunhilda’s Birthday (BOI13_brunhilda)C++17
0 / 100
1099 ms84060 KiB
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
#include <complex>

using namespace std;

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,tune=native")

#define ALL(x) begin(x), end(x)
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);

#if 1
#define M 100005
#define N 10000005
#else
#define M 20000
#define N 20000
#endif

int m, q, dp[N], p[M], yes[N];

multiset<int> ms;

int main()
{
    memset(dp, 63, sizeof dp);
    ShinLena;

    dp[0] = 0;
    cin >> m >> q;
    for (int i = 1; i <= m; ++i)
    {
        cin >> p[i];
        yes[p[i]] = 1;
    }

    auto factorize = [&](int n) {
        vector<int> f, nf;
        for (int d : {2, 3, 5}) {
            if (n % d == 0) {
                f.push_back(d);
                n /= d;
                while (n % d == 0) n /= d;
            }
        }


        static array<int, 8> increments = {4, 2, 4, 2, 4, 6, 2, 6};
        int i = 0;

        for (long long d = 7; d * d <= n; d += increments[i++]) {
            if (n % d == 0) {
                f.push_back(d);
                n /= d;
                while (n % d == 0) n /= d;
            }
            if (i == 8) i = 0;
        }

        if (n > 1) f.push_back(n);

        return f;
    };

    for (int i = 0; i < m; ++i) ms.insert(dp[0]);

    for (int i = 1; i < N; ++i)
    {
        auto F = factorize(i);
        for (auto x : F) if (yes[x])
            ms.erase(ms.find(dp[i-x]));

        if (ms.size())
            dp[i] = min(dp[i], *ms.begin() + 1);

        for (auto x : F) if (yes[x])
            ms.insert(dp[i]);
    }

    for (int x, i = 1; i <= q; ++i)
    {
        cin >> x;
        if (dp[x] > 1e9) cout << "oo\n";
        else cout << dp[x] << '\n';
    }


    return 0;
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...