# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
577108 | goodluck2020 | Brunhilda’s Birthday (BOI13_brunhilda) | C++14 | 278 ms | 80780 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |