Artifact Content
Not logged in

Artifact f1735c1808f6c99405c521ecb3b6ff8f021f1733


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class LCMGCD { public:
	string isPossible(vector <int> x, int t)
	{
		pair<int,int> tt = crack(t);
		vector<pair<int,int>> ab;
		for(int xi: x) {
			pair<int, int> xx = crack(xi);
			ab.emplace_back(sign(xx.first, tt.first), sign(xx.second, tt.second));
		}
		return solve(ab) ? "Possible" : "Impossible";
	}

	pair<int,int> crack(int x)
	{
		int a=0, b=0;
		while(x%2==0) x/=2, a++;
		while(x%3==0) x/=3, b++;
		return make_pair(a,b);
	}

	int sign(int x, int t)
	{
		return x<t ? 0 : x==t ? 1 : 2;
	}

	// can we make (1,1) by max and min?
	bool solve(vector<pair<int,int>> x)
	{
		set<vector<int>> vis;
		vector<int> sig(7);
		vis.insert(sig);

		for(auto ab: x) {
			int s = ab.first*3 + ab.second;
			if(0<s&&s<8)
				if(sig[s-1]<5)
					sig[s-1]++;
		}
		goal = vector<int>(7); goal[3]=1;
		return rec(sig, vis);
	}

	vector<int> goal;
	bool rec(vector<int>& sig, set<vector<int>>& vis)
	{
		if(sig[3] >= 2)
			return true;
		if(sig == goal)
			return true;

		if(vis.count(sig))
			return false;
		vis.insert(sig);

		for(int a=1; a<8; ++a) if(sig[a-1])
		for(int b=a; b<8; ++b) if(sig[b-1]) {
			if(b==a && sig[a-1]==1) continue;
			sig[a-1]--;
			sig[b-1]--;
			int z = max(a/3,b/3)*3 + max(a%3,b%3);
			if(0<z && z<8) {
				sig[z-1]++;
				if(rec(sig, vis)) return true;
				sig[z-1]--;
			} else {
				if(rec(sig, vis)) return true;
			}
			z = min(a/3,b/3)*3 + min(a%3,b%3);
			if(0<z && z<8) {
				sig[z-1]++;
				if(rec(sig, vis)) return true;
				sig[z-1]--;
			} else {
				if(rec(sig, vis)) return true;
			}
			sig[a-1]++;
			sig[b-1]++;
		}
		return false;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, LCMGCD().isPossible(x, t));}
int main(){

CASE(0)
	int x_[] = {2,3};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 6; 
	string _ = "Possible"; 
END
CASE(1)
	int x_[] = {4,9};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 6; 
	string _ = "Impossible"; 
END
CASE(2)
	int x_[] = {6,12,24,18,36,72,54,108,216};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 36; 
	string _ = "Possible"; 
END
CASE(3)
	int x_[] = {6,27,81,729};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 6; 
	string _ = "Impossible"; 
END
CASE(4)
	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,
 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};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 1; 
	string _ = "Possible"; 
END
CASE(5)
	int x_[] = {72,16,16,16,16,16,27,27,27,27,27,27,27,27,27};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 72; 
	string _ = "Possible"; 
END
CASE(6)
	int x_[] = {100663296, 544195584, 229582512, 59049};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 60466176; 
	string _ = "Possible"; 
END
CASE(7)
	int x_[] = {2,4,8,16,32,64};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 256; 
	string _ = "Impossible"; 
END
CASE(8)
	int x_[] = {6,9};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 6; 
	string _ = "Impossible"; 
END
CASE(9)
	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,
	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};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 6; 
	string _ = "Impossible"; 
END
CASE(8)
	int x_[] = {6,6,9};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 6; 
	string _ = "Possible"; 
END
CASE(8)
	int x_[] = {9,18,12};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int t = 6; 
	string _ = "Possible"; 
END
}
// END CUT HERE