-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass05_problem01.cpp
142 lines (129 loc) · 2.54 KB
/
class05_problem01.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
//递归加回溯写法
class PROBLEM01
{
public:
//idx代表目前字符串的长度
//s目前字符串
//N目标长度
int get_res(vector<char> s, int N, int idx)
{
int ans = 0;
//截至条件,每一个达到目标的叶节点
if (s.size() == N)
return 1;
//上一个字符为'1',下一个字符有两种情况
if (s[idx - 1] == '1')
{
s.push_back('1');
ans += get_res(s, N, idx + 1);
//回溯
s[idx] = '0';
ans += get_res(s, N, idx + 1);
}
//上一个字符为'0',下一个字符只有一种情况
if (s[idx - 1] == '0')
{
s.push_back('1');
ans += get_res(s, N, idx + 1);
}
return ans;
}
int MAIN(int T)
{
int ans = 0;
vector<char> Arr1;
Arr1.push_back('0');
PROBLEM01 P1;
ans = P1.get_res(Arr1, T, 1);
Arr1[0] = '1';
ans += P1.get_res(Arr1, T, 1);
cout << ans << endl;
return 0;
}
};
//斐波那契数列写法,复杂度O(N)
class PROBLEM01_2
{
public:
int get_res(int N)
{
vector<int> res(N+1,0);
res[0] = 1;
res[1] = 2;
for (int i = 2; i <= N; i++)
{
res[i] = res[i - 1] + res[i - 2];
}
cout << res[N] << endl;
return 0 ;
}
};
//斐波那契数列进阶模式,复杂度O(logN)
class PROBLEM01_3
{
public:
int get_res(int N)
{
vector<vector<int>> M = { {1,1},{1,0} };//初始转换矩阵
vector<int> base = { 3,2 };//起始项
vector<vector<int>> M_N1(M.size(), vector<int>(M.size(), 0));//最终的转化矩阵
//初始化为对角矩阵
for (int i = 0; i < M.size(); i++)
M_N1[i][i] = 1;
int temp = N - 2;//转换矩阵需要N-2次方
while (temp != 0)
{
if (temp & 1 == 1)
{
M_N1 = POW(M_N1, M);
M = POW(M, M);
}
else
M = POW(M, M);
temp = temp >> 1;
}
vector<int> res = DOT(base, M_N1);
return res[0];
}
vector<vector<int>> POW(vector<vector<int>> Arr1, vector<vector<int>> Arr2)
{
vector<vector<int>> res(Arr1.size(),vector<int>(Arr2[0].size(),0));
for (int i = 0; i < Arr1.size(); i++)
{
for (int j = 0; j < Arr2.size(); j++)
{
for(int k=0;k<Arr1[0].size();k++)
res[i][j] += Arr1[i][k] * Arr2[k][j];
}
}
return res;
}
vector<int> DOT(vector<int> Arr, vector<vector<int>> M)
{
int L = Arr.size();
vector<int> res(L, 0);
for (int i = 0; i < L; i++)
{
for (int j = 0; j < L; j++)
{
res[i] += Arr[j] * M[j][i];
}
}
return res;
}
};
int main()
{
int T = 4;
PROBLEM01 P;
P.MAIN(4);
PROBLEM01_2 P2;
P2.get_res(4);
PROBLEM01_3 P3;
cout << P3.get_res(4) << endl;
return 0;
}