Hamming Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1002 Accepted Submission(s): 422
Problem 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.
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.
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
Source
这个得有丑数的概念才能写!
什么是丑数?
描述:把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
__int64 dp[100001];
__int64 min3(__int64 a,__int64 b,__int64 c)
{
a=min(a,b);
a=min(a,c);
return a;
}
int main()
{
__int64 a,b,c,d;
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)!=EOF)
{
dp[0]=1;
__int64 aa=0,bb=0,cc=0;
__int64 aaa,bbb,ccc;
for(__int64 i=1;i<=d;i++)
{
dp[i]=min3(aaa=dp[aa]*a,bbb=dp[bb]*b,ccc=dp[cc]*c);
if(dp[i]==aaa)
aa++;
if(dp[i]==bbb)
bb++;
if(dp[i]==ccc)
cc++;
}
printf("%I64d\n",dp[d]);
}
}