首页 > 其他 > 详细

USACO 3.1 Humble Numbers (humble)

时间:2014-02-27 12:01:51      阅读:463      评论:0      收藏:0      [点我收藏+]
/*
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;
}

USACO 3.1 Humble Numbers (humble),布布扣,bubuko.com

USACO 3.1 Humble Numbers (humble)

原文:http://blog.csdn.net/damonhao/article/details/19975169

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!