-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMATRIXEXPONENTIATIONALL.cpp
More file actions
97 lines (86 loc) · 2.09 KB
/
MATRIXEXPONENTIATIONALL.cpp
File metadata and controls
97 lines (86 loc) · 2.09 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
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdio>
#include <cassert>
#include <cctype>
#include <algorithm>
#include <iostream>
#include <iostream>
#include <cstdio>
using namespace std;
long long mod = 1000000007LL;
template< class T >
class Matrix {
public:
int m, n;
T *data;
Matrix(int m, int n);
Matrix(const Matrix< T > &matrix);
const Matrix< T > &operator=(const Matrix< T > &A);
const Matrix< T > operator*(const Matrix< T > &A);
const Matrix< T > operator^(int P);
~Matrix();
};
template< class T >
Matrix< T >::Matrix(int m, int n) {
this->m = m;
this->n = n;
data = new T[m * n];
}
template< class T >
Matrix< T >::Matrix(const Matrix< T > &A) {
this->m = A.m;
this->n = A.n;
data = new T[m * n];
for (int i = 0; i < m * n; i++)
data[i] = A.data[i];
}
template< class T >
Matrix< T >::~Matrix() {
delete [] data;
}
template< class T >
const Matrix< T > &Matrix< T >::operator=(const Matrix< T > &A) {
if (&A != this) {
delete [] data;
m = A.m;
n = A.n;
data = new T[m * n];
for (int i = 0; i < m * n; i++)
data[i] = A.data[i];
}
return *this;
}
template< class T >
const Matrix< T > Matrix< T >::operator*(const Matrix< T > &A) {
Matrix C(m, A.n);
for (int i = 0; i < m; ++i)
for (int j = 0; j < A.n; ++j) {
C.data[i * C.n + j] = 0;
for (int k = 0; k < n; ++k)
C.data[i * C.n + j] = (C.data[i * C.n + j] + (data[i * n + k] * A.data[k * A.n + j]) % mod) % mod;
}
return C;
}
template< class T >
const Matrix< T > Matrix< T >::operator^(int P) {
if (P == 1) return (*this);
if (P & 1) return (*this) * ((*this) ^ (P - 1));
Matrix B = (*this) ^ (P / 2);
return B*B;
}
int main() {
Matrix<long long> M(2, 2);
M.data[0] = 1;//(0,0)
M.data[1] = 1;//(0,1)
M.data[2] = 1;//(1,0)
M.data[3] = 0;//(1,1)
long t;
cin >> t;
while (t--) {
long long N;
scanf("%d", &N);
if (N > 1)
printf("%lld\n", ((M^N).data[0]) % mod);
else
printf("%d\n", F[N]);
}
}