ea210c966f 2013-11-20 kinaba: #include <iostream> ea210c966f 2013-11-20 kinaba: #include <sstream> ea210c966f 2013-11-20 kinaba: #include <iomanip> ea210c966f 2013-11-20 kinaba: #include <vector> ea210c966f 2013-11-20 kinaba: #include <string> ea210c966f 2013-11-20 kinaba: #include <map> ea210c966f 2013-11-20 kinaba: #include <set> ea210c966f 2013-11-20 kinaba: #include <algorithm> ea210c966f 2013-11-20 kinaba: #include <numeric> ea210c966f 2013-11-20 kinaba: #include <iterator> ea210c966f 2013-11-20 kinaba: #include <functional> ea210c966f 2013-11-20 kinaba: #include <complex> ea210c966f 2013-11-20 kinaba: #include <queue> ea210c966f 2013-11-20 kinaba: #include <stack> ea210c966f 2013-11-20 kinaba: #include <cmath> ea210c966f 2013-11-20 kinaba: #include <cassert> ea210c966f 2013-11-20 kinaba: #include <tuple> ea210c966f 2013-11-20 kinaba: using namespace std; ea210c966f 2013-11-20 kinaba: typedef long long LL; ea210c966f 2013-11-20 kinaba: typedef complex<double> CMP; ea210c966f 2013-11-20 kinaba: ea210c966f 2013-11-20 kinaba: class BitwiseAnd { public: ea210c966f 2013-11-20 kinaba: vector<long long> lexSmallest(vector<long long> subset, int N) ea210c966f 2013-11-20 kinaba: { ea210c966f 2013-11-20 kinaba: vector<LL> FAIL; ea210c966f 2013-11-20 kinaba: ea210c966f 2013-11-20 kinaba: // precheck ea210c966f 2013-11-20 kinaba: for(int i=0; i<subset.size(); ++i) ea210c966f 2013-11-20 kinaba: for(int j=i+1; j<subset.size(); ++j) ea210c966f 2013-11-20 kinaba: if((subset[i]&subset[j])==0) ea210c966f 2013-11-20 kinaba: return FAIL; ea210c966f 2013-11-20 kinaba: for(int i=0; i<subset.size(); ++i) ea210c966f 2013-11-20 kinaba: for(int j=i+1; j<subset.size(); ++j) ea210c966f 2013-11-20 kinaba: for(int k=j+1; k<subset.size(); ++k) ea210c966f 2013-11-20 kinaba: if((subset[i]&subset[j]&subset[k])!=0) ea210c966f 2013-11-20 kinaba: return FAIL; ea210c966f 2013-11-20 kinaba: ea210c966f 2013-11-20 kinaba: vector<LL> ans = subset; ea210c966f 2013-11-20 kinaba: ea210c966f 2013-11-20 kinaba: LL rest = (1LL<<60)-1; ea210c966f 2013-11-20 kinaba: vector<LL> leftbits = subset; ea210c966f 2013-11-20 kinaba: for(int i=0; i<subset.size(); ++i) ea210c966f 2013-11-20 kinaba: { ea210c966f 2013-11-20 kinaba: rest &= ~subset[i]; ea210c966f 2013-11-20 kinaba: for(int k=0; k<leftbits.size(); ++k) if(k!=i) ea210c966f 2013-11-20 kinaba: leftbits[k] &= ~ subset[i]; ea210c966f 2013-11-20 kinaba: } ea210c966f 2013-11-20 kinaba: ea210c966f 2013-11-20 kinaba: while(leftbits.size() < N) ea210c966f 2013-11-20 kinaba: { ea210c966f 2013-11-20 kinaba: LL v = 0; ea210c966f 2013-11-20 kinaba: for(int i=0; i<leftbits.size(); ++i) ea210c966f 2013-11-20 kinaba: { ea210c966f 2013-11-20 kinaba: if(leftbits[i] == 0) ea210c966f 2013-11-20 kinaba: return FAIL; ea210c966f 2013-11-20 kinaba: LL least = 1; ea210c966f 2013-11-20 kinaba: while((leftbits[i] & least)==0) ea210c966f 2013-11-20 kinaba: least <<= 1; ea210c966f 2013-11-20 kinaba: leftbits[i] &= ~least; ea210c966f 2013-11-20 kinaba: v |= least; ea210c966f 2013-11-20 kinaba: } ea210c966f 2013-11-20 kinaba: ea210c966f 2013-11-20 kinaba: LL restv = 0; ea210c966f 2013-11-20 kinaba: int needbits = (N - leftbits.size() - 1); ea210c966f 2013-11-20 kinaba: while(needbits--) { ea210c966f 2013-11-20 kinaba: if(rest == 0) ea210c966f 2013-11-20 kinaba: return FAIL; ea210c966f 2013-11-20 kinaba: LL least = 1; ea210c966f 2013-11-20 kinaba: while((rest & least)==0) ea210c966f 2013-11-20 kinaba: least <<= 1; ea210c966f 2013-11-20 kinaba: rest &= ~least; ea210c966f 2013-11-20 kinaba: restv |= least; ea210c966f 2013-11-20 kinaba: } ea210c966f 2013-11-20 kinaba: ea210c966f 2013-11-20 kinaba: leftbits.push_back(restv); ea210c966f 2013-11-20 kinaba: ans.push_back(v | restv); ea210c966f 2013-11-20 kinaba: } ea210c966f 2013-11-20 kinaba: ea210c966f 2013-11-20 kinaba: sort(ans.begin(), ans.end()); ea210c966f 2013-11-20 kinaba: return ans; ea210c966f 2013-11-20 kinaba: } ea210c966f 2013-11-20 kinaba: }; ea210c966f 2013-11-20 kinaba: ea210c966f 2013-11-20 kinaba: // BEGIN CUT HERE ea210c966f 2013-11-20 kinaba: #include <ctime> ea210c966f 2013-11-20 kinaba: double start_time; string timer() ea210c966f 2013-11-20 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } ea210c966f 2013-11-20 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) ea210c966f 2013-11-20 kinaba: { os << "{ "; ea210c966f 2013-11-20 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) ea210c966f 2013-11-20 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } ea210c966f 2013-11-20 kinaba: void verify_case(const vector<long long>& Expected, const vector<long long>& Received) { ea210c966f 2013-11-20 kinaba: bool ok = (Expected == Received); ea210c966f 2013-11-20 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; ea210c966f 2013-11-20 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } ea210c966f 2013-11-20 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); ea210c966f 2013-11-20 kinaba: #define END verify_case(_, BitwiseAnd().lexSmallest(subset, N));} ea210c966f 2013-11-20 kinaba: int main(){ ea210c966f 2013-11-20 kinaba: ea210c966f 2013-11-20 kinaba: CASE(0) ea210c966f 2013-11-20 kinaba: long long subset_[] = {14, 20}; ea210c966f 2013-11-20 kinaba: vector<long long> subset(subset_, subset_+sizeof(subset_)/sizeof(*subset_)); ea210c966f 2013-11-20 kinaba: int N = 3; ea210c966f 2013-11-20 kinaba: long long __[] = {14, 18, 20 }; ea210c966f 2013-11-20 kinaba: vector<long long> _(__, __+sizeof(__)/sizeof(*__)); ea210c966f 2013-11-20 kinaba: END ea210c966f 2013-11-20 kinaba: CASE(1) ea210c966f 2013-11-20 kinaba: long long subset_[] = {11, 17, 20}; ea210c966f 2013-11-20 kinaba: vector<long long> subset(subset_, subset_+sizeof(subset_)/sizeof(*subset_)); ea210c966f 2013-11-20 kinaba: int N = 4; ea210c966f 2013-11-20 kinaba: vector<long long> _; ea210c966f 2013-11-20 kinaba: END ea210c966f 2013-11-20 kinaba: CASE(2) ea210c966f 2013-11-20 kinaba: long long subset_[] = {99, 157}; ea210c966f 2013-11-20 kinaba: vector<long long> subset(subset_, subset_+sizeof(subset_)/sizeof(*subset_)); ea210c966f 2013-11-20 kinaba: int N = 4; ea210c966f 2013-11-20 kinaba: long long __[] = {99, 157, 262, 296 }; ea210c966f 2013-11-20 kinaba: vector<long long> _(__, __+sizeof(__)/sizeof(*__)); ea210c966f 2013-11-20 kinaba: END ea210c966f 2013-11-20 kinaba: CASE(3) ea210c966f 2013-11-20 kinaba: long long subset_[] = {1152921504606846975}; ea210c966f 2013-11-20 kinaba: vector<long long> subset(subset_, subset_+sizeof(subset_)/sizeof(*subset_)); ea210c966f 2013-11-20 kinaba: int N = 3; ea210c966f 2013-11-20 kinaba: vector<long long> _; ea210c966f 2013-11-20 kinaba: END ea210c966f 2013-11-20 kinaba: CASE(4) ea210c966f 2013-11-20 kinaba: vector<long long> subset; ea210c966f 2013-11-20 kinaba: int N = 5; ea210c966f 2013-11-20 kinaba: long long __[] = {15, 113, 402, 676, 840 }; ea210c966f 2013-11-20 kinaba: vector<long long> _(__, __+sizeof(__)/sizeof(*__)); ea210c966f 2013-11-20 kinaba: END ea210c966f 2013-11-20 kinaba: CASE(5) ea210c966f 2013-11-20 kinaba: long long subset_[] = {1, 3, 5, 7, 9, 11}; ea210c966f 2013-11-20 kinaba: vector<long long> subset(subset_, subset_+sizeof(subset_)/sizeof(*subset_)); ea210c966f 2013-11-20 kinaba: int N = 6; ea210c966f 2013-11-20 kinaba: vector<long long> _; ea210c966f 2013-11-20 kinaba: END ea210c966f 2013-11-20 kinaba: CASE(6) ea210c966f 2013-11-20 kinaba: long long subset_[] = {(1LL<<60)-1>>50<<50}; ea210c966f 2013-11-20 kinaba: vector<long long> subset(subset_, subset_+sizeof(subset_)/sizeof(*subset_)); ea210c966f 2013-11-20 kinaba: int N = 11; ea210c966f 2013-11-20 kinaba: vector<long long> _; ea210c966f 2013-11-20 kinaba: END ea210c966f 2013-11-20 kinaba: /* ea210c966f 2013-11-20 kinaba: CASE(7) ea210c966f 2013-11-20 kinaba: long long subset_[] = ; ea210c966f 2013-11-20 kinaba: vector<long long> subset(subset_, subset_+sizeof(subset_)/sizeof(*subset_)); ea210c966f 2013-11-20 kinaba: int N = ; ea210c966f 2013-11-20 kinaba: long long __[] = ; ea210c966f 2013-11-20 kinaba: vector<long long> _(__, __+sizeof(__)/sizeof(*__)); ea210c966f 2013-11-20 kinaba: END ea210c966f 2013-11-20 kinaba: */ ea210c966f 2013-11-20 kinaba: } ea210c966f 2013-11-20 kinaba: // END CUT HERE