Skip to content

Instantly share code, notes, and snippets.

@pbesra
Created January 1, 2017 02:12
Show Gist options
  • Save pbesra/7bc1d765958ccf3b4df150848813233e to your computer and use it in GitHub Desktop.
Save pbesra/7bc1d765958ccf3b4df150848813233e to your computer and use it in GitHub Desktop.
Dynamic programming, fibonacci number, memoization
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <cassert>
#include <map>
#include <bitset>
#include <vector>
#include <climits>
#define MAX 1000
typedef long long int lli;
using namespace std;
lli a[MAX];
lli getNum(lli x){
if(a[x]!=INT_MIN){
return a[x];
}
}
int output(int x){
lli f;
if(getNum(x)!=INT_MIN){
f=getNum(x);
}
if(x<=2){
f=1;
}
else{
f=output(x-1)+output(x-2);
}
a[x]=f;
return f;
}
int main(){
fill(a, a+MAX, INT_MIN);
lli n=3;
//cin >> n;
/*for(int i=0;i<n;i++){
lli t;
cin >> t;
cout << output(t) << endl;
} */
cout << output(2) << endl;
cout << output(3) << endl;
cout << output(5) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment