题目链接:http://acm.tju.edu.cn/toj/showp1753.html
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 535 Accepted Runs: 251 Multiple test files
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.
InputIn the single line of input there are space-separated integers p1 p2 p3 i.
OutputThe output must contain the single integer - Hi(p1, p2, p3). All numbers in input and output are less than 1018.
Sample Input
7 13 19 100Sample Output
26590291
Source: Northeastern Europe 2000 Far-Eastern Subregion
关于丑数的一道题,丑数相关知识见:http://blog.csdn.net/acm_zl/article/details/10613073
知道怎么求丑数,这道题就很简单了:
#include <stdio.h>
long long Min(long long a,long long b,long long c){
if(a<b)
return a<c?a:c;
else
return b<c?b:c;
}
int main()
{
int num2,num3,num5,n;
while(~scanf("%d%d%d%d",&num2,&num3,&num5,&n)){
const int Max=n;
long long Prime[Max+1];
int index2=0,index3=0,index5=0;
Prime[0]=1;
for(int i=0;i<n;i++){
long long minnum=Min(Prime[index2]*num2,Prime[index3]*num3,Prime[index5]*num5);
if(minnum==Prime[index2]*num2)
index2++;
if(minnum==Prime[index3]*num3)
index3++;
if(minnum==Prime[index5]*num5)
index5++;
Prime[i+1]=minnum;
}
printf("%lld\n",Prime[n]);
}
}