-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfibonacci_dp.cpp
More file actions
57 lines (51 loc) · 1.12 KB
/
fibonacci_dp.cpp
File metadata and controls
57 lines (51 loc) · 1.12 KB
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
#include <bits/stdc++.h>
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
using namespace std;
typedef long long ll;
typedef long double ld;
#define endl "\n"
#define debug(args...) cout << (#args) << " = " << (args) << endl
#define MOD 1000000007
#define vi vector<int>
#define fl forward_list
#define pb push_back
#define pf push_front
#define read(st) getline(cin, st)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define N 1000005
bool ready[N];
ll value[N];
ll fibo(ll n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
else
{
if (ready[n - 1] && ready[n - 2])
return value[n - 1] + value[n - 2];
else
{
ready[n - 1] = true;
value[n - 1] = fibo(n - 1);
ready[n - 2] = true;
value[n - 2] = fibo(n - 2);
return value[n - 1] + value[n - 2];
}
}
}
int main()
{
fastio;
ll n;
ready[1] = true;
ready[2] = true;
value[1] = 1;
value[2] = 1;
cin >> n;
cout << "Fib(" << n << ") = " << fibo(n) << endl;
return 0;
}