 # MOREFB - Editorial

Author: Amit Pandey
Tester: Mahbubul Hasan and Sunny Aggarwal
Editorialist: Balajiganapathi Senthilnathan
Russian Translator: Sergey Kulik
Mandarian Translator: Minako Kojima

Medium-Hard

### PREREQUISITES:

FFT, Divide and conquer

### PROBLEM:

Given a set S with N elements and a number K, find \sum_{s \subset S \land |s| = k} Fibo(sum(s))

### SHORT EXPLANATION

Since this is modulo a prime for which square root of 5 exists, we can calculate the n^th fibonacci number modulo P as: a(b^n - c^n) mod P for the correct values of a, b and c. Then finding the answer boils down to finding the coefficient of x^{(N - K)} in the equation (x + b^{a_1}) * (x + b^{a_2}) * .. * (x + b^{a_N}). The multiplication can be done using FFT.

### EXPLANATION:

Note that mod = 99991 is a prime number. There is a closed form for finding the n^{th} fibonacci number:

F_n = \frac{\sqrt{5}}{5}((\frac{1 + \sqrt{5}}{2})^{n} - (\frac{1 - \sqrt{5}}{2})^{n})

Now since we are taking everything modulo mod, we can calculate each of the term modulo mod.
So we will get an equation of the form:

F_n = a(b^n - c^n)

Where a, b and c are the corresponding values from the above equation modulo mod.

Now let us use this information to calculate the coefficient of b.

For example, if S = {1, 2} and K = 1, then the answer is F_1 + F_2 = a((b^1 + b^2) - (c^1 + c^2)). Let us call the part with b g(b). We will discuss how to calculate g(b), calculating for the c part is similar.

Let us see the multiplication of (x + b^1)(x + b^2). It comes out to be x^2 + (b^1 + b^2) x + b^3. Note that the coefficient of x is the answer we require. Also, if K were 2, then the answer would have been F_3 whose g(b) would be the coefficient of x^0. Seeing this pattern we can generalize the idea.

The g(b) for any K is the coefficient of x^{N - K} in the polynomial (x + b^{a_1}) * (x + b^{a_2}) * .. * (x + b^{a_N}).

So, how do we calculate this value? A naive implementation of the multiplication will TLE as it will take O(N^2). However we can use FFT (the tutorial is in Russian you may have to translate it first) or Karatsuba’s algorithm to speed up the multiplication.

O(N logN logN)

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

6 Likes

Please could any one explain this editorial more. I really want to learn this problem.

3 Likes

I just wanted to point out that for the given constraints a simpler algorithm for polynomial multiplication, namely [Karatuba], suffices (which has a runtime of O(N^{\log_2(3)})). This is cause the implementation of FFT is fairly involved and calls for maintaining complex values, whereas Karatsuba only requires integers and in essence is a simple divide-and-conquer algorithm.

Also for those curious, the closed form for F_n mod 99991 ends up being:

F_n = \frac{a^n - b^n}{a - b}, a = 55048, b = 44944.

This is because the characteristic polynomial for F_n, i.e. x^2 - x - 1, has integer roots mod 99991 (since \sqrt(5) = 10104 \in Z_{99991})
: https://en.wikipedia.org/wiki/Karatsuba_algorithm

3 Likes

The prime modulo 99991 is not suitable for use with NTT(Number Theoretic Transform), so we will have to resort to using FFT with complex doubles. To avoid rounding errors, after each polynomial multiplication, round the coefficients to the nearest integers. If using double still results in rounding errors, as it did in my case, replacing it with long double will eliminate the problem. This was my first time at solving an FFT problem and it feels nice to have learnt something new!

2 Likes

We can implement the same strategy via matrix multiplication can’t we ?

Would be interesting to know if it can be solved for 100pts with matrix multiplication.

I solved 2 subtasks(40pts) with a DP solution and matrix exponentiation, if you’re interested: http://www.codechef.com/viewsolution/7180786

Why can’t the editorial be more clear, for those who are very new to such topics, finds many difficulty in learning them as we cannot understand anything by seeing someone’s code. The main idea of editorials is for those who are willing to learn which is not sustained here(seems the editorial can be understandable by those who have already a idea about the topic.)

19 Likes

Interesting thing to know about Fibonacci numbers is that when we take mod with 99991 the Fibonacci cycle repeats itself after length of 33330 which can be verified here http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibmaths.html#section6.1

1 Like

Can anyone please explain the last part i.e. Also, if K were 2, then the answer would have been F3 whose g(b) would be the coefficient of x0

@bbackspace, can you explain your approach a little bit, specially that lucas number part.

I propose an alternate solution:

We can prove by induction that the matrix:

[1 1]
[1 0]


raised to the n-th power gives

[F_n+1 F_n  ]
[F_n   F_n-1]


Let’s call the matrix M, for short.

We should easily figure out that
F_(a+b) is the top-right (or bottom-left) element of M^a * M^b

Then how can we use this to out advantage?

Well, if we dive deeper into this statement, we can argue that F_sum(V), over any V, is the top-right cell of the matrix product(M^V[i]).

Also, from M^a + M^b we can translate easily to F_a + F_b (we just take the element I recalled);

Let’s summarize:

• M^a + M^b <-> F_a + F_b
• M^a * M^b <-> F_(a+b)

then SUM of F_(SUM of elements) can be translated to SUM of PRODUCT of corresponding matrices.

Let’s continue.

If we take all subsequences of cardinality K, then FIBOSUM(k) is the sum of all PRODUCT(M^v[i]), for each v subsequence of V of cardinality K. If we further analyze this, by the rule of Viete, it turns out to be the (N-K)th term of the polynom P(X) = PRODUCT(X + M^V[i]), for i = 1,n. Basically, we have to find out the (matrix) coefficients of the polynom P(X), which can be done with divide et impera and FFT or Karatsuba (gave me TLE).

Why would this be better? Well, it’s not. For this case at least. However, it does work in any case, no matter how the modulo is chosen.

4 Likes

I waited two days for this editorial hoping to find a good FFT explanation relevant to this question, and examples.

Why TL is so huge? Karatsuba version passed in 0.8 sec.

Hey, how did you calculate a and b? I don’t know what concept is used here. Please give me a link , if you have so I can read in detail.

Editorial is not clear. Please explain the FFT part.

I learned fft from a very well written article, which is very friendly and not so hectic:

After understanding this, you can try reading some more papers and more easily feel for it.
Google is our friend on this one!

@filmwalams you might have to read up on solving linear recurrences. Here’s a good link for the Fibonacci sequence specifically: http://gozips.uakron.edu/~crm23/fibonacci/fibonacci.htm. The idea is that the characteristic polynomial factors nicely mod 99991:

$(x - 55048)(x - 44944) = x^2 - 99992x + 2474077312 = x^2 - x - 1 (mod 99991)$.

Yes, it happens after Pisano period.

You may see the editorials of following problem for more details : https://www.hackerrank.com/challenges/emma-and-sum-of-products/editorial

1 Like

You may see the editorials of following problem for more details about FFT : https://www.hackerrank.com/challenges/emma-and-sum-of-products/editorial

1 Like
//