PROBLEM LINK:Author: Vadym Prokopec DIFFICULTY:medium PREREQUISITES:sqrt decomposition, log PROBLEM:You are given an array $a$ of size $n$. You have to answer following queries and update.
QUICK EXPLANATION:EXPLANATION:Finding first digit and modulo value of product of some numbers As we know that sum of these elements would be quite smaller, so can we somehow represent the product of some numbers in terms of sum. We can take log of the product. Let $b_1, b_2, \dots, b_n$ be $n$ positive numbers. Let $prod = b_1 \cdot b_2 \cdot \dots \cdot b_n$. We get $log(prod) = log(b_1) + log(b_2) + .. \dots + log(b_n)$. So, given log of a number, can we find first digit of the number? Solution based on number of divisors of a number Based on these observations, we can design a solution as follows. For the given array, before processing all the queries, we can precalculate values of $prod_r and logSum_r$ for each $r$ from $1$ to $n$. Note that the time taken will be $\mathcal{O}(n log n)$. Now let us see how to process the updates and query. If we receive an update of setting $a_r$ to $v$, we will iterate over all the divisors $d$ of $r$ and update the corresponding $prod_d$ and $logSum_d$. Now if we receive a query of finding answer for given a $r$, we can directly answer it by using values of $prod_r$ and $logSum_r$. Now, there is a small catch with the above solution. If we want to make an update at $r = 0$, then it will affect the answers of all the $prod$ and $logSum$ values. So if we directly update these values, we can take $O(n)$ in the worst case. To handle this case, we can separately maintain the value of first element, (i.e. $a_0$). So, whenever we receive an update of setting $a_0$ to some value, instead of changing all the $prod, logSum$ values, we will just change the value of first element we maintained. Note that we will have to change our update accordingly. For each query, we will have to additionally output the modified values of $prod$ and $logSum$ values respectively. Note that this extra catch was because of the reason that $r = 1$ to $n$, all are divisors of 0. But for other numbers, this is not the case. Maximum number of divisors of a number $1 \leq n \leq 10^5$ are 128. Hence, time complexity of this solution will be $\mathcal{O}(128 * n)$. Please note that in order to pass your solution with the given time limit, you have to implement this carefully, e.g. while update, don't calculate inverse of $a_r$ in the loop. Time Complexity:$\mathcal{O}(n \, 128)$ AUTHOR'S AND TESTER'S SOLUTIONS:
This question is marked "community wiki".
asked 12 Jun '16, 12:02

The same code: https://www.codechef.com/viewsolution/10416244 https://www.codechef.com/viewsolution/10416229 One in C++ 14 gets a WA and TLE(by 0.1 sec ) and the other(in C++ 4.3.2) gets AC,these things should not happen with a programmer :( Anyways what can be the reason ? answered 15 Jun '16, 15:57
not sure about the TLE, but I had the same problem with WA...one testacse was WA for me too. It is probably because of precision handling.
(15 Jun '16, 16:03)
So,precision handling technique changes with language version ?
(15 Jun '16, 16:10)
probably.. updated versions might use updated pow functions right
(15 Jun '16, 17:56)

Answer is hidden as author is suspended. Click here to view.
answered 20 Jun '16, 02:50

This question was all about tackling the precision, once the algo was clear! Nice one answered 15 Jun '16, 15:26

The prerequisites mention about Square Root Decomposition, I did not understand how we are implementing Square Root Decomposition here?
link
This answer is marked "community wiki".
answered 15 Jun '16, 15:47
you are decomposing a number r into two products r = a * b. Where a <= b. Hence you need to search until square root of r only to find a and b. Hence, the name square root decomposition.
(15 Jun '16, 15:56)
i don't think that is what square root decomposition means...
(15 Jun '16, 16:00)
Umm.. I am sorry, I did not quite get what you mean to say. Can you please give me some link to solution which is the implementation of what you are trying to convey?
(15 Jun '16, 16:04)
(15 Jun '16, 16:14)
@adkroxx Thanks, I will check that out for sure!
(15 Jun '16, 16:50)

Exactly same logic as described in the editorial, still it was WA. If any body could figure out. link:https://www.codechef.com/viewsolution/10494476 answered 15 Jun '16, 15:55
You are missing out for test where you query with R = 1 as in: 3 2 8 5 1 2 1 Setting a[n+1]=1 fixes that though
(15 Jun '16, 16:50)

Can anyone tell me why am i getting WA? Here is my code link https://www.codechef.com/viewsolution/10512326 answered 15 Jun '16, 18:10

We can do this question using sqrt decomposition also.Divide the array into sqrt n blocks and for each block maintain an array of sqrt n size in which ith element contains information required about the product of the elements in this block due to r=i.For r>=sqrt n just calculate the answer manually(it takes O(root n))and for r< sqrt n traverse the blocks to get the information.We can update in sqrt n time by checking divisors of that particular index and update in its block. So total complexity is O(n*root n). answered 15 Jun '16, 20:00

I'm getting WA for subtask 2. Can somebody explain why? (I know the approach is not ideal, still) https://www.codechef.com/viewsolution/10453146 answered 15 Jun '16, 20:00

Can anyone help me to optimize my code?? answered 15 Jun '16, 21:00

If N=50,say. and we update the 12th element. Then we have to change the value of product for r=1,2,3,4,6,12. How will we do that? Do we have to iterate through each value of r? answered 15 Jun '16, 21:43

I got 95 points in the question since my solution did not pass the first subtask. Can anyone explain why? https://www.codechef.com/viewsolution/10511094 answered 15 Jun '16, 23:16

Implemented as in editorial still getting WA. Can someone figure it why? https://www.codechef.com/viewsolution/10515781 answered 16 Jun '16, 10:36

I dont understand why this additional EPS was needed in the power of 10. Please answer... answered 16 Jun '16, 14:42

This problem could be solved without the prior knowledge of squareroot decomposition. https://www.codechef.com/viewsolution/10448317 answered 17 Jun '16, 12:17

I did this problem without using sqrtdecomposition...I made an array which stores the new value of friendliness at pth index during update query...I never updated the array which stores enjoyment for different values of R. During query 2, I checked whether there is an any update of friendliness int the multiples of r. In this way i updated my Answer... See my solution here for more clarity.. https://www.codechef.com/viewsolution/10384547 answered 18 Jun '16, 09:27

Editorialist solution giving TLE for last 3 testcases . Why ? answered 18 Jun '16, 11:46

can anyone explain me how to precompute the product array in O(nlogn) ?? I am getting it in O(n*sqrt(n)) . answered 18 Jun '16, 17:49

In the editorialist code, what is the purpose of computing modulo inverse and multiplying with existing product of r along with y?
answered 19 Jun '16, 22:44

Can anyone explain me the setter's code: for (int i=1; i<=min(pos1, sq); i++){ if ((pos1)%i==0){ dig[i]=log10(a[pos]+0.0); dig[i]+=log10(val+0.0); remain[i]=inv; remain[i]%=inf; remain[i]=val; remain[i]%=inf; Should'nt this be just min(pos,sq) as the setter had done 1 based indexing in his code? answered 19 Jul '16, 23:43

i am using the same algo as given in solution still i am getting only 95 points. plz help. solution link https://www.codechef.com/viewsolution/10910270 answered 26 Jul '16, 13:53
