4b1cd240b3 2015-08-25 kinaba: #include <iostream> 4b1cd240b3 2015-08-25 kinaba: #include <sstream> 4b1cd240b3 2015-08-25 kinaba: #include <iomanip> 4b1cd240b3 2015-08-25 kinaba: #include <vector> 4b1cd240b3 2015-08-25 kinaba: #include <string> 4b1cd240b3 2015-08-25 kinaba: #include <map> 4b1cd240b3 2015-08-25 kinaba: #include <set> 4b1cd240b3 2015-08-25 kinaba: #include <algorithm> 4b1cd240b3 2015-08-25 kinaba: #include <numeric> 4b1cd240b3 2015-08-25 kinaba: #include <iterator> 4b1cd240b3 2015-08-25 kinaba: #include <functional> 4b1cd240b3 2015-08-25 kinaba: #include <complex> 4b1cd240b3 2015-08-25 kinaba: #include <queue> 4b1cd240b3 2015-08-25 kinaba: #include <stack> 4b1cd240b3 2015-08-25 kinaba: #include <cmath> 4b1cd240b3 2015-08-25 kinaba: #include <cassert> 4b1cd240b3 2015-08-25 kinaba: #include <tuple> 4b1cd240b3 2015-08-25 kinaba: using namespace std; 4b1cd240b3 2015-08-25 kinaba: typedef long long LL; 4b1cd240b3 2015-08-25 kinaba: typedef complex<double> CMP; 4b1cd240b3 2015-08-25 kinaba: 4b1cd240b3 2015-08-25 kinaba: class BallsInBoxes { public: 4b1cd240b3 2015-08-25 kinaba: long long maxTurns(long long N, long long K) 4b1cd240b3 2015-08-25 kinaba: { 4b1cd240b3 2015-08-25 kinaba: if(N < 2*K) 4b1cd240b3 2015-08-25 kinaba: return shortCase(N, K); 4b1cd240b3 2015-08-25 kinaba: if(N < 3*K) 4b1cd240b3 2015-08-25 kinaba: return 1 + shortCase((N+K)/2, K); 4b1cd240b3 2015-08-25 kinaba: if(N < 10*K) 4b1cd240b3 2015-08-25 kinaba: return 1 + maxTurns(N-K, K); 4b1cd240b3 2015-08-25 kinaba: return (N/K-4) + maxTurns(N - (N/K-4)*K, K); 4b1cd240b3 2015-08-25 kinaba: } 4b1cd240b3 2015-08-25 kinaba: 4b1cd240b3 2015-08-25 kinaba: LL shortCase(LL N, LL K) { 4b1cd240b3 2015-08-25 kinaba: // The case where N is small enough and all positions are possoible. 4b1cd240b3 2015-08-25 kinaba: return solve(N-K); 4b1cd240b3 2015-08-25 kinaba: } 4b1cd240b3 2015-08-25 kinaba: 4b1cd240b3 2015-08-25 kinaba: LL solve(LL N) { 4b1cd240b3 2015-08-25 kinaba: // Determine what number in [0,N] out of N boxes are filled. 4b1cd240b3 2015-08-25 kinaba: return N ? solve(N/2)+1 : 0; 4b1cd240b3 2015-08-25 kinaba: } 4b1cd240b3 2015-08-25 kinaba: }; 4b1cd240b3 2015-08-25 kinaba: 4b1cd240b3 2015-08-25 kinaba: // BEGIN CUT HERE 4b1cd240b3 2015-08-25 kinaba: #include <ctime> 4b1cd240b3 2015-08-25 kinaba: double start_time; string timer() 4b1cd240b3 2015-08-25 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4b1cd240b3 2015-08-25 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4b1cd240b3 2015-08-25 kinaba: { os << "{ "; 4b1cd240b3 2015-08-25 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4b1cd240b3 2015-08-25 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4b1cd240b3 2015-08-25 kinaba: void verify_case(const long long& Expected, const long long& Received) { 4b1cd240b3 2015-08-25 kinaba: bool ok = (Expected == Received); 4b1cd240b3 2015-08-25 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4b1cd240b3 2015-08-25 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4b1cd240b3 2015-08-25 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4b1cd240b3 2015-08-25 kinaba: #define END verify_case(_, BallsInBoxes().maxTurns(N, K));} 4b1cd240b3 2015-08-25 kinaba: int main(){ 4b1cd240b3 2015-08-25 kinaba: 4b1cd240b3 2015-08-25 kinaba: CASE(0) 4b1cd240b3 2015-08-25 kinaba: long long N = 10LL; 4b1cd240b3 2015-08-25 kinaba: long long K = 10LL; 4b1cd240b3 2015-08-25 kinaba: long long _ = 0LL; 4b1cd240b3 2015-08-25 kinaba: END 4b1cd240b3 2015-08-25 kinaba: CASE(1) 4b1cd240b3 2015-08-25 kinaba: long long N = 100LL; 4b1cd240b3 2015-08-25 kinaba: long long K = 1LL; 4b1cd240b3 2015-08-25 kinaba: long long _ = 99LL; 4b1cd240b3 2015-08-25 kinaba: END 4b1cd240b3 2015-08-25 kinaba: CASE(2) 4b1cd240b3 2015-08-25 kinaba: long long N = 1000LL; 4b1cd240b3 2015-08-25 kinaba: long long K = 999LL; 4b1cd240b3 2015-08-25 kinaba: long long _ = 1LL; 4b1cd240b3 2015-08-25 kinaba: END 4b1cd240b3 2015-08-25 kinaba: CASE(3) 4b1cd240b3 2015-08-25 kinaba: long long N = 10LL; 4b1cd240b3 2015-08-25 kinaba: long long K = 5LL; 4b1cd240b3 2015-08-25 kinaba: long long _ = 3LL; 4b1cd240b3 2015-08-25 kinaba: END 4b1cd240b3 2015-08-25 kinaba: CASE(4) 4b1cd240b3 2015-08-25 kinaba: long long N = 1234567898765LL; 4b1cd240b3 2015-08-25 kinaba: long long K = 658167578LL; 4b1cd240b3 2015-08-25 kinaba: long long _ = -1LL; 4b1cd240b3 2015-08-25 kinaba: END 4b1cd240b3 2015-08-25 kinaba: /* 4b1cd240b3 2015-08-25 kinaba: CASE(5) 4b1cd240b3 2015-08-25 kinaba: long long N = LL; 4b1cd240b3 2015-08-25 kinaba: long long K = LL; 4b1cd240b3 2015-08-25 kinaba: long long _ = LL; 4b1cd240b3 2015-08-25 kinaba: END 4b1cd240b3 2015-08-25 kinaba: */ 4b1cd240b3 2015-08-25 kinaba: } 4b1cd240b3 2015-08-25 kinaba: // END CUT HERE