Files
python/TangDou/LuoGuBook/P2437.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

41 lines
1021 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
//高精度+裴波那契数列
//本题目考点:
//1、递推
//2、递推关系式的推导找出任意一个位置思考它是怎么来的再用加法原理。
vector<int> add(vector<int> &A, vector<int> &B) {
if (A.size() < B.size()) return add(B, A);
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i++) {
t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t) C.push_back(t);
return C;
}
int main() {
int m, n;
cin >> m >> n;
vector<int> A, B, C;
A.push_back(1);
B.push_back(1);
for (int i = 3; i <= n - m + 1; i++) {
C = add(A, B);
//对加数需要重新赋值
//A<---B
A.assign(B.begin(), B.end());
//B<---C
B.assign(C.begin(), C.end());
}
//倒序输出结果
for (int i = B.size() - 1; i >= 0; i--)printf("%d", B[i]);
return 0;
}