3.1.3 Humble Numbers
For a given set of K prime numbers S = {p1, p2, ..., pK}, consider the set of all numbers whose prime factors are a subset of S. This set contains, for example, p1, p1p2, p1p1, and p1p2p3 (among others). This is the set of `humble numbers' for the input set S. Note: The number 1 is explicitly declared not to be a humble number.
Your job is to find the Nth humble number for a given set S. Long integers (signed 32-bit) will be adequate for all solutions.
PROGRAM NAME: humble
INPUT FORMAT
Line 1: | Two space separated integers: K and N, 1 <= K <=100 and 1 <= N <= 100,000. |
Line 2: | K space separated positive integers that comprise the set S. |
SAMPLE INPUT (file humble.in)
4 19 2 3 5 7
OUTPUT FORMAT
The Nth humble number from set S printed alone on a line.
SAMPLE OUTPUT (file humble.out)
27
/* ID: makeeca1 PROG: humble LANG: C++ */ #include <cstdio> #include<climits> using namespace std; #define MAX 200 int k,n,a[MAX],ans[100001],dex[MAX],count,min; int main(){ freopen("humble.in","r",stdin); freopen("humble.out","w",stdout); scanf("%d%d",&k,&n); for (int i=0;i<k;i++) scanf("%d",&a[i]); count=1; ans[0]=1; while (count<n+1){ min=INT_MAX; for (int i=0;i<k;i++){ while ((long long)a[i]*ans[dex[i]]<=ans[count-1])dex[i]++; if ((long long )a[i]*ans[dex[i]]<min)min=a[i]*ans[dex[i]]; } ans[count++]=min; } printf("%d\n",ans[n]); return 0; }