USACO 3.1 Humble Numbers (humble)

/*
usaco official solution
We compute the first n humble numbers in the "hum" array. For simplicity of implementation, 
we treat 1 as a humble number, and adjust accordingly.
Once we have the first k humble numbers and want to compute the k+1st, we do the following:

        for each prime p
                find the minimum humble number h
                  such that h * p is bigger than the last humble number.
        take the smallest h * p found: that's the next humble number.
To speed up the search, we keep an index "pindex" of what h is for each prime, 
and start there rather than at the beginning of the list.
*/

/*
ID: haolink1
PROG: humble
LANG: C++
*/

//#include <iostream>
#include <fstream>

using namespace std;
int humble[100001];
int prime[100];
int prime_index[100];
int main(){
    int K = 0,N = 0;
    ifstream fin("humble.in");
    fin >> K >> N;
    for(int i = 0; i < K; i++){
        fin >> prime[i]; 
    }
    int num = 1;
    humble[0] = 1;
    while(num != N + 1){
        //I consider the below min should be more suitalbe;
        //long long min = 0x100000000;
        //thought 0x7FFFFFFF is enough for the test data;
        long long min = 0x7FFFFFFF;
        int m = -1;
        for(int i = 0; i < K; i++){
            while(prime[i] * humble[prime_index[i]] <= humble[num - 1])
                prime_index[i]++;
            if((long long)prime[i] * humble[prime_index[i]] < min){
                min = prime[i] * humble[prime_index[i]];
                m = i;
            }
        }
        humble[num++] = min;
        prime_index[m]++;
    }
    ofstream fout("humble.out");
    fout << humble[N] << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值