Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define FOR(i, a, b) for (int i = a; i < b; ++i)
- #define RFOR(i, a, b) for (int i = a; i >= b; --i)
- #define ll long long
- #pragma GCC optimize("O3,unroll-loops")
- #define int long long
- using namespace std;
- int erat[1500001];
- signed main() {
- // O(n log n)
- ios_base::sync_with_stdio(false); cin.tie(0);
- int n, k; cin >> n >> k;
- int mx = 0;
- int a[n];
- FOR(i, 0, n) {
- cin >> a[i];
- mx = max(mx, a[i]);
- }
- vector <int> d[mx + 1];
- for (int i = 2; i <= mx; ++i) {
- if (!erat[i]) {
- erat[i] = i;
- d[i] = {i};
- for (int j = i; j <= mx; j += i) if (!erat[j]) erat[j] = i;
- }
- else {
- int c = i;
- while (c != 1) {
- d[i].push_back(erat[c]);
- c /= erat[c];
- }
- }
- }
- priority_queue <int> cur;
- cur.push(1);
- FOR(i, 0, n) cur.push(a[i]);
- while (true) { // O(n log^2 n)
- int now = cur.top();
- cur.pop();
- int nx = cur.top();
- if (now == 1 || k < d[now][0]) {
- cout << now;
- return 0;
- }
- if (now / d[now][0] <= nx) {
- k -= d[now][0];
- now /= d[now][0];
- cur.push(now);
- continue;
- }
- int l = 0, r = d[now].size();
- while (l < r - 1) {
- int m = (l + r) / 2;
- if (d[now][m] <= k && now / d[now][m] > nx) l = m;
- else r = m;
- }
- if (r != d[now].size()) {
- k -= d[now][r];
- now /= d[now][r];
- }
- else {
- k -= d[now][l];
- now /= d[now][l];
- }
- cur.push(now);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement