Last active
August 29, 2015 14:16
-
-
Save MiSawa/2925be30b5c1912304bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
template<typename Res, typename ...Args> struct _memoized{//{{{ | |
function<Res(Args...)> f; | |
map<tuple<Args...>, Res> memo; | |
_memoized(function<Res(Args...)> &f) : f(f){} | |
Res operator()(Args ...args){ | |
const auto &key = make_tuple(args...); | |
if(memo.count(key)) return memo[key]; | |
return memo[key] = f(args...); | |
} | |
};//}}} | |
template<typename Res, typename ...Args> | |
function<Res(Args...)> memoize(function<Res(Args...)> &f){ f = _memoized<Res, Args...>(f); return f; } | |
bool solve(){ | |
function<int(int)> f = [&](int n) -> int{ return n <= 1 ? n : f(n-1) + f(n-2); }; | |
memoize(f); | |
cout << f(100) << endl; | |
return true; | |
} | |
signed main(){ | |
cin.tie(nullptr); | |
ios_base::sync_with_stdio(false); | |
cout << std::fixed << std::setprecision(10); | |
solve(); | |
return 0; | |
} | |
// vim:set foldmethod=marker commentstring=//%s: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment