poj 2545 Hamming Problem
Hamming Problem
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 5626 | Accepted: 2528 |
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
#include<iostream>
using namespace std;
__int64 mini(__int64 a,__int64 b)
{
if(a<b)
return a;
return b;
}
int main()
{
__int64 p1,p2,p3,ii;
int i,j,k;
int p;
__int64 res[100000];
__int64 resi,resj,resk;
while(scanf("%I64d %I64d %I64d %I64d",&p1,&p2,&p3,&ii)!=EOF)
{
i=0;j=0;k=0;
res[0]=1;
p=1;
while(p<=ii)
{
resi=res[i]*p1;
resj=res[j]*p2;
resk=res[k]*p3;
res[p]=mini(mini(resi,resj),resk);
if(res[p]==resi)i++;
if(res[p]==resj)j++;
if(res[p]==resk)k++;
p++;
}
printf("%I64d\n",res[ii]);
}
return 0;
}
浙公网安备 33010602011771号