Prime Reduction#13085

Prime Reduction (Editorial)

Tutorial

Let v_p(x) denote the exponent of the prime in the prime factorization of .

For example, since 12 = 2^2 \cdot 3, we have v_2(12)=2 and v_3(12)=1.

We will first solve the problem for one fixed prime , and then combine the answers for all primes.

Observation 1: Solving the problem for one prime

Fix a prime and define

e_i = v_p(a_i).

Also define e_0=0.

Consider only operations in which the chosen prime is .

If we divide some a_i by , then e_i decreases by exactly .

Therefore, an operation with prime selects a contiguous subsegment whose exponents are all positive and decreases every exponent in this subsegment by .

So, for a fixed prime , the problem becomes the following:

We have a non-negative array e_1,e_2,\ldots,e_n. In one operation, we may choose a contiguous subsegment consisting only of positive values and subtract from every value in it. We want to make the whole array equal to zero.

The minimum number of operations is

\sum_{i=1}^{n} \max(0,e_i-e_{i-1}).

Let us prove this formula.

Consider some level h \ge 1.

Look at all indices for which

e_i \ge h.

These indices form several maximal contiguous segments.

Each such segment needs exactly one operation at level .

A new segment at level starts at position exactly when

e_{i-1} < h \le e_i.

For a fixed position , the number of values of satisfying this inequality is exactly

\max(0,e_i-e_{i-1}).

Therefore, after summing over all positions, the minimum number of operations using the prime is

\sum_{i=1}^{n} \max \left( 0, v_p(a_i)-v_p(a_{i-1}) \right).

Observation 2: Combining all primes

Now consider the contribution between positions i-1 and .

For one prime , this contribution is

\max \left( 0, v_p(a_i)-v_p(a_{i-1}) \right).

For any non-negative integers and ,

\max(0,A-B)=A-\min(A,B).

Therefore,

\max \left( 0, v_p(a_i)-v_p(a_{i-1}) \right) = v_p(a_i) - \min \left( v_p(a_i),v_p(a_{i-1}) \right).

Now sum this expression over all primes .

Let denote the total number of prime factors of , counting multiplicity.

For example,

12=2^2\cdot3,

so

\Omega(12)=3.

By definition,

\sum_p v_p(x)=\Omega(x).

Also, for every prime ,

v_p(\gcd(x,y)) = \min(v_p(x),v_p(y)).

Thus,

\sum_p \min \left( v_p(a_i),v_p(a_{i-1}) \right) = \Omega \left( \gcd(a_i,a_{i-1}) \right).

Hence the contribution of position is

C_i = \Omega(a_i) - \Omega \left( \gcd(a_{i-1},a_i) \right).

We define a_0=1.

Therefore, the answer for the whole array is

\mathrm{ans} = \sum_{i=1}^{n} \left( \Omega(a_i) - \Omega \left( \gcd(a_{i-1},a_i) \right) \right).

Notice that operations corresponding to different primes are independent: an operation using prime changes only the exponent of and does not change the exponent of any other prime.

Therefore, summing the minimum cost for every prime gives the minimum total number of operations.

Observation 3: Handling an update

Define

C(x,y) = \Omega(y)-\Omega(\gcd(x,y)).

Then

\mathrm{ans} = \sum_{i=1}^{n} C(a_{i-1},a_i).

Suppose we update position .

Only two contributions can contain a_{idx}:

C(a_{idx-1},a_{idx})

and, if idx<n,

C(a_{idx},a_{idx+1}).

Therefore, before changing a_{idx}, we subtract these contributions from the answer.

Then we set

a_{idx}=x

and add the new values of the same contributions.

So each update requires only a constant number of GCD computations.

Precomputation

All values are at most 10^7.

Let

M=10^7.

We precompute the smallest prime factor of every number from to using a linear sieve.

At the same time, we can compute .

We have

\Omega(1)=0.

For every x>1, if is its smallest prime factor, then

\Omega(x) = \Omega \left( \frac{x}{\operatorname{spf}(x)} \right) +1.

Thus, after the precomputation, can be obtained in constant time.

Complexity

The sieve takes

time and

memory.

Computing the initial answer requires GCD computations.

Each query requires only a constant number of GCD computations, and each GCD takes

\mathcal{O}(\log M)

time.

Therefore, the total time complexity is

\mathcal{O} \left( M+(n+q)\log M \right),

where M=10^7.

The memory complexity is

\mathcal{O}(M+n).