From 0f26e3b754e01464df2cfef35f419f0a56cc8eef Mon Sep 17 00:00:00 2001 From: "Paulo H. Lamounier" <53798700+Nanashii76@users.noreply.github.com> Date: Sun, 25 Feb 2024 14:03:11 -0300 Subject: [PATCH] Introduction to Linear recurrences and Matrix --- .../binaryExponetiation.cpp | 38 ++++++++++++++++ .../enxugando_gelo_no_sol.cpp | 44 +++++++++++++++++++ .../modularBinaryExponetation.cpp | 32 ++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 theory/linear_recurrences_matrix_expo/binaryExponetiation.cpp create mode 100644 theory/linear_recurrences_matrix_expo/enxugando_gelo_no_sol.cpp create mode 100644 theory/linear_recurrences_matrix_expo/modularBinaryExponetation.cpp diff --git a/theory/linear_recurrences_matrix_expo/binaryExponetiation.cpp b/theory/linear_recurrences_matrix_expo/binaryExponetiation.cpp new file mode 100644 index 0000000..31b2cec --- /dev/null +++ b/theory/linear_recurrences_matrix_expo/binaryExponetiation.cpp @@ -0,0 +1,38 @@ +#include + +int binaryPower(int a, int b) { + + if (a == 0) + return 0; + + int res = 1; + while(b) { + if(b&1) + res *= a; + a*=a; + b = b>>1; + } + + return res; + +} + + +int main(void) { + + std::cout << binaryPower(2,10) << std::endl; + + +} + + + + + + + + + + + + diff --git a/theory/linear_recurrences_matrix_expo/enxugando_gelo_no_sol.cpp b/theory/linear_recurrences_matrix_expo/enxugando_gelo_no_sol.cpp new file mode 100644 index 0000000..1977811 --- /dev/null +++ b/theory/linear_recurrences_matrix_expo/enxugando_gelo_no_sol.cpp @@ -0,0 +1,44 @@ +#include + +typedef long long ll; + +const int mod = 1e9+7; + +ll mfac(int i) { + + int m = i+2; + int ans = 1; + + for(int i = 0; i < 3; ++i) { + m %= mod; + ans *= m-i; + ans %= mod; + } + + return ans; + +} + +ll solve(int n) { + + int ans = 0; + + while(n > 0) { + ans += mfac(n); + n--; + } + + return ans; +} + + +int32_t main(void) { + + int n; + std::cin >> n; + std::cout << solve(n) << std::endl; + + +} + + diff --git a/theory/linear_recurrences_matrix_expo/modularBinaryExponetation.cpp b/theory/linear_recurrences_matrix_expo/modularBinaryExponetation.cpp new file mode 100644 index 0000000..93581d2 --- /dev/null +++ b/theory/linear_recurrences_matrix_expo/modularBinaryExponetation.cpp @@ -0,0 +1,32 @@ +#include + +#define int long long int + +// Constant isn't necessary +const int mod = 1e9 + 7; + +int binaryExponentation(int a, int b) { + + if (a == 0) + return 0; + + int ans = 1; + while(b) { + if(b&1) + ans *= a, ans %= mod; + a*=a; + a %= mod; + b = b>>1; + } + + return ans; + +} + +int32_t main ( void ){ + + std::cout << binaryExponentation(10,20) << std::endl; + +} + +