Hamming Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1223 Accepted Submission(s): 530
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
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int NI = 1e5+5;
long long p[3];
long long dp[NI];
int n, a, b, c;
long long Min(long long a, long long b, long long c) {
a = min(a, b);
a = min(a, c);
return a;
}
int main() {
while(~scanf("%I64d%I64d%I64d%d", &p[0], &p[1], &p[2], &n)) {
a = b = c = 0;
dp[0] = 1;
for(int i = 1; i <= n; i++) {
dp[i] = Min(dp[a]*p[0], dp[b]*p[1], dp[c]*p[2]);
if(dp[a]*p[0] == dp[i]) a++;
if(dp[b]*p[1] == dp[i]) b++;
if(dp[c]*p[2] == dp[i]) c++;
}
printf("%I64d\n", dp[n]);
}
return 0;
}