Editorial for Bubble Cup V8 A Fibonotci
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's first solve a simpler problem – when the sequence  is cyclic, i.e. when 
, for 
.
This simpler version is similar to Fibonacci sequence. Actually, for  and 
, it is the Fibonacci sequence. To find the 
 number of these kinds of recursive sequences fast we should first write them in their matrix form. Matrix form of this sequence is:
Expanding this, we can see that
How do we calculate this efficiently?
For relatively small , and we will take 
 for this case, we can do this just by multiplying all the matrices. For large 
 
, we will take advantage of the fact that 
 is cyclic. Since 
 is cyclic with cycle of length 
, we know that 
, for 
 (note that 
). Let's define this product of matrices as 
.
Now, we can write a formula for  that can be calculated quickly:
We can calculate  in 
 steps using exponentiation by squaring, and then we can just multiply everything in the expression to get 
 quickly.
Let's get back to the full problem, when  is almost cyclic. In this case, we cannot just use 
 in the formula above, because some matrices in 
 may not respect the cyclic property. Instead of 
, we will have something like
where  denotes the product of matrices of the cycle, with some matrices being different than the matrices of the original cycle. Also, 
 since each of the 
 values of 
 are different than values of the original cycle appears in exactly two matrices, so at most 
 of cycles are affected.
We can still calculate each  quickly, using exponentiation by squaring. Since there are at most 
 of those, total complexity of this would be 
.
Now we only need to calculate each . Naive way would be to just multiply all matrices of 
. Since the number of matrices is 
, this would be 
 worst case, which is too slow. To quickly calculate 
, we will initially create a segment tree of matrices, with matrices of original cycle in the leaves. Internal nodes of the tree will represent the product of their children. This means that the root will represent the product of all matrices in the cycle. To calculate 
, we will just update our segment tree with those values that are different than the original values of the cycle. We will do this by updating the corresponding leaves of the tree, moving up to the root and updating the products in the internal nodes. After we're done updating the tree with all of the matrices that are different than matrices of the original cycle, we will just use the product in the root of the tree. Finally, we will update the tree back with matrices of the original cycle in order to reuse the segment tree for 
.
Since there are  nodes in the segment tree, the complexity of updating is 
. The total complexity is then 
. We should also mention that the constant factor is not very small, since we operate on matrices and not just integers.
Note that we need to find  and 
 may not even be a prime number. However, this does not affect us since we only deal with operations of addition and multiplication throughout the whole procedure and we can just do them all modulo 
.
Comments