<
typenameT =
int64_t>
51std::vector<std::vector<T>> matrix_multiplication(
52 conststd::vector<std::vector<T>>& _mat_a,
53 conststd::vector<std::vector<T>>& _mat_b,
constint64_t mod = 1000000007) {
55assert(_mat_a[0].size() == _mat_b.size());
56std::vector<std::vector<T>> _mat_c(_mat_a.size(),
57std::vector<T>(_mat_b[0].size(), 0));
61 for(uint32_t i = 0; i < _mat_a.size(); ++i) {
62 for(uint32_t j = 0; j < _mat_b[0].size(); ++j) {
63 for(uint32_t k = 0; k < _mat_b.size(); ++k) {
66(_mat_a[i][k] % mod * _mat_b[k][j] % mod) % mod) %
81template<
typenameT =
int64_t>
82boolis_zero_matrix(
conststd::vector<std::vector<T>>& _mat) {
83 for(uint32_t i = 0; i < _mat.size(); ++i) {
84 for(uint32_t j = 0; j < _mat[i].size(); ++j) {
85 if(_mat[i][j] != 0) {
103template<
typenameT =
int64_t>
104std::vector<std::vector<T>> matrix_exponentiation(
105std::vector<std::vector<T>> _mat, uint64_t
power,
106 constint64_t mod = 1000000007) {
112 if(is_zero_matrix(_mat)) {
116std::vector<std::vector<T>> _mat_answer(_mat.size(),
117std::vector<T>(_mat.size(), 0));
119 for(uint32_t i = 0; i < _mat.size(); ++i) {
120_mat_answer[i][i] = 1;
125_mat_answer = matrix_multiplication(_mat_answer, _mat, mod);
128_mat = matrix_multiplication(_mat, _mat, mod);
154template<
typenameT =
int64_t>
155T get_nth_term_of_recurrence_series(
156 conststd::vector<std::vector<T>>& _mat,
157 conststd::vector<std::vector<T>>& _base_cases, uint64_t nth_term,
158 boolconstant_or_sum_included =
false) {
159assert(_mat.size() == _base_cases.back().size());
165 if(nth_term < _base_cases.back().size() - constant_or_sum_included) {
166 return_base_cases.back()[nth_term - constant_or_sum_included];
172std::vector<std::vector<T>> _res_matrix =
173matrix_exponentiation(_mat, nth_term - _base_cases.back().size() +
1741 + constant_or_sum_included);
180std::vector<std::vector<T>> _res =
181matrix_multiplication(_base_cases, _res_matrix);
183 return_res.back().back();
207std::vector<std::vector<int64_t>> fibonacci_matrix = {{0, 1}, {1, 1}},
208fib_base_case = {{0, 1}};
210assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
211fibonacci_matrix, fib_base_case, 11) == 89LL);
212assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
213fibonacci_matrix, fib_base_case, 39) == 63245986LL);
230std::vector<std::vector<int64_t>> tribonacci = {{0, 0, 1},
236assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
237tribonacci, trib_base_case, 11) == 149LL);
238assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
239tribonacci, trib_base_case, 36) == 615693474LL);
253std::vector<std::vector<int64_t>> pell_recurrence = {{0, 1}, {1, 2}},
257assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
258pell_recurrence, pell_base_case, 15) == 551614LL);
259assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
260pell_recurrence, pell_base_case, 23) == 636562078LL);
283std::vector<std::vector<int64_t>>
284custom_recurrence = {{1, 0, 1}, {0, 0, 1}, {0, 1, 2}},
285custom_base_case = {{7, 2, 2}};
287assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
288custom_recurrence, custom_base_case, 10, 1) == 18493LL);
289assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
290custom_recurrence, custom_base_case, 19, 1) == 51531251LL);
316std::vector<std::vector<int64_t>> sum_fibo_recurrence = {{0, 1, 1},
319sum_fibo_base_case = {
322assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
323sum_fibo_recurrence, sum_fibo_base_case, 13, 1) == 609LL);
324assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
325sum_fibo_recurrence, sum_fibo_base_case, 16, 1) == 2583LL);
353std::vector<std::vector<int64_t>> tribonacci_sum = {{0, 0, 1, 1},
357trib_sum_base_case = {{0, 0, 1, 1}};
360assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
361tribonacci_sum, trib_sum_base_case, 18, 1) == 23249LL);
362assert(math::linear_recurrence_matrix::get_nth_term_of_recurrence_series(
363tribonacci_sum, trib_sum_base_case, 19, 1) == 42762LL);
static void test()
Self-test implementations.
int main()
Main function.
Functions for Linear Recurrence Matrix implementation.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4