editorial.pdf

Text preview
Problem A
Letβs first solve a simpler problem β when the sequence π is cyclic, i.
e. when ππ = ππ πππ π , for π β₯ 0.
This simpler version is similar to Fibonacci sequence. Actually, for π = 1
and π0 = 1, it is the Fibonacci sequence. To find the πΎ π‘β number of these
kind of recursive sequences fast we should first write them in their matrix
form. Matrix form of this sequence is:
πΉ
π
π
πΉ
( π ) = ( πβ1 πβ2 ) ( πβ1 )
πΉπβ1
πΉπβ2
1
0
Expanding this, we can see that
πΉ
πΉ
π ππβ1
( πΎ ) = πΆπΎβ1 πΆπΎβ2 β¦ πΆ2 πΆ1 ( 1 ), where πΆπ = ( π
).
πΉπΎβ1
πΉ0
1
0
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 πΆπβ1 πΆπβ2 β¦ πΆ1 πΆ0 =
π π
πΆππ+(πβ1) πΆππ+(πβ2) β¦ πΆππ+1 πΆππ , for π β₯ 0 (note that πΆ0 = ( 0 πβ1 ) ).
1
0
Letβs define this product of matrices as π = πΆπβ1 πΆπβ2 β¦ πΆ1 πΆ0 .
Now, we can write a formula for πΉπΎ that can be calculated quickly:
πΉπΎ
πΉ
(
) = πΆπβ1 πΆπβ2 β¦ πΆ1 πΆ0 π π πΆπβ1 πΆπβ2 β¦ πΆ2 πΆ1 ( 1 ), where π = (πΎ β
πΉπΎβ1
πΉ0
π) πππ£ π and π = πΎ πππ π.
We can calculate π π in π(ππππ) steps using exponentiaton 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
π β π β β¦ β π β πΈ1 β π β π β β¦ β π β πΈ2 β β¦ = π π‘1 β πΈ1β β π π‘2 β πΈ2 β π π‘3 β β¦
where πΈπ denotes the product of matrices of the cycle, with some matrices
being different than the matrices of the original cycle. Also, π β€ 2π since