 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 ?

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

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*).

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*), 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*), 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.

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!

When for S=1,2, it was coefficient in (x+b^a1)(x+b^a2)…
then from where did the term (x-b^a1) come in generalization??? shouldn’t it be (x+b^a1) or you should have explained that too… please reply soon

Here is the DP approach that I used to solve this for 40 pts.

We notice that we need to evaluate sums of the form f(a + b) + f(b + c) + f(a + c). So we’ll go to Wolfram’s Fibonacci Numbers page to find a formula that might help us solve this problem. Scrolling down the page (and admiring the beauty of Fibonacci numbers), we stumble across this cute formula:

F_{m+n} = \frac{1}{2}(F_mL_n + L_mF_n)
where L_n is the n^{th} Lucas number (the Lucas series have the same recurrence as Fibonacci but different initial values).
But how do we deal with the L_n terms? Simple, another formula at the Lucas number page says,

L_{m+n} = \frac{1}{2}(5F_mF_n + L_mL_n)

The n^{th} Lucas number can be found in the same way as Fibonacci numbers in O(\log n) time with matrix exponentiation.
So we now have found a way to build the FIBOSUM(s) values as we consider more and more elements in S. Let’s define FS(n, k) as FIBOSUM(S[1..n]) for subsets of k cardinality. We also define LS(n, k) similarly with the Fibonacci function replaced by the Lucas function.

We will form the recurrence in the lines of \binom{n}{k} = \binom{n - 1}{k} + \binom{n - 1}{k - 1} which is formed by considering the two cases of A) not taking the n^{th} element and B) taking it.

The recurrence for FS(n, k) is a sum of two components, first being FS(n - 1, k) where we do not take the n^{th} element. The second is the sum,

\displaystyle\sum_{i=1}^{t} F_{S[n]+m_i} if FS(n - 1, k - 1) = \displaystyle\sum_{i=1}^{t} F_{m_i}

which is equivalent to,

\frac{1}{2}(F_{S[n]}(\displaystyle\sum_{i=1}^{t} L_{m_i})+L_{S[n]}(\displaystyle\sum_{i=1}^{t} F_{m_i})) = \frac{1}{2}(F_{S[n]}LS(n - 1, k - 1)+L_{S[n]}FS(n - 1, k - 1))

The final recurrence becomes,

FS(n, k) = FS(n - 1, k) + \frac{1}{2}(F_{S[n]}LS(n - 1, k - 1)+L_{S[n]}FS(n - 1, k - 1))

LS(n, k) = LS(n - 1, k) + \frac{1}{2}(5F_{S[n]}FS(n - 1, k - 1)+L_{S[n]}LS(n - 1, k - 1))

FS(1, 1) = F_{S[ 1 ]}

LS(1, 1) = L_{S[ 1 ]}

Following is the pseudocode,

FS = fib(a);
LS = luc(a);
LS = 2; //luc(0) = 2
for i = 2 to N
{
LS* = 2; //luc(0) = 2
for j = 1 to K
{
FS*[j] = (FS[i-1][j-1] * luc(a*) + LS[i-1][j-1] * fib(a*)) / 2 + FS[i-1][j];
LS*[j] = (5 * FS[i-1][j-1] * fib(a*) + LS[i-1][j-1] * luc(a*)) / 2 + LS[i-1][j];
}
}


Note that all operations in the pseudocode above are under mod 99991.

2 Likes

How do you get the values of a,b and c?

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

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

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.

@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)$.