-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_11444.cpp
118 lines (106 loc) · 2.24 KB
/
main_11444.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
using namespace std;
class Matrix {
int n, m;
int** matrix;
public:
Matrix(const int n, const int m) : n(n), m(m) {
matrix = new int*[n];
for (int i = 0; i < n; ++i) {
matrix[i] = new int[m];
for (int j = 0; j < m; ++j) {
cin >> matrix[i][j];
matrix[i][j] %= 1000;
}
}
}
Matrix(int** matrix, const int n, const int m): n(n), m(m), matrix(matrix) {}
Matrix(const Matrix& copy): n(copy.n), m(copy.m) {
matrix = new int*[n];
for (int i = 0; i < n; ++i) {
matrix[i] = new int[m];
for (int j = 0; j < m; ++j) {
matrix[i][j] = copy.matrix[i][j];
}
}
}
~Matrix() {
for (int i = 0; i < n; ++i) {
delete[] matrix[i];
}
delete[] matrix;
}
Matrix& operator=(const Matrix& copy) {
if (this == ©) {
return *this;
}
for (int i = 0; i < n; ++i) {
delete[] matrix[i];
}
delete[] matrix;
n = copy.n;
m = copy.m;
matrix = new int*[n];
for (int i = 0; i < n; ++i) {
matrix[i] = new int[m];
for (int j = 0; j < m; ++j) {
matrix[i][j] = copy.matrix[i][j];
}
}
return *this;
}
Matrix& operator=(Matrix copy) noexcept {
if (this == ©) {
return *this;
}
swap(n, copy.n);
swap(m, copy.m);
swap(matrix, copy.matrix);
return *this;
}
Matrix operator*(const Matrix& other) const {
int** result = new int*[n];
for (int i = 0; i < n; ++i) {
result[i] = new int[other.m]{0,};
for (int j = 0; j < m; ++j) {
const long long tem = matrix[i][j];
for (int l = 0; l < other.m; ++l) {
result[i][l] = (result[i][l] + tem * other.matrix[j][l]) % 1000000007;
}
}
}
return Matrix(result, n, other.m);
}
Matrix operator^(const long long k) const {
if (k == 1) {
return *this;
}
const auto tem = *this ^ (k / 2);
if (k % 2 == 0) {
return tem * tem;
}
return tem * tem * *this;
}
friend ostream& operator<<(ostream& os, const Matrix& m) {
for (int i = 0; i < m.n; ++i) {
for (int j = 0; j < m.m; ++j) {
os << m.matrix[i][j] << ' ';
}
os << '\n';
}
return os;
}
int first() const {
return matrix[0][0];
}
};
int main() {
long long k;
cin >> k;
if (k == 1) {
cout << 1;
} else {
const Matrix m(new int* [2]{ new int[2]{1, 1}, new int[2]{1, 0}}, 2, 2);
cout << (m ^ (k - 1)).first();
}
}