-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmatrix_exponentiation.cpp
More file actions
84 lines (79 loc) · 2.21 KB
/
matrix_exponentiation.cpp
File metadata and controls
84 lines (79 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// https://codeforces.com/gym/102644/problem/C
// achar o n-ésimo termo da sequencia de fibonacci mod (10^9 + 7) em O(log(n))
// n <= 10^18
// podemos escrever a recorrencia de fibonnaci como uma exponenciação de matriz
/*
( fib(n) ) (1 1) ^ (n - 1) (fib(1) = 1)
(fib(n - 1)) = (1 0) * (fib(0) = 1)
*/
// é possivel fazer essa exponenciação em O(log(n)) com um algoritimo muito similar ao de exponenciação rapida
// dai calculamos o n-ésimo termo da sequencia de fibonacci mod (10^9 + 7) em O(log(n))
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define PI acos(-1)
#define pb push_back
#define int long long int
#define pi pair<int, int>
#define pii pair<pi, int>
#define fir first
#define sec second
#define DEBUG 0
#define MAXN 201
#define mod 1000000007
namespace matrix
{
vector<vector<int>> ans;
int multi(int x, int y)
{
return (x * y) % mod;
}
int sum(int a, int b)
{
return (a + b >= mod) ? a + b - mod : a + b;
}
vector<vector<int>> multiply(vector<vector<int>> a, vector<vector<int>> b)
{
vector<vector<int>> res(a[0].size(), vector<int>(b[0].size()));
for (int i = 0; i < a.size(); i++)
{
for (int j = 0; j < b[0].size(); j++)
{
res[i][j] = 0;
for (int k = 0; k < a[0].size(); k++)
res[i][j] = sum(res[i][j], multi(a[i][k], b[k][j]));
}
}
return res;
}
vector<vector<int>> expo(vector<vector<int>> mat, int m)
{
ans = vector<vector<int>>(mat.size(), vector<int>(mat[0].size()));
for (int i = 0; i < mat.size(); i++)
for (int j = 0; j < mat[0].size(); j++)
ans[i][j] = (i == j);
while (m > 0)
{
if (m & 1)
ans = multiply(ans, mat);
m = m / 2;
mat = multiply(mat, mat);
}
return ans;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<vector<int>> mat = {{1, 1}, {1, 0}};
vector<vector<int>> ans = matrix::expo(mat, n);
cout << ans[0][1] << endl;
return 0;
}