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>
using namespace std;
template<class T> struct Seg { // comb(ID,b) = b
const T ID = 1e9; T comb(T a, T b) { return min(a,b); }
int n; vector<T> seg;
void init(int _n) { n = _n; seg.assign(2*n,ID); }
void pull(int p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
void upd(int p, T val) { // set val at position p
seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
T query(int l, int r) { // min on interval [l, r]
T ra = ID, rb = ID;
for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
if (l&1) ra = comb(ra,seg[l++]);
if (r&1) rb = comb(seg[--r],rb);
}
return comb(ra,rb);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int M, Q; cin >> M >> Q;
vector<int> p(M);
for (int i = 0; i < M; i++) {
cin >> p[i];
}
sort(p.begin(), p.end());
int mx = 1000000;
int res[mx];
int lpf[mx];
for (int i = 0; i < mx; i++) {
lpf[i] = -1;
}
for (int j = 0; j < p.size(); j++) {
for (int i = p[j]; i < mx; i += p[j]) {
lpf[i] = j;
}
}
int dp[M];
multiset<int> ms;
for (int i = 0; i < M; i++) {
dp[i] = 0;
ms.insert(dp[i]);
}
for (int i = 1; i < mx; i++) {
int ans = 1e9;
if (lpf[i] == -1) {
ans = *ms.begin() + 1;
} else {
vector<int> invalid;
int x = i;
while (lpf[x] != -1) {
invalid.push_back(lpf[x]);
int a = p[lpf[x]];
while (x % a == 0) x /= a;
}
for (int j: invalid) {
assert(ms.count(dp[j]));
ms.erase(ms.find(dp[j]));
}
if (!ms.empty()) {
ans = *ms.begin() + 1;
}
for (int j: invalid) {
ms.insert(dp[j]);
}
for (int j: invalid) {
if (j >= 0 && j < (int)p.size()) {
ms.erase(ms.find(dp[j]));
dp[j] = ans;
ms.insert(dp[j]);
}
}
}
res[i] = ans;
}
while (Q--) {
int x;
cin >> x;
if (res[x] == (int)1e9) {
cout << "oo\n";
} else {
cout << res[x] << '\n';
}
}
}
Compilation message (stderr)
brunhilda.cpp: In function 'int main()':
brunhilda.cpp:34:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int j = 0; j < p.size(); j++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |