9d33e96a5e 2012-03-31 kinaba: #include <iostream> 9d33e96a5e 2012-03-31 kinaba: #include <sstream> 9d33e96a5e 2012-03-31 kinaba: #include <iomanip> 9d33e96a5e 2012-03-31 kinaba: #include <vector> 9d33e96a5e 2012-03-31 kinaba: #include <string> 9d33e96a5e 2012-03-31 kinaba: #include <map> 9d33e96a5e 2012-03-31 kinaba: #include <set> 9d33e96a5e 2012-03-31 kinaba: #include <algorithm> 9d33e96a5e 2012-03-31 kinaba: #include <numeric> 9d33e96a5e 2012-03-31 kinaba: #include <iterator> 9d33e96a5e 2012-03-31 kinaba: #include <functional> 9d33e96a5e 2012-03-31 kinaba: #include <complex> 9d33e96a5e 2012-03-31 kinaba: #include <queue> 9d33e96a5e 2012-03-31 kinaba: #include <stack> 9d33e96a5e 2012-03-31 kinaba: #include <cmath> 9d33e96a5e 2012-03-31 kinaba: #include <cassert> 9d33e96a5e 2012-03-31 kinaba: #include <cstring> 9d33e96a5e 2012-03-31 kinaba: #ifdef __GNUC__ 9d33e96a5e 2012-03-31 kinaba: #include <ext/hash_map> 9d33e96a5e 2012-03-31 kinaba: #define unordered_map __gnu_cxx::hash_map 9d33e96a5e 2012-03-31 kinaba: #else 9d33e96a5e 2012-03-31 kinaba: #include <unordered_map> 9d33e96a5e 2012-03-31 kinaba: #endif 9d33e96a5e 2012-03-31 kinaba: using namespace std; 9d33e96a5e 2012-03-31 kinaba: typedef long long LL; 9d33e96a5e 2012-03-31 kinaba: typedef complex<double> CMP; 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: class KingXNewCurrency { public: 9d33e96a5e 2012-03-31 kinaba: int howMany(int A, int B, int X) 9d33e96a5e 2012-03-31 kinaba: { 9d33e96a5e 2012-03-31 kinaba: if(A%X==0 && B%X==0) 9d33e96a5e 2012-03-31 kinaba: return -1; 9d33e96a5e 2012-03-31 kinaba: int cnt = 0; 9d33e96a5e 2012-03-31 kinaba: for(int Y=1; Y<=max(A,B); ++Y) 9d33e96a5e 2012-03-31 kinaba: if( possible(A,B,X,Y) ) 9d33e96a5e 2012-03-31 kinaba: cnt++; 9d33e96a5e 2012-03-31 kinaba: return cnt; 9d33e96a5e 2012-03-31 kinaba: } 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: bool possible(int A, int B, int X, int Y) 9d33e96a5e 2012-03-31 kinaba: { 9d33e96a5e 2012-03-31 kinaba: bool Aok = false; 9d33e96a5e 2012-03-31 kinaba: for(int p=0; p*X<=A; ++p) 9d33e96a5e 2012-03-31 kinaba: if( (A-p*X)%Y == 0 ) 9d33e96a5e 2012-03-31 kinaba: Aok = true; 9d33e96a5e 2012-03-31 kinaba: bool Bok = false; 9d33e96a5e 2012-03-31 kinaba: for(int p=0; p*X<=B; ++p) 9d33e96a5e 2012-03-31 kinaba: if( (B-p*X)%Y == 0 ) 9d33e96a5e 2012-03-31 kinaba: Bok = true; 9d33e96a5e 2012-03-31 kinaba: return Aok&&Bok; 9d33e96a5e 2012-03-31 kinaba: } 9d33e96a5e 2012-03-31 kinaba: }; 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: // BEGIN CUT HERE 9d33e96a5e 2012-03-31 kinaba: #include <ctime> 9d33e96a5e 2012-03-31 kinaba: double start_time; string timer() 9d33e96a5e 2012-03-31 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 9d33e96a5e 2012-03-31 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 9d33e96a5e 2012-03-31 kinaba: { os << "{ "; 9d33e96a5e 2012-03-31 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 9d33e96a5e 2012-03-31 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 9d33e96a5e 2012-03-31 kinaba: void verify_case(const int& Expected, const int& Received) { 9d33e96a5e 2012-03-31 kinaba: bool ok = (Expected == Received); 9d33e96a5e 2012-03-31 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 9d33e96a5e 2012-03-31 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 9d33e96a5e 2012-03-31 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 9d33e96a5e 2012-03-31 kinaba: #define END verify_case(_, KingXNewCurrency().howMany(A, B, X));} 9d33e96a5e 2012-03-31 kinaba: int main(){ 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: CASE(0) 9d33e96a5e 2012-03-31 kinaba: int A = 5; 9d33e96a5e 2012-03-31 kinaba: int B = 8; 9d33e96a5e 2012-03-31 kinaba: int X = 5; 9d33e96a5e 2012-03-31 kinaba: int _ = 5; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: CASE(1) 9d33e96a5e 2012-03-31 kinaba: int A = 8; 9d33e96a5e 2012-03-31 kinaba: int B = 4; 9d33e96a5e 2012-03-31 kinaba: int X = 2; 9d33e96a5e 2012-03-31 kinaba: int _ = -1; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: CASE(2) 9d33e96a5e 2012-03-31 kinaba: int A = 7; 9d33e96a5e 2012-03-31 kinaba: int B = 4; 9d33e96a5e 2012-03-31 kinaba: int X = 13; 9d33e96a5e 2012-03-31 kinaba: int _ = 1; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: CASE(3) 9d33e96a5e 2012-03-31 kinaba: int A = 47; 9d33e96a5e 2012-03-31 kinaba: int B = 74; 9d33e96a5e 2012-03-31 kinaba: int X = 44; 9d33e96a5e 2012-03-31 kinaba: int _ = 2; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: CASE(4) 9d33e96a5e 2012-03-31 kinaba: int A = 128; 9d33e96a5e 2012-03-31 kinaba: int B = 96; 9d33e96a5e 2012-03-31 kinaba: int X = 3; 9d33e96a5e 2012-03-31 kinaba: int _ = 65; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: /* 9d33e96a5e 2012-03-31 kinaba: CASE(5) 9d33e96a5e 2012-03-31 kinaba: int A = ; 9d33e96a5e 2012-03-31 kinaba: int B = ; 9d33e96a5e 2012-03-31 kinaba: int X = ; 9d33e96a5e 2012-03-31 kinaba: int _ = ; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: CASE(6) 9d33e96a5e 2012-03-31 kinaba: int A = ; 9d33e96a5e 2012-03-31 kinaba: int B = ; 9d33e96a5e 2012-03-31 kinaba: int X = ; 9d33e96a5e 2012-03-31 kinaba: int _ = ; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: */ 9d33e96a5e 2012-03-31 kinaba: } 9d33e96a5e 2012-03-31 kinaba: // END CUT HERE