forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Climbing_Stairs.cc
50 lines (50 loc) · 1.22 KB
/
Climbing_Stairs.cc
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
class Solution {
public:
int climbStairs(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (n < 2) {
return 1;
}
Mat ans;
ans.a[0][0] = ans.a[1][0] = ans.a[0][1] = 1;
ans = Power(ans, n);
return ans.a[0][0];
}
private:
struct Mat {
int a[2][2];
Mat() {
memset(a, 0, sizeof(a));
}
};
Mat Mul(Mat &A, Mat &B) {
Mat ret;
for (int k = 0; k < 2; k++) {
for (int i = 0; i < 2; i++) {
if (A.a[i][k] == 0) {
continue;
}
for (int j = 0; j < 2; j++) {
if (B.a[k][j] == 0) {
continue;
}
ret.a[i][j] += A.a[i][k] * B.a[k][j];
}
}
}
return ret;
}
Mat Power(Mat A, int k) {
Mat ret;
ret.a[0][0] = ret.a[1][1] = 1;
while (k) {
if (k & 1) {
ret = Mul(ret, A);
}
k >>= 1;
A = Mul(A, A);
}
return ret;
}
};