Two out of Three
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
The ancient Greek philosopher Pythagoras placed great importance on numbers, considering them the
foundation of the world. Numbers that possessed certain mathematical properties were attributed mystical
meanings. For example, odd numbers in Pythagoreanism were considered masculine, while even numbers
were considered feminine. The divisibility of a number by others was a significant distinguishing feature
for Pythagoras. Now he is interested in numbers that are sufficiently divisible but not entirely perfect in
this regard. To this end, he has written down an increasing sequence of natural numbers that are divisible
by exactly two out of three predetermined numbers a, b, and c. Help Pythagoras find the n-th number in
this sequence.
Input
The first line contains the integers a, b, c (1 ≤ a, b, c ≤ 106 ), which Pythagoras checks for divisibility.
The second line contains an integer n (1 ≤ n ≤ 1018 ) — the index of the element in the sequence that
Pythagoras wants to find. The elements are numbered starting from one.
Output
Output the n-th natural number that is divisible by exactly two of the three numbers a, b, c.
If such a number does not exist or exceeds 1018 , output −1.
Examples
standard input standard output
2 3 5 42
10
5 6 7 13999986
1000000
Note
The sequence of natural numbers that are divisible by exactly two of the three numbers 2, 3, and 5 looks
like this: 6, 10, 12, 15, 18, 20, 24, 36, 40, 42, 45, 48, 50, . . . . The tenth term of this sequence is 42.
Page 1 of 1