1475bf9d41 2012-10-31 kinaba: #include <iostream> 1475bf9d41 2012-10-31 kinaba: #include <sstream> 1475bf9d41 2012-10-31 kinaba: #include <iomanip> 1475bf9d41 2012-10-31 kinaba: #include <vector> 1475bf9d41 2012-10-31 kinaba: #include <string> 1475bf9d41 2012-10-31 kinaba: #include <map> 1475bf9d41 2012-10-31 kinaba: #include <set> 1475bf9d41 2012-10-31 kinaba: #include <algorithm> 1475bf9d41 2012-10-31 kinaba: #include <numeric> 1475bf9d41 2012-10-31 kinaba: #include <iterator> 1475bf9d41 2012-10-31 kinaba: #include <functional> 1475bf9d41 2012-10-31 kinaba: #include <complex> 1475bf9d41 2012-10-31 kinaba: #include <queue> 1475bf9d41 2012-10-31 kinaba: #include <stack> 1475bf9d41 2012-10-31 kinaba: #include <cmath> 1475bf9d41 2012-10-31 kinaba: #include <cassert> 1475bf9d41 2012-10-31 kinaba: using namespace std; 1475bf9d41 2012-10-31 kinaba: typedef long long LL; 1475bf9d41 2012-10-31 kinaba: typedef long double LD; 1475bf9d41 2012-10-31 kinaba: typedef complex<LD> CMP; 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: class HyperKnight { public: 1475bf9d41 2012-10-31 kinaba: long long countCells(int a_, int b_, int numRows, int numColumns, int K) 1475bf9d41 2012-10-31 kinaba: { 1475bf9d41 2012-10-31 kinaba: if(a_>b_) swap(a_,b_); 1475bf9d41 2012-10-31 kinaba: const LL A=a_, B=b_, W=numRows, H=numColumns; 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: LL xx[] = {0, A, B, W-B, W-A, W}; 1475bf9d41 2012-10-31 kinaba: LL yy[] = {0, A, B, H-B, H-A, H}; 1475bf9d41 2012-10-31 kinaba: LL dx[] = {+A, +A, -A, -A, +B, +B, -B, -B}; 1475bf9d41 2012-10-31 kinaba: LL dy[] = {+B, -B, +B, -B, +A, -A, +A, -A}; 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: LL cnt = 0; 1475bf9d41 2012-10-31 kinaba: for(int i=0; i+1<sizeof(xx)/sizeof(*xx); ++i) 1475bf9d41 2012-10-31 kinaba: for(int k=0; k+1<sizeof(yy)/sizeof(*yy); ++k) { 1475bf9d41 2012-10-31 kinaba: LL w = xx[i+1]-xx[i], x = xx[i]; 1475bf9d41 2012-10-31 kinaba: LL h = yy[k+1]-yy[k], y = yy[k]; 1475bf9d41 2012-10-31 kinaba: int av = 0; 1475bf9d41 2012-10-31 kinaba: for(int j=0; j<8; ++j) { 1475bf9d41 2012-10-31 kinaba: LL x2 = x + dx[j]; 1475bf9d41 2012-10-31 kinaba: LL y2 = y + dy[j]; 1475bf9d41 2012-10-31 kinaba: if(0<=x2 && x2<W && 0<=y2&&y2<H) 1475bf9d41 2012-10-31 kinaba: ++av; 1475bf9d41 2012-10-31 kinaba: } 1475bf9d41 2012-10-31 kinaba: if(av==K) 1475bf9d41 2012-10-31 kinaba: cnt += w*h; 1475bf9d41 2012-10-31 kinaba: } 1475bf9d41 2012-10-31 kinaba: return cnt; 1475bf9d41 2012-10-31 kinaba: } 1475bf9d41 2012-10-31 kinaba: }; 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: // BEGIN CUT HERE 1475bf9d41 2012-10-31 kinaba: #include <ctime> 1475bf9d41 2012-10-31 kinaba: double start_time; string timer() 1475bf9d41 2012-10-31 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 1475bf9d41 2012-10-31 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 1475bf9d41 2012-10-31 kinaba: { os << "{ "; 1475bf9d41 2012-10-31 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 1475bf9d41 2012-10-31 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 1475bf9d41 2012-10-31 kinaba: void verify_case(const long long& Expected, const long long& Received) { 1475bf9d41 2012-10-31 kinaba: bool ok = (Expected == Received); 1475bf9d41 2012-10-31 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 1475bf9d41 2012-10-31 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 1475bf9d41 2012-10-31 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 1475bf9d41 2012-10-31 kinaba: #define END verify_case(_, HyperKnight().countCells(a, b, numRows, numColumns, k));} 1475bf9d41 2012-10-31 kinaba: int main(){ 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: CASE(0) 1475bf9d41 2012-10-31 kinaba: int a = 2; 1475bf9d41 2012-10-31 kinaba: int b = 1; 1475bf9d41 2012-10-31 kinaba: int numRows = 8; 1475bf9d41 2012-10-31 kinaba: int numColumns = 8; 1475bf9d41 2012-10-31 kinaba: int k = 4; 1475bf9d41 2012-10-31 kinaba: long long _ = 20LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(1) 1475bf9d41 2012-10-31 kinaba: int a = 3; 1475bf9d41 2012-10-31 kinaba: int b = 2; 1475bf9d41 2012-10-31 kinaba: int numRows = 8; 1475bf9d41 2012-10-31 kinaba: int numColumns = 8; 1475bf9d41 2012-10-31 kinaba: int k = 2; 1475bf9d41 2012-10-31 kinaba: long long _ = 16LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(2) 1475bf9d41 2012-10-31 kinaba: int a = 1; 1475bf9d41 2012-10-31 kinaba: int b = 3; 1475bf9d41 2012-10-31 kinaba: int numRows = 7; 1475bf9d41 2012-10-31 kinaba: int numColumns = 11; 1475bf9d41 2012-10-31 kinaba: int k = 0; 1475bf9d41 2012-10-31 kinaba: long long _ = 0LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(3) 1475bf9d41 2012-10-31 kinaba: int a = 3; 1475bf9d41 2012-10-31 kinaba: int b = 2; 1475bf9d41 2012-10-31 kinaba: int numRows = 10; 1475bf9d41 2012-10-31 kinaba: int numColumns = 20; 1475bf9d41 2012-10-31 kinaba: int k = 8; 1475bf9d41 2012-10-31 kinaba: long long _ = 56LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(4) 1475bf9d41 2012-10-31 kinaba: int a = 1; 1475bf9d41 2012-10-31 kinaba: int b = 4; 1475bf9d41 2012-10-31 kinaba: int numRows = 100; 1475bf9d41 2012-10-31 kinaba: int numColumns = 10; 1475bf9d41 2012-10-31 kinaba: int k = 6; 1475bf9d41 2012-10-31 kinaba: long long _ = 564LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(5) 1475bf9d41 2012-10-31 kinaba: int a = 2; 1475bf9d41 2012-10-31 kinaba: int b = 3; 1475bf9d41 2012-10-31 kinaba: int numRows = 1000000000; 1475bf9d41 2012-10-31 kinaba: int numColumns = 1000000000; 1475bf9d41 2012-10-31 kinaba: int k = 8; 1475bf9d41 2012-10-31 kinaba: long long _ = 999999988000000036LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: /* 1475bf9d41 2012-10-31 kinaba: CASE(6) 1475bf9d41 2012-10-31 kinaba: int a = ; 1475bf9d41 2012-10-31 kinaba: int b = ; 1475bf9d41 2012-10-31 kinaba: int numRows = ; 1475bf9d41 2012-10-31 kinaba: int numColumns = ; 1475bf9d41 2012-10-31 kinaba: int k = ; 1475bf9d41 2012-10-31 kinaba: long long _ = LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(7) 1475bf9d41 2012-10-31 kinaba: int a = ; 1475bf9d41 2012-10-31 kinaba: int b = ; 1475bf9d41 2012-10-31 kinaba: int numRows = ; 1475bf9d41 2012-10-31 kinaba: int numColumns = ; 1475bf9d41 2012-10-31 kinaba: int k = ; 1475bf9d41 2012-10-31 kinaba: long long _ = LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: */ 1475bf9d41 2012-10-31 kinaba: } 1475bf9d41 2012-10-31 kinaba: // END CUT HERE