#include <iostream> #include <sstream> #include <string> #include <stdlib.h> #include <algorithm> #include <vector> #include <iterator> using namespace std; struct greaterDesc { template<class T> bool operator()(T const &a, T const &b) const { return a > b; } }; unsigned long long GetHighestNumber(vector<short> primes, unsigned long long n) { unsigned long long highest = 1; for (unsigned long long k = 0; k < primes.size(); k++) { while (highest * primes[k] < n) { highest = highest * primes.back(); } } for (unsigned long long i = n; i >= highest; i--) { unsigned long long test = i; bool _continue = true; while (_continue) { _continue = false; for (unsigned long long j = 0; j < primes.size(); j++) { if (test % primes[j] == 0) { _continue = true; test = test / primes[j]; if (test == 1) { return i; } } } } } return highest; } int main() { unsigned long long k, n; cin >> k >> n; vector<string> lines; string line; short counter = 0; while (counter < 1 && getline(cin >> ws, line)) { if (!line.empty()) { lines.push_back(line); counter++; } } short prime; vector<short> primes; stringstream primes_line(lines[0]); while (primes_line >> prime) primes.push_back(prime); sort(primes.begin(), primes.end(), greaterDesc()); unsigned long long result = GetHighestNumber(primes, n); cout << result; }
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 | #include <iostream> #include <sstream> #include <string> #include <stdlib.h> #include <algorithm> #include <vector> #include <iterator> using namespace std; struct greaterDesc { template<class T> bool operator()(T const &a, T const &b) const { return a > b; } }; unsigned long long GetHighestNumber(vector<short> primes, unsigned long long n) { unsigned long long highest = 1; for (unsigned long long k = 0; k < primes.size(); k++) { while (highest * primes[k] < n) { highest = highest * primes.back(); } } for (unsigned long long i = n; i >= highest; i--) { unsigned long long test = i; bool _continue = true; while (_continue) { _continue = false; for (unsigned long long j = 0; j < primes.size(); j++) { if (test % primes[j] == 0) { _continue = true; test = test / primes[j]; if (test == 1) { return i; } } } } } return highest; } int main() { unsigned long long k, n; cin >> k >> n; vector<string> lines; string line; short counter = 0; while (counter < 1 && getline(cin >> ws, line)) { if (!line.empty()) { lines.push_back(line); counter++; } } short prime; vector<short> primes; stringstream primes_line(lines[0]); while (primes_line >> prime) primes.push_back(prime); sort(primes.begin(), primes.end(), greaterDesc()); unsigned long long result = GetHighestNumber(primes, n); cout << result; } |