Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
matrix_exponentiation.cpp File Reference

Matrix Exponentiation. More...

#include <iostream>
#include <vector>
Include dependency graph for matrix_exponentiation.cpp:

Macros

#define ll   int64_t
 
#define endl   std::endl
 
#define pb   push_back
 
#define MOD   1000000007
 

Functions

ll ab (ll x)
 
vector< vector< ll > > multiply (const vector< vector< ll >> &A, const vector< vector< ll >> &B)
 
vector< vector< ll > > power (const vector< vector< ll >> &A, ll p)
 
ll ans (ll n)
 
int main ()
 

Variables

ll k
 
vector< lla
 
vector< llb
 
vector< llc
 

Detailed Description

Matrix Exponentiation.

The problem can be solved with DP but constraints are high.
\(a_i = b_i\) (for \(i <= k\))
\(a_i = c_1 a_{i-1} + c_2 a_{i-2} + ... + c_k a_{i-k}\) (for \(i > k\))
Taking the example of Fibonacci series, \(k=2\)
\(b_1 = 1,\; b_2=1\)
\(c_1 = 1,\; c_2=1\)
\(a = \begin{bmatrix}0& 1& 1& 2& \ldots\end{bmatrix}\)
This way you can find the \(10^{18}\) fibonacci numberMOD. I have given a general way to use it. The program takes the input of B and C matrix.

Steps for Matrix Expo

  1. Create vector F1 : which is the copy of B.
  2. Create transpose matrix (Learn more about it on the internet)
  3. Perform \(T^{n-1}\) [transpose matrix to the power n-1]
  4. Multiply with F to get the last matrix of size (1 \(\times\)k).

The first element of this matrix is the required result.

Macro Definition Documentation

◆ endl

#define endl   std::endl

shorthand definition for std::endl

◆ ll

#define ll   int64_t

shorthand definition for int64_t

◆ pb

#define pb   push_back

shorthand definition for int64_t

Function Documentation

◆ ab()

ll ab ( ll  x)
inline

returns absolute value

43 { return x > 0LL ? x : -x; }

◆ ans()

ll ans ( ll  n)

Wrapper for Fibonacci

Parameters
[in]n\(n^\text{th}\) Fibonacci number
Returns
\(n^\text{th}\) Fibonacci number
94  {
95  if (n == 0)
96  return 0;
97  if (n <= k)
98  return b[n - 1];
99  // F1
100  vector<ll> F1(k + 1);
101  for (ll i = 1; i <= k; i++) F1[i] = b[i - 1];
102 
103  // Transpose matrix
104  vector<vector<ll>> T(k + 1, vector<ll>(k + 1));
105  for (ll i = 1; i <= k; i++) {
106  for (ll j = 1; j <= k; j++) {
107  if (i < k) {
108  if (j == i + 1)
109  T[i][j] = 1;
110  else
111  T[i][j] = 0;
112  continue;
113  }
114  T[i][j] = c[k - j];
115  }
116  }
117  // T^n-1
118  T = power(T, n - 1);
119 
120  // T*F1
121  ll res = 0;
122  for (ll i = 1; i <= k; i++) {
123  res = (res + (T[1][i] * F1[i]) % MOD) % MOD;
124  }
125  return res;
126 }

◆ main()

int main ( )

Main function

129  {
130  cin.tie(0);
131  cout.tie(0);
132  ll t;
133  cin >> t;
134  ll i, j, x;
135  while (t--) {
136  cin >> k;
137  for (i = 0; i < k; i++) {
138  cin >> x;
139  b.pb(x);
140  }
141  for (i = 0; i < k; i++) {
142  cin >> x;
143  c.pb(x);
144  }
145  cin >> x;
146  cout << ans(x) << endl;
147  b.clear();
148  c.clear();
149  }
150  return 0;
151 }

◆ multiply()

vector<vector<ll> > multiply ( const vector< vector< ll >> &  A,
const vector< vector< ll >> &  B 
)

To multiply 2 matrices

Parameters
[in]Amatrix 1 of size (m \(\times\)n)
[in]Bmatrix 2 of size (p \(\times\)q)
Note
\(p=n\)
Returns
matrix of dimension (m \(\times\)q)
61  {
62  vector<vector<ll>> C(k + 1, vector<ll>(k + 1));
63  for (ll i = 1; i <= k; i++) {
64  for (ll j = 1; j <= k; j++) {
65  for (ll z = 1; z <= k; z++) {
66  C[i][j] = (C[i][j] + (A[i][z] * B[z][j]) % MOD) % MOD;
67  }
68  }
69  }
70  return C;
71 }

◆ power()

vector<vector<ll> > power ( const vector< vector< ll >> &  A,
ll  p 
)

computing integer power of a matrix using recursive multiplication.

Note
A must be a square matrix for this algorithm.
Parameters
[in]Abase matrix
[in]pexponent
Returns
matrix of same dimension as A
79  {
80  if (p == 1)
81  return A;
82  if (p % 2 == 1) {
83  return multiply(A, power(A, p - 1));
84  } else {
85  vector<vector<ll>> X = power(A, p / 2);
86  return multiply(X, X);
87  }
88 }
Here is the call graph for this function:

Variable Documentation

◆ a

vector<ll> a

global vector variables

Todo:
@stepfencurryxiao add documetnation

◆ k

ll k

global variable k

Todo:
@stepfencurryxiao add documetnation
std::vector
STL class.
ans
ll ans(ll n)
Definition: matrix_exponentiation.cpp:94
multiply
vector< vector< ll > > multiply(const vector< vector< ll >> &A, const vector< vector< ll >> &B)
Definition: matrix_exponentiation.cpp:60
std::cout
k
ll k
Definition: matrix_exponentiation.cpp:48
endl
#define endl
Definition: matrix_exponentiation.cpp:36
power
vector< vector< ll > > power(const vector< vector< ll >> &A, ll p)
Definition: matrix_exponentiation.cpp:79
std::cin