1

Input for this problem is an array A of positive integers and single positive integer k. The output of the program is True if k is in the set S defined below, False otherwise.

Define the set S as follows:

  1. if x is in A then x is in S
  2. if x and y are in S, then GCD(x,y) is in S
  3. if x and y are in S, then LCD(x,y) is in S

Additional constraints: The size of the array A is ≤ 50000, k ≤ 1012, and x ≤ 1012 for each x in A. The program must return an answer in 1 second or less.

I don't have any good idea. The only thing I can think of is to find GCD and LCM of any pair of integers in the array, then I can enlarge the set S. But as the S is expanding, the new integers will be new pairs to make more GCDs and LCMs.

I hope you guys help me. I have been stuck with this for long.

Update:

For instance an array is given is A= { 10, 12, 15 };

As we have mentioned, the S = {..., 10, 12, 15, ...}

We know that 10, 12, 15 is in S. So their GCD and LCM is in S too. Which means:

GCD(10, 12) = 2 in S

GCD(10, 15) = 5 in S

GCD(12, 15) = 3 in S

LCM(10, 12) = 60 in S

...

Finally:

S = { 1, 2, 3, 5, 10, 12, 15, 30, 60 }

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • "It is consider that, all integers in the array are in S. " - Sorry, to me at least, this question is very unclear. – Ami Tavory Dec 26 '15 at 12:28
  • 2
    @AmiTavory: Let A bet the set of integers in the given array. Then S is the augmented set such that every element of A is in S and the GCD and LCM of two elements of S are in S. You are asked to check if a given k is in S. For instance A= { 10, 12, 15 } => S = { 1, 2, 3, 5, 10, 12, 15, 30, 60 } –  Dec 26 '15 at 12:59
  • @YvesDaoust has just said the point. :) – Hoang Van Thien Dec 26 '15 at 13:43
  • 1
    One observation: By noting the [prime power definitions of GCD and LCM](http://math.stackexchange.com/questions/624163/gcd-is-min-of-exponents-of-prime-factors-lcm-is-max-of-exponents-of-prime-facto) it should be clear that the elements of set S contain no prime divisors that were not already present in the original array. – President James K. Polk Dec 26 '15 at 15:07
  • I have updated the question. Thanks Ami for the example. And I am sorry if this question is not clear for you guys. :) – Hoang Van Thien Dec 26 '15 at 15:25
  • (Now this is reopened, a link to the [follow-on question](http://stackoverflow.com/questions/34472494/check-if-an-integer-is-gcd-of-some-elements-in-a-given-set) seems in order.) – greybeard Jan 01 '16 at 09:38

1 Answers1

2

Factor k into powers of distinct primes. For each such prime power factor, compute the GCD of all array elements of which it is a divisor. If (and only if) some GCD is not a divisor of k, then S does not contain k.

The correctness proof involves the fact that the positive integers are a distributive lattice with operators GCD and LCM.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120