3c4124479f 2014-04-04 kinaba: #include <iostream> 3c4124479f 2014-04-04 kinaba: #include <sstream> 3c4124479f 2014-04-04 kinaba: #include <iomanip> 3c4124479f 2014-04-04 kinaba: #include <vector> 3c4124479f 2014-04-04 kinaba: #include <string> 3c4124479f 2014-04-04 kinaba: #include <map> 3c4124479f 2014-04-04 kinaba: #include <set> 3c4124479f 2014-04-04 kinaba: #include <algorithm> 3c4124479f 2014-04-04 kinaba: #include <numeric> 3c4124479f 2014-04-04 kinaba: #include <iterator> 3c4124479f 2014-04-04 kinaba: #include <functional> 3c4124479f 2014-04-04 kinaba: #include <complex> 3c4124479f 2014-04-04 kinaba: #include <queue> 3c4124479f 2014-04-04 kinaba: #include <stack> 3c4124479f 2014-04-04 kinaba: #include <cmath> 3c4124479f 2014-04-04 kinaba: #include <cassert> 3c4124479f 2014-04-04 kinaba: #include <tuple> 3c4124479f 2014-04-04 kinaba: using namespace std; 3c4124479f 2014-04-04 kinaba: typedef long long LL; 3c4124479f 2014-04-04 kinaba: typedef complex<double> CMP; 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: LL gcd(LL a, LL b) 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: while(a) 3c4124479f 2014-04-04 kinaba: swap(a, b%=a); 3c4124479f 2014-04-04 kinaba: return b; 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: LL lcm(LL a, LL b) 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: return a/gcd(a,b)*b; 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: class LCMSet { public: 3c4124479f 2014-04-04 kinaba: string equal(vector <int> A, vector <int> B) 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: return is_equal(A,B) ? "Equal" : "Not equal"; 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: bool is_equal(const vector<int>& A, const vector<int>& B) 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: return can_make(A, B) && can_make(B, A); 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: bool can_make(const vector<int>& A, const vector<int>& B) 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: return count_if(A.begin(), A.end(), [&](int a){return can_make(a, B);}) == A.size(); 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: bool can_make(int a, const vector<int>& B) 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: LL cur = 1; 3c4124479f 2014-04-04 kinaba: for(int b: B) { 3c4124479f 2014-04-04 kinaba: if(lcm(lcm(cur, b), a) == a) 3c4124479f 2014-04-04 kinaba: cur = lcm(cur, b); 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: return a == cur; 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: }; 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: // BEGIN CUT HERE 3c4124479f 2014-04-04 kinaba: #include <ctime> 3c4124479f 2014-04-04 kinaba: double start_time; string timer() 3c4124479f 2014-04-04 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 3c4124479f 2014-04-04 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 3c4124479f 2014-04-04 kinaba: { os << "{ "; 3c4124479f 2014-04-04 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 3c4124479f 2014-04-04 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 3c4124479f 2014-04-04 kinaba: void verify_case(const string& Expected, const string& Received) { 3c4124479f 2014-04-04 kinaba: bool ok = (Expected == Received); 3c4124479f 2014-04-04 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 3c4124479f 2014-04-04 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 3c4124479f 2014-04-04 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 3c4124479f 2014-04-04 kinaba: #define END verify_case(_, LCMSet().equal(A, B));} 3c4124479f 2014-04-04 kinaba: int main(){ 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: CASE(0) 3c4124479f 2014-04-04 kinaba: int A_[] = {2,3,4,12}; 3c4124479f 2014-04-04 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 3c4124479f 2014-04-04 kinaba: int B_[] = {2,3,4,6}; 3c4124479f 2014-04-04 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 3c4124479f 2014-04-04 kinaba: string _ = "Equal"; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(1) 3c4124479f 2014-04-04 kinaba: int A_[] = {4,9}; 3c4124479f 2014-04-04 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 3c4124479f 2014-04-04 kinaba: int B_[] = {6,36}; 3c4124479f 2014-04-04 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 3c4124479f 2014-04-04 kinaba: string _ = "Not equal"; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(2) 3c4124479f 2014-04-04 kinaba: int A_[] = {2,3,5,7,14,105}; 3c4124479f 2014-04-04 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 3c4124479f 2014-04-04 kinaba: int B_[] = {2,3,5,6,7,30,35}; 3c4124479f 2014-04-04 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 3c4124479f 2014-04-04 kinaba: string _ = "Equal"; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(3) 3c4124479f 2014-04-04 kinaba: int A_[] = {2,3,5,7,14,105}; 3c4124479f 2014-04-04 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 3c4124479f 2014-04-04 kinaba: int B_[] = {2,3,5,6,7,30,36}; 3c4124479f 2014-04-04 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 3c4124479f 2014-04-04 kinaba: string _ = "Not equal"; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(4) 3c4124479f 2014-04-04 kinaba: int A_[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; 3c4124479f 2014-04-04 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 3c4124479f 2014-04-04 kinaba: int B_[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; 3c4124479f 2014-04-04 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 3c4124479f 2014-04-04 kinaba: string _ = "Equal"; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(5) 3c4124479f 2014-04-04 kinaba: int A_[] = {999999999,1953125,512,1000000000}; 3c4124479f 2014-04-04 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 3c4124479f 2014-04-04 kinaba: int B_[] = {999999999,1953125,512}; 3c4124479f 2014-04-04 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 3c4124479f 2014-04-04 kinaba: string _ = "Equal"; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(6) 3c4124479f 2014-04-04 kinaba: int A_[] = {999999998,999999999,1000000000}; 3c4124479f 2014-04-04 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 3c4124479f 2014-04-04 kinaba: int B_[] = {999999999,1000000000}; 3c4124479f 2014-04-04 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 3c4124479f 2014-04-04 kinaba: string _ = "Not equal"; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: /* 3c4124479f 2014-04-04 kinaba: CASE(7) 3c4124479f 2014-04-04 kinaba: int A_[] = ; 3c4124479f 2014-04-04 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 3c4124479f 2014-04-04 kinaba: int B_[] = ; 3c4124479f 2014-04-04 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 3c4124479f 2014-04-04 kinaba: string _ = ; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(8) 3c4124479f 2014-04-04 kinaba: int A_[] = ; 3c4124479f 2014-04-04 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 3c4124479f 2014-04-04 kinaba: int B_[] = ; 3c4124479f 2014-04-04 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 3c4124479f 2014-04-04 kinaba: string _ = ; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: */ 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: // END CUT HERE