Editorial for Bubble Cup V9 E Festival organization
Submitting an official solution before solving the problem yourself is a bannable offence.
It is easy to prove that the answer for the query is , where 
 are Fibonacci numbers. Let's notice, that 
 is a polynomial for 
 and can be expressed in the form 
. Knowing this, we can express the answer in a different way:
Thus we reduced our problem to computing the sum of  powers of Fibonacci numbers. To do so we will refer to Binet's formula:
The inner sum is almost always a geometric progression with  and 
, except for the cases when 
, but we may avoid any special cases by computing it in a way similar to binary exponentiation.
Indeed, in order to compute , we may start with computing 
 and 
. Two sums with 
 and 
 elements can be merged together in the following way:
Thus we can compute the sum of  powers only with additions and multiplications. The only difficulty is that there is 
 present in these formulas (in 
 and 
) and there is no such number modulo 
. The solution to this is simple: let's never specify an exact value for it. This way we will always work with numbers of the form 
. The addition and multiplication of these numbers is fairly easy:
These are the only operations that we require. The sum of Fibonacci numbers (which are integers) is also an integer, so the final result will never contain any . Thus we have solved this problem.
Comments