e87d5512af 2011-06-03 kinaba: #include <iostream> e87d5512af 2011-06-03 kinaba: #include <sstream> e87d5512af 2011-06-03 kinaba: #include <iomanip> e87d5512af 2011-06-03 kinaba: #include <vector> e87d5512af 2011-06-03 kinaba: #include <string> e87d5512af 2011-06-03 kinaba: #include <map> e87d5512af 2011-06-03 kinaba: #include <set> e87d5512af 2011-06-03 kinaba: #include <algorithm> e87d5512af 2011-06-03 kinaba: #include <numeric> e87d5512af 2011-06-03 kinaba: #include <iterator> e87d5512af 2011-06-03 kinaba: #include <functional> e87d5512af 2011-06-03 kinaba: #include <complex> e87d5512af 2011-06-03 kinaba: #include <queue> e87d5512af 2011-06-03 kinaba: #include <stack> e87d5512af 2011-06-03 kinaba: #include <cmath> e87d5512af 2011-06-03 kinaba: #include <cassert> e87d5512af 2011-06-03 kinaba: #include <cstring> e87d5512af 2011-06-03 kinaba: using namespace std; e87d5512af 2011-06-03 kinaba: typedef long long LL; e87d5512af 2011-06-03 kinaba: typedef complex<double> CMP; e87d5512af 2011-06-03 kinaba: e87d5512af 2011-06-03 kinaba: class CubePacking { public: e87d5512af 2011-06-03 kinaba: int getMinimumVolume(int Ns, int Nb, int L) e87d5512af 2011-06-03 kinaba: { e87d5512af 2011-06-03 kinaba: int m = L*L*L*Nb + Ns; e87d5512af 2011-06-03 kinaba: int M = L*L*L*Nb + Ns + L*L; e87d5512af 2011-06-03 kinaba: for(int c=m; c<=M; ++c) // <M should be fine, but just in case... e87d5512af 2011-06-03 kinaba: { e87d5512af 2011-06-03 kinaba: for(int p=1; p*p*p<=c; ++p) if( c%p == 0 ) e87d5512af 2011-06-03 kinaba: for(int q=p; p*q*q<=c; ++q) if( c/p%q == 0 ) e87d5512af 2011-06-03 kinaba: { e87d5512af 2011-06-03 kinaba: int r = c/p/q; e87d5512af 2011-06-03 kinaba: int nL = (p/L)*(q/L)*(r/L); e87d5512af 2011-06-03 kinaba: if( Nb <= nL ) e87d5512af 2011-06-03 kinaba: return c; e87d5512af 2011-06-03 kinaba: } e87d5512af 2011-06-03 kinaba: } e87d5512af 2011-06-03 kinaba: assert(false); e87d5512af 2011-06-03 kinaba: return -1; e87d5512af 2011-06-03 kinaba: } e87d5512af 2011-06-03 kinaba: }; e87d5512af 2011-06-03 kinaba: e87d5512af 2011-06-03 kinaba: // BEGIN CUT HERE e87d5512af 2011-06-03 kinaba: #include <ctime> e87d5512af 2011-06-03 kinaba: double start_time; string timer() e87d5512af 2011-06-03 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } e87d5512af 2011-06-03 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) e87d5512af 2011-06-03 kinaba: { os << "{ "; e87d5512af 2011-06-03 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) e87d5512af 2011-06-03 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } e87d5512af 2011-06-03 kinaba: void verify_case(const int& Expected, const int& Received) { e87d5512af 2011-06-03 kinaba: bool ok = (Expected == Received); e87d5512af 2011-06-03 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; e87d5512af 2011-06-03 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } e87d5512af 2011-06-03 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); e87d5512af 2011-06-03 kinaba: #define END verify_case(_, CubePacking().getMinimumVolume(Ns, Nb, L));} e87d5512af 2011-06-03 kinaba: int main(){ e87d5512af 2011-06-03 kinaba: e87d5512af 2011-06-03 kinaba: CASE(0) e87d5512af 2011-06-03 kinaba: int Ns = 2; e87d5512af 2011-06-03 kinaba: int Nb = 2; e87d5512af 2011-06-03 kinaba: int L = 2; e87d5512af 2011-06-03 kinaba: int _ = 20; e87d5512af 2011-06-03 kinaba: END e87d5512af 2011-06-03 kinaba: CASE(1) e87d5512af 2011-06-03 kinaba: int Ns = 19; e87d5512af 2011-06-03 kinaba: int Nb = 1; e87d5512af 2011-06-03 kinaba: int L = 2; e87d5512af 2011-06-03 kinaba: int _ = 27; e87d5512af 2011-06-03 kinaba: END e87d5512af 2011-06-03 kinaba: CASE(2) e87d5512af 2011-06-03 kinaba: int Ns = 51; e87d5512af 2011-06-03 kinaba: int Nb = 7; e87d5512af 2011-06-03 kinaba: int L = 5; e87d5512af 2011-06-03 kinaba: int _ = 950; e87d5512af 2011-06-03 kinaba: END e87d5512af 2011-06-03 kinaba: CASE(3) e87d5512af 2011-06-03 kinaba: int Ns = 12345; e87d5512af 2011-06-03 kinaba: int Nb = 987; e87d5512af 2011-06-03 kinaba: int L = 10; e87d5512af 2011-06-03 kinaba: int _ = 999400; e87d5512af 2011-06-03 kinaba: END e87d5512af 2011-06-03 kinaba: CASE(4) e87d5512af 2011-06-03 kinaba: int Ns = 1000000000; e87d5512af 2011-06-03 kinaba: int Nb = 1000000; e87d5512af 2011-06-03 kinaba: int L = 10; e87d5512af 2011-06-03 kinaba: int _ = -1; e87d5512af 2011-06-03 kinaba: END e87d5512af 2011-06-03 kinaba: CASE(5) e87d5512af 2011-06-03 kinaba: int Ns = 1; e87d5512af 2011-06-03 kinaba: int Nb = 1; e87d5512af 2011-06-03 kinaba: int L = 1; e87d5512af 2011-06-03 kinaba: int _ = 2; e87d5512af 2011-06-03 kinaba: END e87d5512af 2011-06-03 kinaba: CASE(5) e87d5512af 2011-06-03 kinaba: int Ns = 1; e87d5512af 2011-06-03 kinaba: int Nb = 1; e87d5512af 2011-06-03 kinaba: int L = 2; e87d5512af 2011-06-03 kinaba: int _ = 12; e87d5512af 2011-06-03 kinaba: END e87d5512af 2011-06-03 kinaba: e87d5512af 2011-06-03 kinaba: } e87d5512af 2011-06-03 kinaba: // END CUT HERE