#include <iostream> #include <algorithm> using namespace std; short is_valid(long long a, short *p, short k) { long long s = sqrt(a); for (int i=0; i<k && p[i]<=a; i++) { if (a == p[i]) return 1; if (p[i] <=s && a % p[i] == 0) return a / p[i]; } return 0; } int main() { short k; long long n; cin >> k >> n; short p[k]; for (int i=0; i<k; i++) cin >> p[i]; sort(p, p+k); long long tmp = n; while (tmp != 1) { if (!tmp) tmp = --n; tmp = is_valid(tmp, p, k); } cout << n; }
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 | #include <iostream> #include <algorithm> using namespace std; short is_valid(long long a, short *p, short k) { long long s = sqrt(a); for (int i=0; i<k && p[i]<=a; i++) { if (a == p[i]) return 1; if (p[i] <=s && a % p[i] == 0) return a / p[i]; } return 0; } int main() { short k; long long n; cin >> k >> n; short p[k]; for (int i=0; i<k; i++) cin >> p[i]; sort(p, p+k); long long tmp = n; while (tmp != 1) { if (!tmp) tmp = --n; tmp = is_valid(tmp, p, k); } cout << n; } |