a7e27f416f 2014-08-09 kinaba: #include <iostream> a7e27f416f 2014-08-09 kinaba: #include <sstream> a7e27f416f 2014-08-09 kinaba: #include <iomanip> a7e27f416f 2014-08-09 kinaba: #include <vector> a7e27f416f 2014-08-09 kinaba: #include <string> a7e27f416f 2014-08-09 kinaba: #include <map> a7e27f416f 2014-08-09 kinaba: #include <set> a7e27f416f 2014-08-09 kinaba: #include <algorithm> a7e27f416f 2014-08-09 kinaba: #include <numeric> a7e27f416f 2014-08-09 kinaba: #include <iterator> a7e27f416f 2014-08-09 kinaba: #include <functional> a7e27f416f 2014-08-09 kinaba: #include <complex> a7e27f416f 2014-08-09 kinaba: #include <queue> a7e27f416f 2014-08-09 kinaba: #include <stack> a7e27f416f 2014-08-09 kinaba: #include <cmath> a7e27f416f 2014-08-09 kinaba: #include <cassert> a7e27f416f 2014-08-09 kinaba: #include <tuple> a7e27f416f 2014-08-09 kinaba: using namespace std; a7e27f416f 2014-08-09 kinaba: typedef long long LL; a7e27f416f 2014-08-09 kinaba: typedef complex<double> CMP; a7e27f416f 2014-08-09 kinaba: a7e27f416f 2014-08-09 kinaba: LL C(LL n, LL k) a7e27f416f 2014-08-09 kinaba: { a7e27f416f 2014-08-09 kinaba: LL v = 1; a7e27f416f 2014-08-09 kinaba: for(LL i=1; i<=k; ++i) a7e27f416f 2014-08-09 kinaba: v = v*(n-i+1)/i; a7e27f416f 2014-08-09 kinaba: return v; a7e27f416f 2014-08-09 kinaba: } a7e27f416f 2014-08-09 kinaba: a7e27f416f 2014-08-09 kinaba: class IntoTheMatrix { public: a7e27f416f 2014-08-09 kinaba: int takePills(int turns, int N) a7e27f416f 2014-08-09 kinaba: { a7e27f416f 2014-08-09 kinaba: for(int F=0 ;; ++F) a7e27f416f 2014-08-09 kinaba: if(best(F,turns)>=N) a7e27f416f 2014-08-09 kinaba: return F; a7e27f416f 2014-08-09 kinaba: } a7e27f416f 2014-08-09 kinaba: a7e27f416f 2014-08-09 kinaba: map<pair<int,int>,LL> memo; a7e27f416f 2014-08-09 kinaba: LL best(int F, int T) a7e27f416f 2014-08-09 kinaba: { a7e27f416f 2014-08-09 kinaba: if(T==0) a7e27f416f 2014-08-09 kinaba: return 1; a7e27f416f 2014-08-09 kinaba: a7e27f416f 2014-08-09 kinaba: pair<int,int> key(F,T); a7e27f416f 2014-08-09 kinaba: if(memo.count(key)) a7e27f416f 2014-08-09 kinaba: return memo[key]; a7e27f416f 2014-08-09 kinaba: a7e27f416f 2014-08-09 kinaba: LL sum = 0; a7e27f416f 2014-08-09 kinaba: for(int f=0; f<=F; ++f) a7e27f416f 2014-08-09 kinaba: sum += best(f, T-1)*C(F,f); a7e27f416f 2014-08-09 kinaba: return memo[key] = sum; a7e27f416f 2014-08-09 kinaba: } a7e27f416f 2014-08-09 kinaba: }; a7e27f416f 2014-08-09 kinaba: a7e27f416f 2014-08-09 kinaba: // BEGIN CUT HERE a7e27f416f 2014-08-09 kinaba: #include <ctime> a7e27f416f 2014-08-09 kinaba: double start_time; string timer() a7e27f416f 2014-08-09 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } a7e27f416f 2014-08-09 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) a7e27f416f 2014-08-09 kinaba: { os << "{ "; a7e27f416f 2014-08-09 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) a7e27f416f 2014-08-09 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } a7e27f416f 2014-08-09 kinaba: void verify_case(const int& Expected, const int& Received) { a7e27f416f 2014-08-09 kinaba: bool ok = (Expected == Received); a7e27f416f 2014-08-09 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; a7e27f416f 2014-08-09 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } a7e27f416f 2014-08-09 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); a7e27f416f 2014-08-09 kinaba: #define END verify_case(_, IntoTheMatrix().takePills(turns, N));} a7e27f416f 2014-08-09 kinaba: int main(){ a7e27f416f 2014-08-09 kinaba: a7e27f416f 2014-08-09 kinaba: CASE(0) a7e27f416f 2014-08-09 kinaba: int turns = 1; a7e27f416f 2014-08-09 kinaba: int N = 1; a7e27f416f 2014-08-09 kinaba: int _ = 0; a7e27f416f 2014-08-09 kinaba: END a7e27f416f 2014-08-09 kinaba: CASE(1) a7e27f416f 2014-08-09 kinaba: int turns = 2; a7e27f416f 2014-08-09 kinaba: int N = 10; a7e27f416f 2014-08-09 kinaba: int _ = 3; a7e27f416f 2014-08-09 kinaba: END a7e27f416f 2014-08-09 kinaba: CASE(2) a7e27f416f 2014-08-09 kinaba: int turns = 2; a7e27f416f 2014-08-09 kinaba: int N = 1000; a7e27f416f 2014-08-09 kinaba: int _ = 7; a7e27f416f 2014-08-09 kinaba: END a7e27f416f 2014-08-09 kinaba: CASE(3) a7e27f416f 2014-08-09 kinaba: int turns = 10; a7e27f416f 2014-08-09 kinaba: int N = 2; a7e27f416f 2014-08-09 kinaba: int _ = 1; a7e27f416f 2014-08-09 kinaba: END a7e27f416f 2014-08-09 kinaba: CASE(4) a7e27f416f 2014-08-09 kinaba: int turns = 4; a7e27f416f 2014-08-09 kinaba: int N = 50; a7e27f416f 2014-08-09 kinaba: int _ = 3; a7e27f416f 2014-08-09 kinaba: END a7e27f416f 2014-08-09 kinaba: CASE(5) a7e27f416f 2014-08-09 kinaba: int turns = 1; a7e27f416f 2014-08-09 kinaba: int N = 1000000; a7e27f416f 2014-08-09 kinaba: int _ = -1; a7e27f416f 2014-08-09 kinaba: END a7e27f416f 2014-08-09 kinaba: CASE(6) a7e27f416f 2014-08-09 kinaba: int turns = 10; a7e27f416f 2014-08-09 kinaba: int N = 1000000; a7e27f416f 2014-08-09 kinaba: int _ = -1; a7e27f416f 2014-08-09 kinaba: END a7e27f416f 2014-08-09 kinaba: } a7e27f416f 2014-08-09 kinaba: // END CUT HERE