Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

CHGCDG - Editorial

$
0
0

PROBLEM LINK:

Div1
Div2
Practice

Setter-Kirill Gulin
Tester-Ivan Safonov
Editorialist-Abhishek Pandey

DIFFICULTY:

EASY

PRE-REQUISITES:

Sieve of Erastothenes, Binary Search

PROBLEM:

Given an array $A$ of $N$ integers, we have to maximize the GCD of all elements of the array. To do that, you can select any array element $A_i$ which is divisible by any number ${d}^{2}$, divide it by ${d}^{2}$ and multiply any other array element by $d$.

QUICK EXPLANATION:

Key to AC- Realize that manipulation of powers of one prime is independent of powers of other primes. Perceiving that this question is more about manipulating prime and their powers rather than some complex mathematics theorem was important.

Make a list of primes, and smallest prime divisors of all numbers in range using sieve. Now, for each prime $p_i$, keep a note of how many array elements are divisible by it, and what is the power of $p_i$ in their prime factorization form. Now whats left is finding what power of this prime can we maximum achieve in final answer using above operations - we can use BSH (Binary Search Heuristics) for this. Repeat for all primes and get an AC :).

EXPLANATION:

This editorial is divided into $2$ sections. The first section will highlight on the pre-processing required, and the second section will feature how to calculate answers using Binary Search Heuristics (BSH).

Note that, I assume you all realize that, using operation of same number (i.e. dividing the same element by ${d}^{2}$ and increasing it by $d$) is useless. It will just reduce the prime power of that prime, which we might want to maximize for GCD.

1. Pre-processing-

It seems kind of obvious that we might need factorization of elements. Also, usually such problems do involve manipulation of prime numbers. Hence the first step is to use sieve of erastothenes to pre-calculate-

  • Prime numbers in range $[1,{10}^{6}]$
  • The smallest prime factor of a given number. This ensures $O(LogN)$ factorization of elements.

The observation to make is that manipulation of exponents of prime factors of $A_i$ are independent. We can choose a prime $d$ every time for our operation so that only the chosen prime gets affected. If this doesnt ring a bell, you can refer to tab below.

View Content

This is important as it helps in realizing that, increasing GCD prime by prime is the way to go. It gives an intuition that - "Ok, initialize GCD=1 for now. Lets pick a prime. Let me see what is the maximum possible power of it I can get for GCD of array using above operations. Multiply answer by it. Lets repeat this until we get answer."

What is the next thing we need if we are to do such manipulations? We'd need the prime numbers and their exponents for all elements of array. I specifically liked the tester's method of storing it (to use it later) and I think it can use some credit :) .Its in tab below for reference.

View Content

2. Binary Search Heuristics-

I hope that the intuition so far, especially that of manipulating prime by prime, is clear to you.

Now, for each prime, we will Binary Search on the highest possible power achievable (of that prime) in the final GCD. It is actually quite simple.

Lets say we have to check if a power $k$ is achievable for some prime $p$. How do we do it? Recall that using our operation, we can reduce the power of $p$ in some $A_i$ by $2$ and increase power in another $A_i$ by $1$. In a way, its like "Sacrifice $2$ powers of some $A_i$ for $1$ power in another element." Since we aim to calculate Gcd, our aim is to maximize the minimum power $p$.

Now, we have to check if we can make exponent of $p$ equal to $k$ in GCD. Hence, it makes sense to calculate how much "excess exponent" of $p$ can we take from "rich $A_i$", and how much exponent do we need to "poor $A_i$" to make exponent of $p$ equal to $k$ in GCD.

It can be elegantly done if you have pre-processed the exponents and primes like tester did. The tester used a vector idx[100001] where $idx[p]$ had "Exponents of $p$ which are $>0$ among all elements of array." Hence, he can simply loop over $idx[p]$ and calculate the excess and deficit exponents. (Do not forget that we are currently checking if we can achieve an exponent of $k$ in GCD).

Now, things are quite simple. If we have gained sufficient exponent from "Rich $A_i$" to give to "Poor $A_i$", then a power of at least $k$ is achievable in the final GCD. We check for the highest power achievable using binary search (proof in tab below).

View Content

The code to implement this is given below :)

View Content

SOLUTION:

The solutions are also pasted in tab below for you guys to see. Please refer to them in case links dont work. @admin may need time to link solutions up :)

Setter

View Content

Tester

View Content

$Time$ $Complexity=$ $O((K+N)logN)$ where $K$ is number of primes in given constraints of $A_i$

CHEF VIJJU'S CORNER :D

1. Is this problem solvable if we raise constraint of $A_i$ to $1 \le A_i \le {10}^{9}$?

2. Usually, many problems using GCD need knowledge of Mobius Function, Euler Totient functions etc. There is a very good blog describing them here

3.Linear Sieve deserves a mention here. :)

4. Prove that you can get an AC by this approach-

View Content

For above approach, solve the following-

  • This approach considers only first $100$ prime number. Validate and comment on its correctness. "Proof by AC" not accepted.
  • Can this approach be extended to $A_i \le {10}^{9}$. Why/Why not?
  • What is the lower bound on number of primes to consider for making it useful for $A_i <{10}^{9}$ and $A_i <{10}^{18}$?

Credits for this question to - @codebreaker123


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>