# FIBQ - editorial

Author: Sunny Aggarwal
Tester: Sergey Kulik
Editorialist: Mugurel Ionut Andreica

MEDIUM

### PROBLEM:

Given an array A consisting of N elements, Chef asked you to process following two types of queries on this array accurately and efficiently.

• C X Y: Change the value of X-th element of array to Y i.e A[X] = Y.
• Q L R: Compute the function F over the subarray defined by the elements of array A in the range L to R, both inclusive.

The function F(S) is defined as F(S)=(\sum_{W\subseteq S}{Fibonacci(Sum(W))})\ modulo\ (10^9+7), where Sum(W) denotes the sum of all the elements in the sub-multiset W and Fibonacci(Z)=the Z-th Fibonacci number.

The function F applied over a subarray [L,R] of the array A is defined as F(S) where S is the multiset consisting of all the elements from the range [L,R] from the array A (i.e. S={A(L), A(L+1), …, A®}).

### EXPLANATION:

We can use the following property of Fibonacci numbers: Fibonacci(A+B)=Fibonacci(A)xFibonacci(B+1)+Fibonacci(A-1)xFibonacci(B).

With this property we can use a segment tree in order to efficiently process updates and queries. For each interval corresponding to a node of the segment tree we will store 3 values:

• sfib = sum of the values Fibonacci(Sum(W)) (modulo 10^9+7), for all the sub-multisets W
• sfibm1 = sum of the values Fibonacci(Sum(W) - 1) (modulo 10^9+7), for all the sub-multisets W
• sfibp1 = sum of the values Fibonacci(Sum(W) + 1) (modulo 10^9+7), for all the sub-multisets W

For the leaves of the segment tree we will initialize these values directly. For a leaf node L corresponding to a position i of the array A we will simply set:

• L.sfib = Fibonacci(A[i])
• L.sfibm1 = Fibonacci(A[i] - 1)
• L.sfibp1 = Fibonacci(A[i] + 1)

Since A[i] can be pretty large, we need an efficient method of computing Fibonacci(A[i]) (and, in general, for computing Fibonacci(Y) for Y as large as 10^9). There are multiple methods which can be used for computing these values in O(log(Y)) time. Below you can see one such function which uses memoization:

Fibonacci(y) {
if (y <= 0) return 0;
if (y <= 2) return 1;
if (y in fibonacci_cache) {
return fibonacci_cache[y];
}
int f, b, a;
b = y / 2; // integer division
a = y - b;
f = (Fibonacci(a) * Fibonacci(b + 1) + Fibonacci(a - 1) * Fibonacci(b)) % MOD;
fibonacci_cache[y] = f;
return f;
}


For a non-leaf node node we can use the following function for computing its information based on the information available in its left and right children:

CombineIntervalInfo(left, right, node) {
node.sfib = (left.sfib + right.sfib + left.sfib * right.sfibp1 + left.sfibm1 * right.sfib) % MOD;
node.sfibm1 = (left.sfibm1 + right.sfibm1 + left.sfib * right.sfib + left.sfibm1 * right.sfibm1) % MOD;
node.sfibp1 = (left.sfibp1 + right.sfibp1 + left.sfibp1 * right.sfibp1 + left.sfib * right.sfib) % MOD;
}


Processing an update operation X Y can be done as follows in O(log(N)) time. For the leaf node L corresponding to the position X from the array we reinitialize its values:

• L.sfib = Fibonacci(Y)
• L.sfibm1 = Fibonacci(Y - 1)
• L.sfibp1 = Fibonacci(Y + 1)

Then, for every ancestor of the node L, starting from its parent and going up to the root, we recompute its information using the CombineIntervalInfo function.

Handling a query L R can be done as follows, also in O(log(N)) time. We find the O(log(N)) nodes of the segment tree such that the disjoint union of their intervals is equal to the interval [L,R]. Let these nodes be node(1), …, node(K). If K=1 then the answer is node(K).sfib. Otherwise, we will we compute a tuple ANS containing the same fields sfib, sfibm1 and sfibp1 as the information stored for each segment tree node. We initialize ANS by combining the information from node(1) and node(2). Then, for every 3\leq i\leq K we update ANS by combining its information with that of node(i). At the end, the answer is ANS.sfib.

### AUTHOR’S, TESTER’S AND EDITORIALIST’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here.
Editorialist’s solution can be found here.

6 Likes

A very nice problem, also excited for other approaches too.

Alternate approach :

Build a segment tree where we store a 2x2 transition matrix in each node . ( Transition matrix is [1,1],[1,0] and fib(n) = T^n ) .
Let matrix for parent and its two child nodes be X,A,B respectively . We can easily see that X = A+B+A*B .
To get the final answer just multiply the matrix by the base case matrix ([0,1]) .


(https://www.codechef.com/viewsolution/9838458)
9 Likes

I used Matrix exponentiation in my Solution
I have added my explanation in it.

4 Likes

Where is the specific property described? What is its proof?

2 Likes

Let f(x) be the xth fibonacci number. Let M = [ [1,1], [1,0]].

Consider a 1-element range. The answer to this query is f(x) where x is the element, which is given by the [1][0] element of M^x. Let us call this A for now.

Consider now a 2-element range (x,y). Here the result must be f(x) + f(y) + f(x+y). Which is the [1][0] element of A + M^y + A.M^y where A = M^x and we use M^(x+y) = M^x.M^y . Now let us call this matrix as A.

Similarly for the 3-element range (x,y,z), we get the result as A + M^z + A.M^z and we can clearly see the recurrence kind of relation.

You can get 20 points for running a loop from l to r. But for 100 points you need to build a segment tree with each node containing a Matrix (yes, the segtree will be a 3-D array, an array of 2-D Matrices).

Here is the code. Took much less time than the editorial code and much much less heap memory.

4 Likes

@ash_code can be solved using golden ratio … check this


[1]
check the last comments of [this][2].

[1]: https://www.codechef.com/viewsolution/9837297
[2]: http://codeforces.com/blog/entry/18292

I used the golden ratio and the polynomial multiplication approach https://www.codechef.com/viewsolution/9838337

2 Likes

There is one more optimization which should work for computing fibonacci. For modulus 10^9+7, pisano number was 2*10^9+18. So if number greater than this, we first take modulus with 2 * 10^9 + 18 and then compute Fibonacci.

It would be great if you can explain me how the multisets are generated of each interval in the segment tree from Fibo(A+B)?

4 Likes

The problem could be made more challenging by introducing range update

good explanation can u describe how to done it with segment trees @atulshanbhag

I also I don’t find the relationship between Fibonacci(n+m) and F(A U {x}) intuitive at all. Does anyone have a justification of this, other than ‘clearly’ or ‘notice’ or ‘easily see’?

Please mention the topics or Algorithms along with the links that are required as a prerequisite for this problem…
Can anyone explain this editorial in an easy language…that is understandable to a newbiee…

2 Likes

Here is a decent intro to segment trees: http://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/

Here is a reference I used for Fibonacci generation: http://fedelebron.com/fast-modular-fibonacci

Hope that helps, though I think there is a little bit of mystery that noone has explained linked to regarding this particular function (not prereqs).

Can some one explain how CombineIntervalInfo() generates multiset as well as the summation.

whole time i was figuring out how can we get all subset of the set… as to me that was the main problem… but after contest ended i came to know of -> (1+a)(1+b)(1+c)…(1+n)=1 + (all__subset__in__product__form)… eg. (1+a)(1+b)(1+c)=1+a+b+c+ab+bc+ca+abc…

as u can see now we just can now easily use segment tree…

@rohitangira

Consider a segment tree “tree” where all nodes hold some matrix. Like I said in my previous comment, let us store in the leaf node, M^x where x is the corresponding element and M = [[1,1],[1,0]].

Now for a node which is not a leaf, we simply need tree + tree[right] + tree.tree[right], where left and right are the left and right child respectively. We keep doing this recursively and build the segment tree.

Now for updating, we will update only one element and not a range. So we update a leaf node, and make sure to update all the ancestors of this leaf node. In other words, we are rebuilding the segment tree for every update. Also note, after using the array element initially to build the segment tree, the array is not needed anywhere else in the code. So actually when updating the array, we can only update the segment tree because all further computations will be held by the segment tree.

For query, I followed the basic segment tree propagation method (not lazy) to create a dummy matrix which will hold the data for the range query. This matrix will contain the final result. We only output the [1][0] element of this matrix. For further details, please read my code here and my earlier post. Cheers!

let array= [n1,n2,n3…nn];
A=(1+sqrt(5))/2 and B=(1-sqrt(5))/2
thus from a derivation i found Sum of all fibonacci multiset is=> 1/sqrt(5)*[(1+A^n1)(1+A^n2)…(1+A^nn)-(1+B^n1)(1+B^n2)…(1+B^nn)]
with this fetch takes O(1) time.
because:suppose x1=(1+A^n1) and y1=(1+A^n1) similarly x2=(1+A^n1)(1+A^n2) and y2=(1+B^n1)(1+B^n2).
Thus if we make an array of x and y such as
X=[x1,x1x2,x1x2x3,x1x2x3x4, to xn] and
Y=[y1,y1y2,y1y2y3,y1y2y3y4, to yn]

thus for Q 1 5 we take (X5-Y5)/sqrt(5); thus happens in O(1) time. And to build this array we take O(n) time… I want to know why this approach is not suitable here??? I have no clue whatsoever!!! I was getting wrong ans for this. I used long double for storing all double values!! can some one help??

could someone pls explain the reasoning/intuition behind the combineIntervalInfo() routine.