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
这道题目也是一道常见题目,是一种步步递进的方式获取最终的结果。
时间复杂度为O(k×n),但是由于系数很小,因此最后时间仍然非常快,最复杂的情况也仅仅有0.076ms。
USER: millky wang [ts_mill1]
TASK: humble
LANG: C++
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.000 secs, 3112 KB]
Test 2: TEST OK [0.000 secs, 3112 KB]
Test 3: TEST OK [0.022 secs, 3112 KB]
Test 4: TEST OK [0.000 secs, 3108 KB]
Test 5: TEST OK [0.011 secs, 3112 KB]
Test 6: TEST OK [0.011 secs, 3112 KB]
Test 7: TEST OK [0.022 secs, 3108 KB]
Test 8: TEST OK [0.000 secs, 3112 KB]
Test 9: TEST OK [0.011 secs, 3108 KB]
Test 10: TEST OK [0.011 secs, 3112 KB]
Test 11: TEST OK [0.000 secs, 3112 KB]
Test 12: TEST OK [0.076 secs, 3112 KB]
- /*
- ID: millky
- PROG: humble
- LANG: C++
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cmath>
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- #include<iomanip>
- using namespace std;
- struct factor
- {
- int prime,pt,val;
- };
- bool myComp(const factor& A,const factor& B)
- {
- return A.prime<B.prime;
- }
- int k,n,humble[100000],ea[100],ean;
- factor primes[100];
- int main()
- {
- freopen("humble.in","r",stdin);
- freopen("humble.out","w",stdout);
- int cur,curI;
- scanf("%d%d",&k,&n);
- for (int i=0;i<k;++i) {
- scanf("%d",&primes[i].prime);
- primes[i].pt=0;
- primes[i].val=primes[i].prime;
- }
- sort(primes,primes+k,myComp);
- for (int i=0;i<n;++i)
- {
- ean=0;
- cur=primes[0].val;
- for (int j=1;j<k;++j)
- if(primes[j].val<cur)
- cur=primes[j].val;
- humble[i]=cur;
- for (int j=0;j<k;++j)
- if(primes[j].val==cur)
- primes[j].val=primes[j].prime*humble[primes[j].pt++];
- }
- printf("%d/n",humble[n-1]);
- return 0;
- }