Skip to content

Commit 7ba37de

Browse files
authored
Update domino-and-tromino-tiling.cpp
1 parent bb0b49d commit 7ba37de

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

C++/domino-and-tromino-tiling.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,19 @@ class Solution {
1313

1414
private:
1515
vector<vector<int>> matrixExpo(const vector<vector<int>>& A, int pow) {
16-
if (pow == 0) {
17-
vector<vector<int>> I(A.size(), vector<int>(A.size()));
18-
for (int i = 0; i < A.size(); ++i) {
19-
I[i][i] = 1;
20-
}
21-
return I;
22-
}
23-
if (pow == 1) {
24-
return A;
16+
vector<vector<int>> result(A.size(), vector<int>(A.size()));
17+
vector<vector<int>> A_exp(A);
18+
for (int i = 0; i < A.size(); ++i) {
19+
result[i][i] = 1;
2520
}
26-
if (pow % 2 == 1) {
27-
return matrixMult(matrixExpo(A, pow - 1), A);
21+
while (pow) {
22+
if (pow % 2 == 1) {
23+
result = matrixMult(result, A_exp);
24+
}
25+
A_exp = matrixMult(A_exp, A_exp);
26+
pow /= 2;
2827
}
29-
const auto& B = matrixExpo(A, pow / 2);
30-
return matrixMult(B, B);
28+
return result;
3129
}
3230

3331
vector<vector<int>> matrixMult(const vector<vector<int>>& A, const vector<vector<int>>& B) {

0 commit comments

Comments
 (0)