f79e63c2fd 2016-05-26 kinaba: #include <iostream> f79e63c2fd 2016-05-26 kinaba: #include <sstream> f79e63c2fd 2016-05-26 kinaba: #include <iomanip> f79e63c2fd 2016-05-26 kinaba: #include <vector> f79e63c2fd 2016-05-26 kinaba: #include <string> f79e63c2fd 2016-05-26 kinaba: #include <map> f79e63c2fd 2016-05-26 kinaba: #include <set> f79e63c2fd 2016-05-26 kinaba: #include <algorithm> f79e63c2fd 2016-05-26 kinaba: #include <numeric> f79e63c2fd 2016-05-26 kinaba: #include <iterator> f79e63c2fd 2016-05-26 kinaba: #include <functional> f79e63c2fd 2016-05-26 kinaba: #include <complex> f79e63c2fd 2016-05-26 kinaba: #include <queue> f79e63c2fd 2016-05-26 kinaba: #include <stack> f79e63c2fd 2016-05-26 kinaba: #include <cmath> f79e63c2fd 2016-05-26 kinaba: #include <cassert> f79e63c2fd 2016-05-26 kinaba: #include <tuple> f79e63c2fd 2016-05-26 kinaba: using namespace std; f79e63c2fd 2016-05-26 kinaba: typedef long long LL; f79e63c2fd 2016-05-26 kinaba: typedef complex<double> CMP; f79e63c2fd 2016-05-26 kinaba: f79e63c2fd 2016-05-26 kinaba: class LCMGCD { public: f79e63c2fd 2016-05-26 kinaba: string isPossible(vector <int> x, int t) f79e63c2fd 2016-05-26 kinaba: { f79e63c2fd 2016-05-26 kinaba: pair<int,int> tt = crack(t); f79e63c2fd 2016-05-26 kinaba: vector<pair<int,int>> ab; f79e63c2fd 2016-05-26 kinaba: for(int xi: x) { f79e63c2fd 2016-05-26 kinaba: pair<int, int> xx = crack(xi); f79e63c2fd 2016-05-26 kinaba: ab.emplace_back(sign(xx.first, tt.first), sign(xx.second, tt.second)); f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: return solve(ab) ? "Possible" : "Impossible"; f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: f79e63c2fd 2016-05-26 kinaba: pair<int,int> crack(int x) f79e63c2fd 2016-05-26 kinaba: { f79e63c2fd 2016-05-26 kinaba: int a=0, b=0; f79e63c2fd 2016-05-26 kinaba: while(x%2==0) x/=2, a++; f79e63c2fd 2016-05-26 kinaba: while(x%3==0) x/=3, b++; f79e63c2fd 2016-05-26 kinaba: return make_pair(a,b); f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: f79e63c2fd 2016-05-26 kinaba: int sign(int x, int t) f79e63c2fd 2016-05-26 kinaba: { f79e63c2fd 2016-05-26 kinaba: return x<t ? 0 : x==t ? 1 : 2; f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: f79e63c2fd 2016-05-26 kinaba: // can we make (1,1) by max and min? f79e63c2fd 2016-05-26 kinaba: bool solve(vector<pair<int,int>> x) f79e63c2fd 2016-05-26 kinaba: { f79e63c2fd 2016-05-26 kinaba: set<vector<int>> vis; f79e63c2fd 2016-05-26 kinaba: vector<int> sig(7); f79e63c2fd 2016-05-26 kinaba: vis.insert(sig); f79e63c2fd 2016-05-26 kinaba: f79e63c2fd 2016-05-26 kinaba: for(auto ab: x) { f79e63c2fd 2016-05-26 kinaba: int s = ab.first*3 + ab.second; f79e63c2fd 2016-05-26 kinaba: if(0<s&&s<8) f79e63c2fd 2016-05-26 kinaba: if(sig[s-1]<5) f79e63c2fd 2016-05-26 kinaba: sig[s-1]++; f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: goal = vector<int>(7); goal[3]=1; f79e63c2fd 2016-05-26 kinaba: return rec(sig, vis); f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: f79e63c2fd 2016-05-26 kinaba: vector<int> goal; f79e63c2fd 2016-05-26 kinaba: bool rec(vector<int>& sig, set<vector<int>>& vis) f79e63c2fd 2016-05-26 kinaba: { f79e63c2fd 2016-05-26 kinaba: if(sig[3] >= 2) f79e63c2fd 2016-05-26 kinaba: return true; f79e63c2fd 2016-05-26 kinaba: if(sig == goal) f79e63c2fd 2016-05-26 kinaba: return true; f79e63c2fd 2016-05-26 kinaba: f79e63c2fd 2016-05-26 kinaba: if(vis.count(sig)) f79e63c2fd 2016-05-26 kinaba: return false; f79e63c2fd 2016-05-26 kinaba: vis.insert(sig); f79e63c2fd 2016-05-26 kinaba: f79e63c2fd 2016-05-26 kinaba: for(int a=1; a<8; ++a) if(sig[a-1]) f79e63c2fd 2016-05-26 kinaba: for(int b=a; b<8; ++b) if(sig[b-1]) { f79e63c2fd 2016-05-26 kinaba: if(b==a && sig[a-1]==1) continue; f79e63c2fd 2016-05-26 kinaba: sig[a-1]--; f79e63c2fd 2016-05-26 kinaba: sig[b-1]--; f79e63c2fd 2016-05-26 kinaba: int z = max(a/3,b/3)*3 + max(a%3,b%3); f79e63c2fd 2016-05-26 kinaba: if(0<z && z<8) { f79e63c2fd 2016-05-26 kinaba: sig[z-1]++; f79e63c2fd 2016-05-26 kinaba: if(rec(sig, vis)) return true; f79e63c2fd 2016-05-26 kinaba: sig[z-1]--; f79e63c2fd 2016-05-26 kinaba: } else { f79e63c2fd 2016-05-26 kinaba: if(rec(sig, vis)) return true; f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: z = min(a/3,b/3)*3 + min(a%3,b%3); f79e63c2fd 2016-05-26 kinaba: if(0<z && z<8) { f79e63c2fd 2016-05-26 kinaba: sig[z-1]++; f79e63c2fd 2016-05-26 kinaba: if(rec(sig, vis)) return true; f79e63c2fd 2016-05-26 kinaba: sig[z-1]--; f79e63c2fd 2016-05-26 kinaba: } else { f79e63c2fd 2016-05-26 kinaba: if(rec(sig, vis)) return true; f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: sig[a-1]++; f79e63c2fd 2016-05-26 kinaba: sig[b-1]++; f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: return false; f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: }; f79e63c2fd 2016-05-26 kinaba: f79e63c2fd 2016-05-26 kinaba: // BEGIN CUT HERE f79e63c2fd 2016-05-26 kinaba: #include <ctime> f79e63c2fd 2016-05-26 kinaba: double start_time; string timer() f79e63c2fd 2016-05-26 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f79e63c2fd 2016-05-26 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f79e63c2fd 2016-05-26 kinaba: { os << "{ "; f79e63c2fd 2016-05-26 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f79e63c2fd 2016-05-26 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f79e63c2fd 2016-05-26 kinaba: void verify_case(const string& Expected, const string& Received) { f79e63c2fd 2016-05-26 kinaba: bool ok = (Expected == Received); f79e63c2fd 2016-05-26 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f79e63c2fd 2016-05-26 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } f79e63c2fd 2016-05-26 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f79e63c2fd 2016-05-26 kinaba: #define END verify_case(_, LCMGCD().isPossible(x, t));} f79e63c2fd 2016-05-26 kinaba: int main(){ f79e63c2fd 2016-05-26 kinaba: f79e63c2fd 2016-05-26 kinaba: CASE(0) f79e63c2fd 2016-05-26 kinaba: int x_[] = {2,3}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 6; f79e63c2fd 2016-05-26 kinaba: string _ = "Possible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(1) f79e63c2fd 2016-05-26 kinaba: int x_[] = {4,9}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 6; f79e63c2fd 2016-05-26 kinaba: string _ = "Impossible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(2) f79e63c2fd 2016-05-26 kinaba: int x_[] = {6,12,24,18,36,72,54,108,216}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 36; f79e63c2fd 2016-05-26 kinaba: string _ = "Possible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(3) f79e63c2fd 2016-05-26 kinaba: int x_[] = {6,27,81,729}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 6; f79e63c2fd 2016-05-26 kinaba: string _ = "Impossible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(4) f79e63c2fd 2016-05-26 kinaba: int x_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, f79e63c2fd 2016-05-26 kinaba: 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 1; f79e63c2fd 2016-05-26 kinaba: string _ = "Possible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(5) f79e63c2fd 2016-05-26 kinaba: int x_[] = {72,16,16,16,16,16,27,27,27,27,27,27,27,27,27}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 72; f79e63c2fd 2016-05-26 kinaba: string _ = "Possible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(6) f79e63c2fd 2016-05-26 kinaba: int x_[] = {100663296, 544195584, 229582512, 59049}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 60466176; f79e63c2fd 2016-05-26 kinaba: string _ = "Possible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(7) f79e63c2fd 2016-05-26 kinaba: int x_[] = {2,4,8,16,32,64}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 256; f79e63c2fd 2016-05-26 kinaba: string _ = "Impossible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(8) f79e63c2fd 2016-05-26 kinaba: int x_[] = {6,9}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 6; f79e63c2fd 2016-05-26 kinaba: string _ = "Impossible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(9) f79e63c2fd 2016-05-26 kinaba: int x_[] = {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, f79e63c2fd 2016-05-26 kinaba: 18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 6; f79e63c2fd 2016-05-26 kinaba: string _ = "Impossible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(8) f79e63c2fd 2016-05-26 kinaba: int x_[] = {6,6,9}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 6; f79e63c2fd 2016-05-26 kinaba: string _ = "Possible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: CASE(8) f79e63c2fd 2016-05-26 kinaba: int x_[] = {9,18,12}; f79e63c2fd 2016-05-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); f79e63c2fd 2016-05-26 kinaba: int t = 6; f79e63c2fd 2016-05-26 kinaba: string _ = "Possible"; f79e63c2fd 2016-05-26 kinaba: END f79e63c2fd 2016-05-26 kinaba: } f79e63c2fd 2016-05-26 kinaba: // END CUT HERE