斐波那契数列记忆化递归
This commit is contained in:
22
others/test3-1.cpp
Normal file
22
others/test3-1.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// 斐波那契数列记忆化递归
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
const ll p = 1e9 + 7;
|
||||
const int N = 1e5 + 3;
|
||||
ll dep[N];
|
||||
|
||||
ll fib(int a){
|
||||
if(a <= 2) return 1;
|
||||
if(dep[a] != -1) return dep[a] % p;
|
||||
return dep[a] = (fib(a-1) + fib(a-2)) % p;
|
||||
}
|
||||
int main(){
|
||||
fill(dep, dep + N, -1);
|
||||
int n; cin >> n;
|
||||
cout << fib(n) << endl;
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
1 1 2 3 5 8 13 21 34
|
||||
*/
|
||||
21
others/test3.cpp
Normal file
21
others/test3.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
// 斐波那契数列记忆化递归
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
#define int long long
|
||||
const int N = 1e5 + 3;
|
||||
int dep[N];
|
||||
|
||||
int fib(int a){
|
||||
if(a <= 2) return 1;
|
||||
if(dep[a] != -1) return dep[a];
|
||||
return dep[a] = fib(a-1) + fib(a-2);
|
||||
}
|
||||
signed main(){
|
||||
fill(dep, dep + N, -1);
|
||||
int n; cin >> n;
|
||||
cout << fib(n) << endl;
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
1 1 2 3 5 8 13 21 34
|
||||
*/
|
||||
Reference in New Issue
Block a user