For the first subtask, just find out all of the subsequences. In this case, the number of subsequences will be 2n. Then calculate the LCM and GCD of every subsequence. If (LCM - GCD) of any subsequence equals to k, then print "YES", otherwise print "NO". So, the complexity will be O(n * 2n).
For the second subtask, you have to ovserve some criteria of this problem. As, GCD is the factor of some numbers in the array, It can't be greater than the maximum value of array a. Let the maximum value is m. So, if answer exist, then we are sure that LCM will between 1 + k and m + k, as GCD is between 1 and m. Now, Check for every value v from 1 + k to m + k and try to find out the numbers from the array, which are devided by v - k and which can devide v. As, they all can be devided by v - k then, their GCD is greater than v - k. Also, they all can devide v, their LCM is less than v. But we are not sure yet. So, we can calculate their LCM and GCD. If, LCM equals to v and GCD equals to v - k, then print "YES". If, for any v, following conditions doesn't match, then print "NO".So, the complexity will be O(n * (m + k)).
For the last subtask, grab the main idea from second subtask. You can see that for every v, we are taking those numbers who can devide v ownself. In other words, taken values are factors of v. So, we can calculate the divisors of v and check whether the value exists in the array and divisable by GCD. If it is, then take the value. There's at most 100 divisor of a number less than 2 * 105. So, it is very efficient than the checking every value of the array for every v. Of course,you have to precalculate the divisors of numbers from 1 to 2 * 105 by the approach of sieve. Then once you can detect the factors of v which are divisable by v - k in lower complexity, you know what should be checked next. In this approach, if the summation of number of divisors from (1 + k) to (m + k) equals to x then the complexity is O(nlogn + x), where nlogn is complexity of sieve.

Statistics

50% Solution Ratio
AST_TheCoderEarliest, Aug '20
Kuddus.6068Fastest, 0.0s
serotoninLightest, 131 kB
serotoninShortest, 792B
Toph uses cookies. By continuing you agree to our Cookie Policy.