题1
代码
矩阵快速幂,不会的看【蓝桥杯】试题 算法提高 递推求值c++实现
#include<bits/stdc++.h>
using namespace std;
class mat {
public:
int a[3][3];
mat operator*(mat b) {
mat ans = {0, 0, 0,
0, 0, 0,
0, 0, 0};
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k) {
ans.a[i][j] += this->a[i][k] * b.a[k][j];
ans.a[i][j] %= 10000;
}
}
}
return ans;
}
};
mat s = {3, 2, 1,
0, 0, 0,
0, 0, 0};
mat a = {1, 1, 0,
1, 0, 1,
1, 0, 0};
mat f(int i) {
mat res = {1, 0, 0,
0, 1, 0,
0, 0, 1};
while (i) {
if (i & 1) {
res = res * a;
}
a = a * a;
i >>= 1;
}
return res;
}
int main() {
int n = 4;
mat ans = s * f(n - 3);
cout << ans.a[0][0];
return 0;
}
题2
代码
dp
#include<bits/stdc++.h>
using namespace std;
int n;
//stair[last step][last foot]:
//left foot:0,right foot:1
int stair[40][2];
int f() {
stair[1][0] = 0;
stair[2][0] = 1;
stair[1][1] = 1;
stair[2][1] = 1;
if (n <= 2)return stair[n][1];
for (int i = 3; i <= n; ++i) {
stair[i][0] = stair[i - 1][1] + stair[i - 2][1];
stair[i][1] = stair[i - 1][0] + stair[i - 2][0];
}
return stair[n][1];
}
int main() {
cin >> n;
cout << f();
return 0;
}
题3
代码
直接set完事
#include<bits/stdc++.h>
using namespace std;
int main() {
int temp;
set<int> a;
for (int i = 0; i < 10; ++i) {
cin >> temp;
a.insert(temp);
}
for (set<int>::iterator i = a.begin(); i != a.end(); ++i) {
cout << *i << endl;
}
return 0;
}