Hamming Problem

Time Limit:
1000ms
Memory limit:
65536kB
题目描述
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.
输入
In the single line of input file there are space-separated integers p1 p2 p3 i.
输出
The output file must contain the single integer - Hi(p1, p2, p3). All numbers in input and output are less than 10^18.
样例输入
7 13 19 100
样例输出
26590291

解答:

#include <iostream>
using namespace std;
//__int64 p1,p2,p3;
__int64 a[10000001];//设置为101为什么不可以?
__int64 min(__int64 a,__int64 b,__int64 c)//开始时返回值写成了int,一直wa,提交了十几次,在本地测试正确
{
   __int64 min;
   min=a<b?a:b;
   min=min<c?min:c; 
   return min;
}
int main()
{
 int i,n;
 int one,two,three;
 int p1,p2,p3;
 cin>>p1>>p2>>p3>>n;
 one=two=three=0;
 a[0]=1;
 for(i=1;i<=n;i++)
 {
   a[i]=min(p1*a[one],p2*a[two],p3*a[three]);
   if(a[i]==p1*a[one]) one++;
   if(a[i]==p2*a[two]) two++;
   if(a[i]==p3*a[three]) three++; 
 }
 cout<<a[n]<<endl;
 
    //system("pause");
    return 0;
}

posted on 2010-04-11 22:24  蓝牙  阅读(179)  评论(0编辑  收藏  举报