We call a sequence of non-negative integers, , awesome if there exists some positive integer such that each element in (where ) is evenly divisible by . Recall that evenly divides if there exists some integer such that .
Given an awesome sequence, , and a positive integer, , find and print the maximum integer, , satisfying the following conditions:
- is also awesome
Input Format
The first line contains two space-separated positive integers, (the length of sequence ) and (the upper bound on answer ), respectively.
The second line contains space-separated positive integers describing the respective elements in sequence (i.e., ).
Constraints
Output Format
Print a single, non-negative integer denoting the value of (i.e., the maximum integer such that is awesome). As is evenly divisible by any , the answer will always exist.
Solution
Given the problem statement defines
We now get an awesome list , which says they have a common . The largest is the GCD of the entire list, which can be determined in Ruby with
x = A.reduce(:gcd)
We're now looking for the largest for which also an exists that divides all as well as our .
This should be the greatest under the given constraint. In order to maximize , the idea is to take the smallest prime of (which we know divides all already) and make it the new common . Bringing in the new common into is then pretty straightforward:
Finding which we said should be the smallest prime of the GCD can be done with this little helper function:
def smallestPrime(n)
i = 2
while i * i <= n do
return i if n % i == 0
i+= 1
end
return n
end
The solution is thus
k = gets.strip.split.map(&:to_i)[1]
A = gets.strip.split.map(&:to_i)
x = A.reduce(:gcd)
x_= smallestPrime(x)
puts(x_ * (k / x_).floor)