puzzle contents
Contents
raw puzzle

Original Problem

We call a sequence of nn non-negative integers, AA, awesome if there exists some positive integer x>1x>1 such that each element aia_i in AA (where 0i<n0\leq i<n) is evenly divisible by xx. Recall that aa evenly divides bb if there exists some integer cc such that b=acb=a\cdot c.

Given an awesome sequence, AA, and a positive integer, kk, find and print the maximum integer, ll, satisfying the following conditions:

  1. 0lk0\leq l \leq k
  2. A{l}A\cup \{l\} is also awesome

Input Format

The first line contains two space-separated positive integers, nn (the length of sequence AA) and kk (the upper bound on answer ll), respectively.

The second line contains nn space-separated positive integers describing the respective elements in sequence AA (i.e., a0,a1,,an1a_0, a_1, \dots, a_{n-1}).

Constraints

Output Format

Print a single, non-negative integer denoting the value of ll (i.e., the maximum integer k\leq k such that A{l}A\cup \{l\} is awesome). As 00 is evenly divisible by any x>1x>1, the answer will always exist.

Solution

Given x>1x>1 the problem statement defines

A is awesomexaiaiAA\text{ is awesome} \Leftrightarrow x | a_i\forall a_i\in A

We now get an awesome list AA, which says they have a common xx. The largest xx 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 lkl\leq k for which also an xx' exists that divides all aia_i as well as our ll.

This ll should be the greatest under the given constraint. In order to maximize ll, the idea is to take the smallest prime of xx (which we know divides all aia_i already) and make it the new common xx'. Bringing in the new common xx' into ll is then pretty straightforward:

l=xkxl = x'\left\lfloor\frac{k}{x'}\right\rfloor

Finding xx' which we said should be the smallest prime of the GCD can be done with this little O(n)O(\sqrt{n}) 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)