zoukankan      html  css  js  c++  java
  • 3.1.3 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

    /*
    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
    ",ans[n]);
        return 0;
    }
  • 相关阅读:
    堆(优先队列)模板
    线段树(递归)模板
    快速幂(含二阶方阵类)模板
    ACM/CF赛制getstart模板
    [Andrew Stankevich's Contest#21] Lempel-Ziv Compression
    [NOIP2001]Car的旅行路线
    [NOIP2007] 矩阵取数游戏
    [NOIP2010] 关押罪犯
    [NOIP1999] 拦截导弹
    设计模式之第1章-工厂方法模式(Java实现)
  • 原文地址:https://www.cnblogs.com/makeecat/p/3289222.html
Copyright © 2011-2022 走看看