题目链接
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.
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
Northeastern Europe 2000 - Far-Eastern Subregion
思路:很巧妙的一个方法,但自己怎么也想不到。。。
我们发现由这三个因子组成的数要产生一个新的数的话无非就*a,*b,*c,用三个pos来控制a,b,c的位置,具体看代码。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=5e5+100;
ll num[maxn],a,b,c,k,posa,posb,posc;
int main()
{
while(scanf("%lld%lld%lld%d",&a,&b,&c,&k)!=EOF){
posa=posb=posc=0;num[0]=1;
for(int i=1;i<=k;++i)
{
num[i]=min(num[posa]*a,min(num[posb]*b,num[posc]*c));
if(num[i]==num[posa]*a) posa++;
if(num[i]==num[posb]*b) posb++;
if(num[i]==num[posc]*c) posc++;
}
printf("%lld\n",num[k]);
}
}