Codeforces Round 641 Div 2
Problem A: Orac and Factors
Given two numbers N and K, apply f(N)
to N exactly K times, where f(n)
is the smallest positive divisor of n except 1.
Hint 1
- After applying f(N) for the first time, what property do you observe on the result?
Hint 2
- After applying f once, the resulting number will be even, why?
- If so, for the remaining (k - 1) times, 2 will be added to the result.
Time Complexity
Code
Problem B: Orac and Models
Problem Statement:
- You are given an array arr containing n elements. Find the maximum beautiful subsequence length.
- A subsequence of arr is beautiful, if for every adjacent elements, with indices and in the array, where , is divisible by and .
Hint 1
- What if you already knew the maximum subsequence length for all subarrays starting from 0 and ending till index (i-1)?
- How will you find the answer for subarray starting from 0 and ending at index (i) ?
Hint 2
- Well, what can be the previous index where you may pick an element in the subsequence? It must be a factor of i!
- So, for all factors of i, say j, which can be computed in sqrt(n) time, see if arr[j] < arr[i] holds, then update answer for arr[0...i] by maximum of ans[i], ans[j] + 1.
- See code for details.
Time Complexity
Code
Problem C: Orac and LCM
Problem Statement:
- You are given an array arr containing n elements.
- Construct a multiset containing the lcm of all pairs of elements, say M.
- Find the gcd of all the elements in M.
- Note the constraints. So, solution won’t work.
Constraints:
Hints:
- Consider the prime factorization of two numbers
a
andb
say:- For simplicity, considering
k
as the maximum no. of primes that we can get in the problem. - So, gcd(a, b) =
- And lcm(a, b) =
- For simplicity, considering
- So, when we take lcm of two numbers, we get the maximum of the powers of the primes for each prime and multiply them.
- We need to get gcd of lcm of all pairs of the numbers.
- So, let’s just focus on one prime number, say 2, that might occur in factorization of the numbers.
- Let’s assume there are 3 numbers and powers of 2 in their factorizations are: 1, 2, 3.
- So, when you will consider all 3 choose 2, i.e. 3 pairs, the powers of 2 you will be getting in the lcms will be: max(1, 2), max(1, 3), max(2, 3) = 2, 3, 3.
- So, in the gcd, the power of 2 will be 2, since we need to take the minimum one in gcd.
- Can you try building up some observation for more numbers and given the power of the prime?
- Will the power be the second minimum one? Always? Or in some particular case? When the prime is present in factorization of all
N
numbers.
- Consider a case when a particular prime is not available in atleast 2 numbers, so multiplying those 2 numbers you get no power of that prime. Thus, it won’t come in gcd also.
- Consider one more example:
- There are 5 numbers and powers of 2 are: 0, 3, 4, 4, 5.
- Here, also when computing lcm of pairs, the min power you will get will be 3, i.e. second minimum.
- Now, also thinking about complexity of solution, you can’t see for every prime if it divides every number, That will cost .
- So, we will update the minimum powers as we factorize the number, using the shortest prime factor approach (see some articles on it) to do prime factorization in .
- Please see the code for more details.