Submission #577108

#TimeUsernameProblemLanguageResultExecution timeMemory
577108goodluck2020Brunhilda’s Birthday (BOI13_brunhilda)C++14
100 / 100
278 ms80780 KiB
#include <bits/stdc++.h>
#define task "GROUP"
#define MASK(n) (1ll << (n))
#define getBit(bit, i) (((bit) >> (i)) & 1)
using namespace std;
using lli = long long;
using pii = pair<int, int>;
const int N = 1e7 + 1;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;

int dp[N], n, q, nxt[N];
vector<int> a;

void readInput() {
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        int x; cin >> x;
        a.push_back(x);
    }
    sort(a.begin(), a.end());
    a.resize(unique(a.begin(), a.end()) - a.begin());
    for (auto x: a)
        for (int i = x; i < N; i += x)
            nxt[i] = x;
}

bool minimize(int &a, int b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

void solve() {
    queue<int> heap;
    for (int i = 1; i < a.back(); i++) heap.push(i);
    for (int i = a.back(); i < N; i++) dp[i] = INF;
    while (!heap.empty()) {
        int u = heap.front(); heap.pop();
        int x = nxt[u];
        for (int i = min(x - 1, N - 1 - u); i >= 1; i--)
            if (minimize(dp[u + i], dp[u] + 1)) heap.push(u + i);
            else break;
    }
    while (q--) {
        int x; cin >> x;
        if (dp[x] == INF) cout << "oo";
        else cout << dp[x] + 1;
        cout << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int t = 1;
    while (t--) {
        readInput();
        solve();
    }
    return 0;
}

Compilation message (stderr)

brunhilda.cpp: In function 'int main()':
brunhilda.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
brunhilda.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...