PROBLEM LINK:Author: Sunny Aggarwal DIFFICULTY:MEDIUM PREREQUISITES:Fibonacci Numbers, Segment Trees 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.
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 submultiset W and Fibonacci(Z)=the Zth 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(R)}). EXPLANATION:We can use the following property of Fibonacci numbers: Fibonacci(A+B)=Fibonacci(A)xFibonacci(B+1)+Fibonacci(A1)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:
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:
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 nonleaf 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:
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.
This question is marked "community wiki".
asked 02 Apr '16, 12:45

Answer is hidden as author is suspended. Click here to view.
answered 11 Apr '16, 17:38

Let f(x) be the xth fibonacci number. Let M = [ [1,1], [1,0]]. Consider a 1element 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 2element 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 3element 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 3D array, an array of 2D Matrices). Here is the code. Took much less time than the editorial code and much much less heap memory. answered 11 Apr '16, 19:35
Thank you Sir, for such a neat code. Wish everyone would write code of this clarity. (Y)
(17 Mar '17, 21:28)

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)? answered 11 Apr '16, 22:05
To partially answer your question: the interval multisets are not generated, it's not necessary. CombineIntervalInfo will calculate the correct answer, but I don't understand how. Specifically I don't understand how "F(A+B) = F(A).F(B+1) + F(A1).F(B)" can be applied to Sum(F(Sum(s))) for s in multisets. Here is an example where I computed the combination of [3, 4] and [1,2] and then [3, 4, 1, 2] separately, just to confirm it works: https://i.imgur.com/UwakZCg.jpg
(12 Apr '16, 03:44)

Answer is hidden as author is suspended. Click here to view.
answered 12 Apr '16, 20:32

Where is the specific property described? What is its proof? answered 11 Apr '16, 18:58

I used the golden ratio and the polynomial multiplication approach https://www.codechef.com/viewsolution/9838337 answered 11 Apr '16, 19:55

A very nice problem, also excited for other approaches too. answered 11 Apr '16, 17:37

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. answered 11 Apr '16, 20:15

good explanation can u describe how to done it with segment trees @atulshanbhag answered 12 Apr '16, 02:27

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'? answered 12 Apr '16, 18:42

Here is a decent intro to segment trees: http://www.geeksforgeeks.org/segmenttreeset1sumofgivenrange/ Here is a reference I used for Fibonacci generation: http://fedelebron.com/fastmodularfibonacci 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). answered 12 Apr '16, 21:52

Can some one explain how CombineIntervalInfo() generates multiset as well as the summation. Thanx in advance. answered 12 Apr '16, 22:31

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 + (allsubsetinproductform)... 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.. answered 13 Apr '16, 00:58

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[left] + tree[right] + tree[left].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! answered 13 Apr '16, 11:28

let array= [n1,n2,n3...nn]; A=(1+sqrt(5))/2 and B=(1sqrt(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 (X5Y5)/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?? answered 13 Apr '16, 16:32

could someone pls explain the reasoning/intuition behind the combineIntervalInfo() routine. answered 14 Apr '16, 03:14

Can this be solved using the golden ratio?
Can anyone explain how they are finding the sum of individual subsets in a given range?
Excellent problem ,Poor editorial , with no explanation for Combineintervalinfo function..!! , it feels like like editorial wants us to digest us the solution without any explanation..!!