# Sudo Placement 2 | Matrix Series

A Matrix series is defined as follows:

M, M,^{T}, M(M^{T}), M(M^{T})^{2}, M^{2}(M^{T})^{3}, M^{3}(M^{T})^{5}, M^{5}(M^{T})^{8}. . . . . . . .where M is a binary square matrix of size K x K (Binary Matrix is a special type of Matrix where each element of the matrix is either 0 or 1) and

Mrepresents the^{T}transposeof Matrix M.

Given **N** and **K**, find the N^{th} term of the series.

**Prerequisites : ** Modular Exponentiation

Examples:

Input : N = 2, K = 4 M = { {1, 1}, {0, 1} } Output : [ 3 1] [ 2 1]Explanation: The 4^{th}term of the series is M^{2}(M^{T})^{3}and the value ofMis {{3, 1}, {2, 1}}. Input : N = 2, K = 5 M = { {1, 1}, {0, 1} } Output : [7 2] [3 1]^{2}(M^{T})^{3}Explanation: The 4^{th}term of the series isMand the value of^{3}(M^{T})^{5}Mis {{7, 2}, {3, 1}}.^{3}(M^{T})^{5}

**Approach :**

It can be observed that the powers of **M ^{T}** are 0, 1, 1, 2, 3, 5, 8….. for the 1

^{st}, 2

^{nd}, 3

^{rd}….. terms respectively. This pattern for the powers of M

^{T}is nothing but the Fibonacci series.

Except for the first term, it can be seen that the powers of ** M **also have the same pattern but, here the power of M is the same as the power of M^{T} for the previous term.

Since in K^{th} term M^{T} has a power of fib_{K}, M has the power of fib_{K – 1}.

Where fib_{i} represents the i^{th} fibonacci number.

Thus, for the K^{th} term (for K ≠ 1) of the series can be calculated as:

S_{k}= M^{fib(k - 1)}(M^{T})^{fib(K) }

As Fibonacci values increase pretty fast the 45^{th} Fibonacci number is close to 10^{10}. So the K^{th} power cant be calculated by repeated multiplication of the matrices K times. To do this, efficiently we can calculate the K^{th} power of the matrix using an idea similar to Modular Exponentiation.

As in Modular Exponentiation, the power is divider by 2 at every step, here also we follow the same Divide and conquer strategy except the fact that here we don’t multiply numbers, instead, here multiplication of matrices is required which can be done in O(N^{3}), where N is the size of the square matrix.

Below program illustrate the above approach:

`// CPP code to find Kth term of the Matrix Series ` `#include <bits/stdc++.h> ` ` ` `#define ll long long ` `#define mod 1000000007 ` ` ` `using` `namespace` `std; ` ` ` `// This function multiplies two matrices A and B, under modulo mod ` `// and returns the resultant matrix after multiplication ` `vector<vector<` `int` `> > multiply(vector<vector<` `int` `> > A, ` ` ` `vector<vector<` `int` `> > B) ` `{ ` ` ` ` ` `// n is the size of the matrix ` ` ` `int` `n = A.size(); ` ` ` ` ` `// Resultant matrix formded after multiplying matrix A and B ` ` ` `vector<vector<` `int` `> > result(n, vector<` `int` `>(n, 0)); ` ` ` ` ` `// Matrix Multiplication ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `for` `(` `int` `j = 0; j < n; j++) { ` ` ` `for` `(` `int` `k = 0; k < n; k++) { ` ` ` `result[i][j] = (result[i][j] + (A[i][k] * B[k][j]) % mod) % mod; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `result; ` `} ` ` ` `// Function to find the Kth power of matrix A of size nXn in O(logK) ` `// similar to Modular Exponentiation ` `vector<vector<` `int` `> > fastpower(vector<vector<` `int` `> > A, ` `int` `n, ll K) ` `{ ` ` ` `// Base Case ` ` ` `if` `(K == 1) ` ` ` `return` `A; ` ` ` ` ` `// Recursive Case1: If power is Odd ` ` ` `if` `(K & 1) { ` ` ` `// power(A, K) = power(A, K/2) * power(A, K/2) * A ` ` ` `// when K is odd, Note than in this implementation ` ` ` `// multiply (power(A, K - 1) * A) as in the case ` ` ` `// the power becomes even in the next recursive call ` ` ` `return` `multiply(A, fastpower(A, n, K - 1)); ` ` ` `} ` ` ` ` ` `// power(A, K) = power(A, K/2) * power(A, K/2) if K is even ` ` ` `vector<vector<` `int` `> > result = fastpower(A, n, K / 2); ` ` ` `return` `multiply(result, result); ` `} ` ` ` `// Returns the transpose of the matrix A ` `vector<vector<` `int` `> > transpose(vector<vector<` `int` `> > A) ` `{ ` ` ` `int` `N = A.size(); ` ` ` `vector<vector<` `int` `> > transposeMatrix(N, vector<` `int` `>(N, 0)); ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `for` `(` `int` `j = 0; j < N; j++) { ` ` ` `transposeMatrix[i][j] = A[j][i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `transposeMatrix; ` `} ` ` ` `// Prints the matrix A ` `void` `printMatrix(vector<vector<` `int` `> > A) ` `{ ` ` ` `int` `n = A.size(); ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `for` `(` `int` `j = 0; j < n; j++) { ` ` ` `cout << A[i][j] << ` `" "` `; ` ` ` `} ` ` ` `cout << endl; ` ` ` `} ` `} ` ` ` `// Return the Kth term of the series where matrix M ` `// is a boolean matrix of size n X n ` `void` `getKthTerm(vector<vector<` `int` `> > M, ` `int` `n, ` `int` `K) ` `{ ` ` ` ` ` `// precompue fibonacci till the Kth term ` ` ` `ll fibonacci[K + 1]; ` ` ` ` ` `// ith fibonacci number denotes the power of M' in ` ` ` `// the ith term, M' represents the transpose of M ` ` ` `// 1st term has power of M' as 0 thus fib[0] = 1 ` ` ` `fibonacci[1] = 0ll; ` ` ` `fibonacci[2] = 1ll; ` ` ` `for` `(` `int` `i = 3; i <= K; i++) { ` ` ` `fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]; ` ` ` `} ` ` ` ` ` `// stores the transpose of Matrix M ` ` ` `vector<vector<` `int` `> > transposeM = transpose(M); ` ` ` ` ` `// K = 1 and K = 2, is handled separately ` ` ` `if` `(K == 1) { ` ` ` `printMatrix(M); ` ` ` `} ` ` ` `else` `if` `(K == 2) { ` ` ` `printMatrix(transposeM); ` ` ` `} ` ` ` ` ` `else` `{ ` ` ` `vector<vector<` `int` `> > MpowerFibKminusOne; ` ` ` `MpowerFibKminusOne = fastpower(M, n, fibonacci[K - 1]); ` ` ` ` ` `vector<vector<` `int` `> > MTransposePowerFibK; ` ` ` `MTransposePowerFibK = fastpower(transposeM, n, fibonacci[K]); ` ` ` ` ` `// kthTerm = (M^fib[K - 1]) * (transposeM ^ fib[K]) ` ` ` `vector<vector<` `int` `> > kthTerm = multiply(MpowerFibKminusOne, ` ` ` `MTransposePowerFibK); ` ` ` ` ` `// Print the Resultant Matrix ` ` ` `printMatrix(kthTerm); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `n, K; ` ` ` `n = 2; ` ` ` `K = 4; ` ` ` `vector<vector<` `int` `> > M{ { 1, 1 }, { 0, 1 } }; ` ` ` `getKthTerm(M, n, K); ` ` ` ` ` `// prints the 5th term ` ` ` `K = 5; ` ` ` `getKthTerm(M, n, K); ` ` ` ` ` `return` `0; ` `} ` ` ` |

*chevron_right*

*filter_none*

**Output :**

3 1 2 1 7 2 3 1

## Recommended Posts:

- Sudo Placement | Placement Tour
- Sudo Placement[1.4] | K Sum
- Sudo Placement[1.5] | Wolfish
- Sudo Placement[1.5] | Partition
- Sudo Placement[1.4] | BST Traversal
- Sudo Placement | Range Queries
- Sudo Placement | Special Subsequences
- Sudo Placement | Beautiful Pairs
- Sudo Placement | Palindrome Family
- Sudo Placement[1.4] | Jumping the Subtree
- Sudo Placement[1.3] | Playing with Stacks
- Sudo Placement[1.3] | Stack Design
- Sudo Placement[1.3] | Final Destination
- Sudo Placement[1.5] | Second Smallest in Range
- Sudo Placement[1.7] | Greatest Digital Root

This article is contributed by **Team GeeksforGeeks**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.