
#include <bits/stdc++.h>
using namespace std;
#define N 10007
#define M 1000000+10
int f[M];
int fab(int n){
f[1]=1;
f[2]=1;
for(int i=3;i<=n;i++){
f[i]=f[i-1]+f[i-2];
f[i]%=N;
}
return f[n];
}
int main(){
ios::sync_with_stdio(false);
int n;
while(cin>>n){
cout<<fab(n)<<endl;
}
return 0;
}

本文介绍了一种使用C++实现斐波那契数列的方法,通过动态规划算法来计算斐波那契数列的第n项,并在每次计算中取模以防止整数溢出。
1246

被折叠的 条评论
为什么被折叠?



