2827f87024 2014-01-21 kinaba: #include <iostream> 2827f87024 2014-01-21 kinaba: #include <sstream> 2827f87024 2014-01-21 kinaba: #include <iomanip> 2827f87024 2014-01-21 kinaba: #include <vector> 2827f87024 2014-01-21 kinaba: #include <string> 2827f87024 2014-01-21 kinaba: #include <map> 2827f87024 2014-01-21 kinaba: #include <set> 2827f87024 2014-01-21 kinaba: #include <algorithm> 2827f87024 2014-01-21 kinaba: #include <numeric> 2827f87024 2014-01-21 kinaba: #include <iterator> 2827f87024 2014-01-21 kinaba: #include <functional> 2827f87024 2014-01-21 kinaba: #include <complex> 2827f87024 2014-01-21 kinaba: #include <queue> 2827f87024 2014-01-21 kinaba: #include <stack> 2827f87024 2014-01-21 kinaba: #include <cmath> 2827f87024 2014-01-21 kinaba: #include <cassert> 2827f87024 2014-01-21 kinaba: #include <tuple> 2827f87024 2014-01-21 kinaba: using namespace std; 2827f87024 2014-01-21 kinaba: typedef long long LL; 2827f87024 2014-01-21 kinaba: typedef complex<double> CMP; 2827f87024 2014-01-21 kinaba: 2827f87024 2014-01-21 kinaba: class PowerOfThree { public: 2827f87024 2014-01-21 kinaba: string ableToGet(int x_, int y_) 2827f87024 2014-01-21 kinaba: { 2827f87024 2014-01-21 kinaba: LL x=x_, y=y_; 2827f87024 2014-01-21 kinaba: for(LL tk=1;; tk*=3) 2827f87024 2014-01-21 kinaba: { 2827f87024 2014-01-21 kinaba: if(x==0 && y==0) 2827f87024 2014-01-21 kinaba: break; 2827f87024 2014-01-21 kinaba: LL xr = ((x/tk)%3+3)%3; 2827f87024 2014-01-21 kinaba: LL yr = ((y/tk)%3+3)%3; 2827f87024 2014-01-21 kinaba: if(xr==1 && yr==0) x-=tk; 2827f87024 2014-01-21 kinaba: else if(xr==2 && yr==0) x+=tk; 2827f87024 2014-01-21 kinaba: else if(xr==0 && yr==1) y-=tk; 2827f87024 2014-01-21 kinaba: else if(xr==0 && yr==2) y+=tk; 2827f87024 2014-01-21 kinaba: else return "Impossible"; 2827f87024 2014-01-21 kinaba: } 2827f87024 2014-01-21 kinaba: return "Possible"; 2827f87024 2014-01-21 kinaba: } 2827f87024 2014-01-21 kinaba: }; 2827f87024 2014-01-21 kinaba: 2827f87024 2014-01-21 kinaba: // BEGIN CUT HERE 2827f87024 2014-01-21 kinaba: #include <ctime> 2827f87024 2014-01-21 kinaba: double start_time; string timer() 2827f87024 2014-01-21 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 2827f87024 2014-01-21 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 2827f87024 2014-01-21 kinaba: { os << "{ "; 2827f87024 2014-01-21 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 2827f87024 2014-01-21 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 2827f87024 2014-01-21 kinaba: void verify_case(const string& Expected, const string& Received) { 2827f87024 2014-01-21 kinaba: bool ok = (Expected == Received); 2827f87024 2014-01-21 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 2827f87024 2014-01-21 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 2827f87024 2014-01-21 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 2827f87024 2014-01-21 kinaba: #define END verify_case(_, PowerOfThree().ableToGet(x, y));} 2827f87024 2014-01-21 kinaba: int main(){ 2827f87024 2014-01-21 kinaba: 2827f87024 2014-01-21 kinaba: CASE(0) 2827f87024 2014-01-21 kinaba: int x = 1; 2827f87024 2014-01-21 kinaba: int y = 3; 2827f87024 2014-01-21 kinaba: string _ = "Possible"; 2827f87024 2014-01-21 kinaba: END 2827f87024 2014-01-21 kinaba: CASE(1) 2827f87024 2014-01-21 kinaba: int x = 0; 2827f87024 2014-01-21 kinaba: int y = 2; 2827f87024 2014-01-21 kinaba: string _ = "Possible"; 2827f87024 2014-01-21 kinaba: END 2827f87024 2014-01-21 kinaba: CASE(2) 2827f87024 2014-01-21 kinaba: int x = 1; 2827f87024 2014-01-21 kinaba: int y = 9; 2827f87024 2014-01-21 kinaba: string _ = "Impossible"; 2827f87024 2014-01-21 kinaba: END 2827f87024 2014-01-21 kinaba: CASE(3) 2827f87024 2014-01-21 kinaba: int x = 3; 2827f87024 2014-01-21 kinaba: int y = 0; 2827f87024 2014-01-21 kinaba: string _ = "Impossible"; 2827f87024 2014-01-21 kinaba: END 2827f87024 2014-01-21 kinaba: CASE(4) 2827f87024 2014-01-21 kinaba: int x = 1; 2827f87024 2014-01-21 kinaba: int y = 1; 2827f87024 2014-01-21 kinaba: string _ = "Impossible"; 2827f87024 2014-01-21 kinaba: END 2827f87024 2014-01-21 kinaba: CASE(5) 2827f87024 2014-01-21 kinaba: int x = -6890; 2827f87024 2014-01-21 kinaba: int y = 18252; 2827f87024 2014-01-21 kinaba: string _ = "Possible"; 2827f87024 2014-01-21 kinaba: END 2827f87024 2014-01-21 kinaba: CASE(6) 2827f87024 2014-01-21 kinaba: int x = 1000000000; 2827f87024 2014-01-21 kinaba: int y = -1000000000; 2827f87024 2014-01-21 kinaba: string _ = "Impossible"; 2827f87024 2014-01-21 kinaba: END 2827f87024 2014-01-21 kinaba: CASE(7) 2827f87024 2014-01-21 kinaba: int x = 0; 2827f87024 2014-01-21 kinaba: int y = 0; 2827f87024 2014-01-21 kinaba: string _ = "Possible"; 2827f87024 2014-01-21 kinaba: END 2827f87024 2014-01-21 kinaba: CASE(8) 2827f87024 2014-01-21 kinaba: int x = 8; 2827f87024 2014-01-21 kinaba: int y = 3; 2827f87024 2014-01-21 kinaba: string _ = "P"; 2827f87024 2014-01-21 kinaba: END 2827f87024 2014-01-21 kinaba: CASE(9) 2827f87024 2014-01-21 kinaba: // -3^0 - 3^1 - ... - 3^18 + 3^19 2827f87024 2014-01-21 kinaba: // NOTE: 3^19 = 1162261467 > 10^9 2827f87024 2014-01-21 kinaba: int x = 581130734; 2827f87024 2014-01-21 kinaba: int y = 0; 2827f87024 2014-01-21 kinaba: string _ = "P"; 2827f87024 2014-01-21 kinaba: END 2827f87024 2014-01-21 kinaba: } 2827f87024 2014-01-21 kinaba: // END CUT HERE