-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib.hpp
More file actions
56 lines (56 loc) · 1.17 KB
/
fib.hpp
File metadata and controls
56 lines (56 loc) · 1.17 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
#ifndef FIB_HPP
#define FIB_HPP
#include <iostream>
#include <type_traits>
#include <array>
#include <cassert>
using namespace std;
namespace Fib{
namespace Fibimpl{
const int max = 99;
std::array<long long int, max+1> cache;
int cache_max = 0;
template<int N>
struct FIB_impl{
static const long long int value = FIB_impl<N-1>::value + FIB_impl<N-2>::value;
static void warmup(){
cache[N] = value;
FIB_impl<N-1>::warmup();
}
static void foo(){
}
};
template<>
struct FIB_impl<0>{
static const long long int value = 1;
};
template<>
struct FIB_impl<1>{
static const long long int value = 1;
static void warmup(){
}
};
}
using namespace Fibimpl;
template<int N,class = typename std::enable_if<N<=max>::type >
struct FIB{
static const long long int value = FIB_impl<N-1>::value + FIB_impl<N-2>::value;
FIB(){
if(N>cache_max){
cache_max=N;
}
cache[0]=cache[1]=1;
cache[N]=value;
FIB_impl<N-1>::foo();
FIB_impl<N-1>::warmup();
}
};
long long int get(int n){
assert(n<=cache_max);
return cache[n];
}
namespace{
auto dummy = FIB<Fibimpl::max>();
}
}
#endif // FIB_HPP