USACO Humble Numbers

Code

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
/*
ID: wcr19961
PROG: humble
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
using namespace std;

typedef long long LL;
const int maxn = 110;
int a[maxn];

int main() {
freopen("humble.in", "r", stdin);
freopen("humble.out", "w", stdout);
int k, n;
scanf("%d%d", &k, &n);
for (int i = 0; i < k; ++i) {
scanf("%d", &a[i]);
}
set<LL> s;
s.insert(1);
//++n;
for (int i = 0; ; ++i) {
LL x = *s.begin();
s.erase(s.begin());
if (i == n) {
printf("%lld\n", x);
return 0;
}
for (int j = 0; j < k; ++j) {
LL tmp = x * a[j];
if (!s.count(tmp)) {
if (s.size() < n - i) {
s.insert(tmp);
} else if (s.size() == n - i && *s.rbegin() > tmp) {
s.erase((++s.rbegin()).base());
s.insert(tmp);
}
}
}
}
return 0;
}