-1

The original statement for this problem can be found here

This is a question from IEEExtream 2014. There is an array of integers given. Input is X, so output is the number of subsets where there GCD equals to X.

Eg:

OriginalSet = {2, 3, 5, 6, 6}
numberOfSubsets(2) => 3 // there are 3 subsets where their GCD equals to 2
i.e. {2, 6}, {2, 6}, {2, 6}

Limits:

OriginalSet.length < pow(10, 5),
element_of_an_array < pow(10, 4),
X < pow(10,4)

I bruteforced the solution and it gives me TLE. I am stuck on this question for few days now. Could you explain me how to solve this efficiently.

Edit

  • the subset size is not limited to 2. It is from the power-set.
  • It guarantees that for a given query there is only 100 unique numbers.
Alex Stack
  • 13
  • 3

2 Answers2

2

See the solution for "585E - Present for Vitalik the Philatelist" here where is says "Let's calculate the number of subsets with gcd equal to 1". I hope you can extend the logic from there to solve the problem for X.

e_noether
  • 1,289
  • 2
  • 12
  • 19
1

You can use dynamic programming approach to solve this problem; however, you have not yet specified what the limitation on X is. This is too similar to the question Algorithm for finding maximum mutually coprime subset of a multiset of integers In that way, you could also use GCD instead of LCM to solve your problem since the question I gave is a special case of your question.

beginner1010
  • 168
  • 7