#include <algorithm> #include <iostream> #include <vector> #include <queue> #include <cassert> #include <cmath> constexpr int64_t INF = int64_t(1e18)+5; constexpr int N_SETS = 4; struct Pair { int a, b; int64_t val; bool operator<(const Pair& r) const { return val < r.val; } bool operator>(const Pair& r) const { return val > r.val; } }; int64_t nPrimes, limit; std::vector<int64_t> allPrimes, primes[N_SETS]; std::vector<int64_t> elems[N_SETS]; double balance[N_SETS]; std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> lowQue; std::priority_queue<Pair> highQue; inline int64_t safeMult(int64_t a, int64_t b) { constexpr int64_t OVER = 1ll << 31; if (a >= OVER && b >= OVER) { return INF; } if (((a >> 31)*b + (b >> 31)*a) >= OVER) { return INF; } int64_t c = a*b; return (c <= limit ? c : INF); } void compute(int part, int64_t current, int since) { elems[part].push_back(current); for (unsigned i = since; i < primes[part].size(); i++) { int64_t next = safeMult(current, primes[part][i]); if (next <= limit) { compute(part, next, i); } } } void popQueLow() { Pair top = lowQue.top(); lowQue.pop(); int a = top.a, b = top.b+1; if (b >= int(elems[1].size())) { return; } int64_t mult = safeMult(elems[0][a], elems[1][b]); if (mult <= limit) { lowQue.push(Pair{a, b, mult}); } } void popQueHigh(int64_t low) { Pair top = highQue.top(); highQue.pop(); int a = top.a, b = top.b; while (--b >= 0) { int64_t tmp = elems[2][a]*elems[3][b]; if (safeMult(low, tmp) <= limit) { highQue.push(Pair{a, b, tmp}); break; } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cin >> nPrimes >> limit; allPrimes.resize(nPrimes); for (auto& p : allPrimes) { std::cin >> p; } // Partition (good enough) sort(allPrimes.begin(), allPrimes.end()); for (auto& x : balance) { x = 0.0; } for (auto p : allPrimes) { int best = 0; for (int i = 1; i < N_SETS; i++) { if (balance[i] < balance[best]) { best = i; } } primes[best].push_back(p); balance[best] += 1 / (std::log(p)+1); } // Compute partial results for (int i = 0; i < N_SETS; i++) { compute(i, 1, 0); sort(elems[i].begin(), elems[i].end()); } // Init queues for (unsigned i = 0; i < elems[0].size(); i++) { int64_t mult = safeMult(elems[0][i], elems[1][0]); if (mult <= limit) { lowQue.push(Pair{int(i), 0, mult}); } } int j = elems[3].size()-1; for (unsigned i = 0; i < elems[2].size(); i++) { int64_t lowVal = elems[2][i], cur = 0; while (j >= 0) { cur = safeMult(lowVal, elems[3][j]); if (cur <= limit) { break; } j--; } if (cur <= 0) { assert(j < 0); break; } highQue.push(Pair{int(i), j, cur}); } // Combine int64_t best = 1; while (!lowQue.empty() && !highQue.empty()) { int64_t lowVal = lowQue.top().val, cur = 0; while (!highQue.empty()) { cur = safeMult(lowVal, highQue.top().val); if (cur <= limit) { break; } popQueHigh(lowVal); } best = std::max(best, cur); popQueLow(); } std::cout << best << std::endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include <algorithm> #include <iostream> #include <vector> #include <queue> #include <cassert> #include <cmath> constexpr int64_t INF = int64_t(1e18)+5; constexpr int N_SETS = 4; struct Pair { int a, b; int64_t val; bool operator<(const Pair& r) const { return val < r.val; } bool operator>(const Pair& r) const { return val > r.val; } }; int64_t nPrimes, limit; std::vector<int64_t> allPrimes, primes[N_SETS]; std::vector<int64_t> elems[N_SETS]; double balance[N_SETS]; std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> lowQue; std::priority_queue<Pair> highQue; inline int64_t safeMult(int64_t a, int64_t b) { constexpr int64_t OVER = 1ll << 31; if (a >= OVER && b >= OVER) { return INF; } if (((a >> 31)*b + (b >> 31)*a) >= OVER) { return INF; } int64_t c = a*b; return (c <= limit ? c : INF); } void compute(int part, int64_t current, int since) { elems[part].push_back(current); for (unsigned i = since; i < primes[part].size(); i++) { int64_t next = safeMult(current, primes[part][i]); if (next <= limit) { compute(part, next, i); } } } void popQueLow() { Pair top = lowQue.top(); lowQue.pop(); int a = top.a, b = top.b+1; if (b >= int(elems[1].size())) { return; } int64_t mult = safeMult(elems[0][a], elems[1][b]); if (mult <= limit) { lowQue.push(Pair{a, b, mult}); } } void popQueHigh(int64_t low) { Pair top = highQue.top(); highQue.pop(); int a = top.a, b = top.b; while (--b >= 0) { int64_t tmp = elems[2][a]*elems[3][b]; if (safeMult(low, tmp) <= limit) { highQue.push(Pair{a, b, tmp}); break; } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cin >> nPrimes >> limit; allPrimes.resize(nPrimes); for (auto& p : allPrimes) { std::cin >> p; } // Partition (good enough) sort(allPrimes.begin(), allPrimes.end()); for (auto& x : balance) { x = 0.0; } for (auto p : allPrimes) { int best = 0; for (int i = 1; i < N_SETS; i++) { if (balance[i] < balance[best]) { best = i; } } primes[best].push_back(p); balance[best] += 1 / (std::log(p)+1); } // Compute partial results for (int i = 0; i < N_SETS; i++) { compute(i, 1, 0); sort(elems[i].begin(), elems[i].end()); } // Init queues for (unsigned i = 0; i < elems[0].size(); i++) { int64_t mult = safeMult(elems[0][i], elems[1][0]); if (mult <= limit) { lowQue.push(Pair{int(i), 0, mult}); } } int j = elems[3].size()-1; for (unsigned i = 0; i < elems[2].size(); i++) { int64_t lowVal = elems[2][i], cur = 0; while (j >= 0) { cur = safeMult(lowVal, elems[3][j]); if (cur <= limit) { break; } j--; } if (cur <= 0) { assert(j < 0); break; } highQue.push(Pair{int(i), j, cur}); } // Combine int64_t best = 1; while (!lowQue.empty() && !highQue.empty()) { int64_t lowVal = lowQue.top().val, cur = 0; while (!highQue.empty()) { cur = safeMult(lowVal, highQue.top().val); if (cur <= limit) { break; } popQueHigh(lowVal); } best = std::max(best, cur); popQueLow(); } std::cout << best << std::endl; return 0; } |