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
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
Let us prove this formula.
Consider some level h \ge 1.
Look at all indices for which
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
For a fixed position , the number of values of satisfying this inequality is exactly
Therefore, after summing over all positions, the minimum number of operations using the prime is
Observation 2: Combining all primes
Now consider the contribution between positions i-1 and .
For one prime , this contribution is
For any non-negative integers and ,
Therefore,
Now sum this expression over all primes .
Let denote the total number of prime factors of , counting multiplicity.
For example,
so
By definition,
Also, for every prime ,
Thus,
Hence the contribution of position is
We define a_0=1.
Therefore, the answer for the whole array is
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
Then
Suppose we update position .
Only two contributions can contain a_{idx}:
and, if idx<n,
Therefore, before changing a_{idx}, we subtract these contributions from the answer.
Then we set
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
We precompute the smallest prime factor of every number from to using a linear sieve.
At the same time, we can compute .
We have
For every x>1, if is its smallest prime factor, then
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
time.
Therefore, the total time complexity is
where M=10^7.
The memory complexity is