[置顶] Hamming Problem(打表,需技巧,不错的题)

Hamming Problem
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

For each three prime numbers p1, p2 and p3, let's define Hamming sequence Hi(p1, p2, p3), i=1, ... as containing in increasing order all the natural numbers whose only prime divisors are p1, p2 or p3.

For example, H(2, 3, 5) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, ...

So H5(2, 3, 5)=6.


Input

In the single line of input file there are space-separated integers p1 p2 p3 i.

Process to the end of file.


Output

The output file must contain the single integer - Hi(p1, p2, p3). All numbers in input and output are less than 10^18.

Sample Input

7 13 19 100

Sample Output

26590291

思路。。。就是打表,而且打表的方法还是挺巧的,参照以下AC code,笔算模拟一下就能明白,其实我觉得这题用STL的set去做也是一个可行的方法,因为set可以保证元素不重复,而且元素从小到大排序。

AC Code:
//Memory: 8056 KB		Time: 0 MS
//Language: GNU C++		Result: Accepted

#include <iostream>
#include <cstdio>
using namespace std;

const int MAXN = 1000000;
long long num[MAXN];

int main()
{
    long long p1, p2, p3;
    int i, k1, k2, k3;
    while(scanf("%I64d %I64d %I64d %d", &p1, &p2, &p3, &i) != EOF)
    {
        num[0] = 1;
        k1 = k2 = k3 = 0;
        for(int j = 1; j != i + 1; j++)
        {
            long long x1 = num[k1]*p1;
            long long x2 = num[k2]*p2;
            long long x3 = num[k3]*p3;
            long long min = x1 < x2 ? x1 : x2;
            if(min > x3) min = x3;
            num[j] = min;
            //注意这里不是if-else if-else结构
            if(num[j] == x1) k1++;
            if(num[j] == x2) k2++;
            if(num[j] == x3) k3++;
        }
        //for(int j = 1; j != i; j++) cout << num[j] << " ";
        printf("%I64d\n", num[i]);
    }
    return 0;
}


posted on 2012-08-09 17:09  铁树银花  阅读(146)  评论(0编辑  收藏  举报

导航