一 原题
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 compose 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
二 分析
假如已经知道前n个humble数,第n+1个humble数一定是由某个素数乘前n个humble数中的某一个而得。因为给的素数集合很小(100个),枚举可能的素数就行了。
三 代码
运行结果:
USER: Qi Shen [maxkibb3] TASK: humble LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4568 KB] Test 2: TEST OK [0.000 secs, 4568 KB] Test 3: TEST OK [0.000 secs, 4568 KB] Test 4: TEST OK [0.000 secs, 4568 KB] Test 5: TEST OK [0.000 secs, 4568 KB] Test 6: TEST OK [0.011 secs, 4568 KB] Test 7: TEST OK [0.000 secs, 4568 KB] Test 8: TEST OK [0.000 secs, 4568 KB] Test 9: TEST OK [0.000 secs, 4568 KB] Test 10: TEST OK [0.000 secs, 4568 KB] Test 11: TEST OK [0.000 secs, 4568 KB] Test 12: TEST OK [0.054 secs, 4568 KB] All tests OK.
Your program ('humble') produced all correct answers! This is your submission #3 for this problem. Congratulations!
AC代码:
/*
ID:maxkibb3
LANG:C++
PROG:humble
*/
#include<cstdio>
const int MAXN = 105;
const int MAXM = 100005;
const int INF = 0x7fffffff;
int n, m;
int p[MAXN];
int s[MAXN];
int hum[MAXM];
int main() {
freopen("humble.in", "r", stdin);
freopen("humble.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
hum[0] = 1;
for(int i = 1; i <= m; i++) {
int c = INF;
for(int j = 0; j < n; j++) {
for(int k = s[j]; k < i; k++) {
if(hum[k] * p[j] > hum[i - 1]) {
if(c > hum[k] * p[j])
c = hum[k] * p[j];
s[j] = k;
break;
}
}
}
hum[i] = c;
}
printf("%d\n", hum[m]);
return 0;
}