Humble Numbers(技巧)

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

 

       题意:

       给出 N(1 ~ 100)个素数 和 K (1 ~ 100000)。输出第 K 个数,这个数的质因子只能由这 N 个里面的数组成。规定 1 不算入其中。

 

       思路:

       技巧。与第二次个人排位赛素数筛选那道题类似,为了方便计算,所以把 1 也算入其中。用数组 hum 记录前 100000 个数( hum 数组绝对是从小到大排序的 ),更新的时候只需要更新到 K 为止。对于每一个素数 num[ i ] 对应都有一个下标值 fir [ i ]。当需要更新第 j 个 hum 时,每个一个素数都要找到第一个 fir [ i ] 满足 num [ i ] * hum [ fir [ i ] ] > hum [ j - 1 ] ,同时比较取得每个素数 num [ i ] * hum [ fir [ i ] ] 的最小值,这个最小值即为 hum [ j ] 的值。如此更新下去,最后输出 hum [ k ] 即可。

 

        AC:

/*
TASK:humble
LANG:C++
ID:sum-g1
*/

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;

const ll INF = 999999999999;

ll num[105], fir[105];
ll hum[100005];

int main() {
        freopen("humble.in", "r", stdin);
        freopen("humble.out", "w", stdout);

        int n, k;
        scanf("%d%d", &n, &k);

        for (int i = 1; i <= n; ++i) {
                scanf("%lld", &num[i]);
                fir[i] = 0;
        }

        hum[0] = 1;
        for (int i = 1; i <= k; ++i) {
                ll Min = INF;

                for (int j = 1; j <= n; ++j) {
                        while (num[j] * hum[ fir[j] ] <= hum[i - 1]) ++fir[j];
                        if (num[j] * hum[ fir[j] ] < Min) 
                            Min = num[j] * hum[ fir[j] ];
                }

                hum[i] = Min;
        }

        printf("%lld\n", hum[k]);

        return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值