tl;dr — video tutorial https://www.youtube.com/watch?v=eMXNWcbw75E and codeforces GYM training https://codeforces.com/gym/102644 (register by finding this contest in GYM instead of using the link directly)

video editorial: part 1 (ABCDEF) and part 2 (GHI)

codes to all 9 problems: https://github.com/Errichto/youtube/tree/master/matrix-exponentiation

Prerequisites: binary exponentiation and iterative dp (you don't need to know matrices)

The youtube tutorial (link) focuses on intuition and graph-like visualization . Or, if you prefer, below is a shorter (less detailed) text tutorial instead. You can practice by solving a set of 9 educational problems in GYM https://codeforces.com/gym/102644. ABCD are easy, EF medium, GHI are hard. If you are stuck, see hints below or watch the full solution analysis — part 1 (ABCDEF) and part 2 (GHI).

#### Hints

**C. Fibonacci**

**D. Count Paths**

**E. Knight Paths**

**F. Min Path**

**G. Recurrence With Square**

**H. String Mood Updates**

**I. Count Paths Qeuries**

#### Matrix Exponentiation

Consider this problem:

String Mood — Limak can be either happy or sad. His mood changes (or stays same) when he reads an English uppercase letter. Letters S and D always make him sad, H makes him happy, and every vowel (AEIOU) flips his mood. Other letters do nothing.

Limak is happy right now. Among all $$$26^n$$$ possible strings of length $$$n$$$ ($$$n \leq 10^{18}$$$), count such strings that Limak will be happy after reading that string, modulo $$$10^9+7$$$.

If something can be solved with dp in $$$O(1)$$$ space, we can usually speed it up later with matrix exponentiation. This dp is easy — for length from $$$1$$$ to $$$n$$$ compute the number of strings making you happy and making you sad at the end.

**dp in O(1) space**

Let's visualize that by drawing vertices representing the two moods, and edges with the number of ways to move from one state to the other.

**drawing 1**

For example, if you are happy, there are 19 ways to make you happy and 7 ways to make you sad (SDAEIOU). Thin edges on the right represent the second letter of a string and the number there should be the same, because they are again 19 ways to make you happy if you were happy, etc. If we were asked about answer for $$$n = 2$$$, we would need to compute the number of ways to get from HAPPY state in first column to HAPPY state in third column, which is they yellow edge below.

**drawing 2**

That's $$$19 \cdot 19 + 7 \cdot 6 = 403$$$. In a similar way, we can compute all four counts for 2-letter strings: happy to happy (equal to $$$403$$$), happy to sad ($$$19\cdot7+7\cdot20=273$$$), sad to happy ($$$234$$$), sad to sad ($$$442$$$). We'll actually keep these four values $$$[[403,273],[234, 442]]$$$ in a 2-dimensional array, also called a *matrix*.

Starting from a matrix with four values describing 1-letter strings $$$[[19,7],[6,20]]$$$, in $$$O(1)$$$ we got a matrix describing 2-letter strings. We can do the same to get a matrix for 4-letter strings, then 8-letter strings, and so on. We can do binary exponentiation to find a matrix for any huge $$$n$$$. Formally, we compute $$$M^n$$$ where $$$M = [[19,7],[6,20]]$$$ and multiplying two matrices is exactly what we did above to compute a new matrix $$$[[403,273],[234, 442]]$$$, done with the following code:

```
for(int i = 0; i < s; i++) { // s is the number of states, s=2 in the String Mood problem
for(int j = 0; j < s; j++) {
for(int k = 0; k < s; k++) {
new_matrix[i][k] += matrix1[i][j] * matrix2[j][k];
}
}
}
```

The total time complexity is $$$O(s^3 \cdot \log n)$$$ where $$$s$$$ is the matrix size (also called *order*), which is equal to the number of states (variables) you need in space-efficient dp. We had $$$s=2$$$ in String Mood so the complexity is just $$$O(\log n)$$$.

**full C++ solution**

Now, try to find the $$$n$$$-th Fibonacci number for $$$n \leq 10^{18}$$$ (problem link) by first implementing space-efficient dp with transitions of form `new_variable += old_variable * x`

where $$$x$$$ is some number you will need to put in the initial matrix (like $$$19$$$ or $$$7$$$ in String Mood).

#### Footnote

Thanks to tnowak for suggesting and creating one problem, and to mnbvmar for a few problem ideas. For the future, I'm looking for somebody to help me with the preparation of such training contests. If you want to help and you are high-rated and perfectly with experience in using polygon, please write to me. I can pay you a little if you want.

I hope you learned something thanks to this tutorial. Feel free to discuss problems from the GYM contest or give links to more matrix exponentiation problems or useful resources. If anybody wants that, I can make tests public. If you are a teacher and want to use some problems in your classes, I will give you access in Polygon.

I assume there is no way to register lately for such contest? Unfortunatly there seems to be no way to submit without registering.

Apart from that, thanks for the work, I can submit after contests ends, too.

Strange, it should be possible. Maybe go to GYM tab and find the contest there?

Yes, from GYM tab registration works, thanks.

Great Initiative! Hoping for more such training contests. What topic are you planning next ?

I think you should make group or something if you are willing to organise more such contests :)

"I can pay you a little if you want". Respect for this helping attitude of yours.

It's good that you are appreciating his effort but please never say pay you, it kind of sounds disrespectful for the effort he has Put in. These are some of the situations where person did not care about money but rather just want to help others.

Kind of a stupid concern, but why does WA on pretest 1 give a -1? If someone has a WA on pretest 1 and then AC, he gets +1 in standings.

Waiting for the video :>

Can anyone suggest where I am wrong at

`I. Count Paths Qeuries`

it is still giving`TLE`

You are still doing matrix-matrix multiplications when answering queries. That will be too slow. You need to do vector-matrix multiplications. I.e. if you want to compute

`v*M*N*O*P`

, do`(((v*M)*N)*O)*P`

instead of`v*((((N*M)*O)*P)`

.But first, you need to work out the vector

`v`

.`(((v*M)*N)*O)*P`

I am doing this only , what make you think I am doing`v*((((N*M)*O)*P)`

By the way thank you very much I got AC by vector-matrix multiplication.

`v`

(or your`I`

) is a $$$n \times n$$$ matrix, and therefore each multiplication is $$$O(n^3)$$$. It needs to be a vector (so $$$1 \times n$$$). That way every multiplication is $$$O(n^2)$$$.Yes , I was able to score AC with your suggestions

The multiply() works in $$$O(n^3)$$$, right?

You call multiply q*log(k) times, which makes it $$$O(n^3 * q * log(k))$$$

Since q==n it is basically $$$O(n^4 * log(k))$$$

Jakube is correct that you should do vector-matrix multiplication. And you should compute the time complexity next time before asking why your code is too slow.

Also, your

`multiply`

function can be improved a lot:`a[i][k]`

is already stored modulo $$$p$$$, there's no need for`(a[i][k]%p)`

.`a[i][k]`

and`b[k][j]`

, neither $$$i$$$ nor $$$k$$$ should be the third of three for-loops because you jump all over memory this way.Or you can just take implementation from my blog and compare the running time yourself.