552e57677b 2013-06-14 kinaba: #include <iostream> 552e57677b 2013-06-14 kinaba: #include <sstream> 552e57677b 2013-06-14 kinaba: #include <iomanip> 552e57677b 2013-06-14 kinaba: #include <vector> 552e57677b 2013-06-14 kinaba: #include <string> 552e57677b 2013-06-14 kinaba: #include <map> 552e57677b 2013-06-14 kinaba: #include <set> 552e57677b 2013-06-14 kinaba: #include <algorithm> 552e57677b 2013-06-14 kinaba: #include <numeric> 552e57677b 2013-06-14 kinaba: #include <iterator> 552e57677b 2013-06-14 kinaba: #include <functional> 552e57677b 2013-06-14 kinaba: #include <complex> 552e57677b 2013-06-14 kinaba: #include <queue> 552e57677b 2013-06-14 kinaba: #include <stack> 552e57677b 2013-06-14 kinaba: #include <cmath> 552e57677b 2013-06-14 kinaba: #include <cassert> 552e57677b 2013-06-14 kinaba: using namespace std; 552e57677b 2013-06-14 kinaba: typedef long long LL; 552e57677b 2013-06-14 kinaba: typedef long double LD; 552e57677b 2013-06-14 kinaba: typedef complex<double> CMP; 552e57677b 2013-06-14 kinaba: 552e57677b 2013-06-14 kinaba: int bitcnt(int x) 552e57677b 2013-06-14 kinaba: { 552e57677b 2013-06-14 kinaba: int c = 0; 552e57677b 2013-06-14 kinaba: for(; x; x>>=1) 552e57677b 2013-06-14 kinaba: c += x&1; 552e57677b 2013-06-14 kinaba: return c; 552e57677b 2013-06-14 kinaba: } 552e57677b 2013-06-14 kinaba: 552e57677b 2013-06-14 kinaba: class YetAnotherANDProblem { public: 552e57677b 2013-06-14 kinaba: string test(vector <int> X, int K, vector <int> queries) 552e57677b 2013-06-14 kinaba: { 552e57677b 2013-06-14 kinaba: string s; 552e57677b 2013-06-14 kinaba: for(int i=0; i<queries.size(); ++i) 552e57677b 2013-06-14 kinaba: s += (can(X, K, queries[i]) ? "+" : "-"); 552e57677b 2013-06-14 kinaba: return s; 552e57677b 2013-06-14 kinaba: } 552e57677b 2013-06-14 kinaba: 552e57677b 2013-06-14 kinaba: bool can(const vector<int>& X, int K, int q) 552e57677b 2013-06-14 kinaba: { 552e57677b 2013-06-14 kinaba: int N = X.size(); 552e57677b 2013-06-14 kinaba: for(int mask=0; mask<(1<<N); ++mask) 552e57677b 2013-06-14 kinaba: { 552e57677b 2013-06-14 kinaba: int bc = bitcnt(mask); 552e57677b 2013-06-14 kinaba: if(K==1 && bc==2 || K>=2 && 3<=bc && bc<=1LL<<K) 552e57677b 2013-06-14 kinaba: { 552e57677b 2013-06-14 kinaba: int num = 0x7fffffff; 552e57677b 2013-06-14 kinaba: for(int i=0; i<N; ++i) 552e57677b 2013-06-14 kinaba: if(mask & (1<<i)) 552e57677b 2013-06-14 kinaba: num &= X[i]; 552e57677b 2013-06-14 kinaba: if(num==q) 552e57677b 2013-06-14 kinaba: return true; 552e57677b 2013-06-14 kinaba: } 552e57677b 2013-06-14 kinaba: } 552e57677b 2013-06-14 kinaba: return false; 552e57677b 2013-06-14 kinaba: } 552e57677b 2013-06-14 kinaba: }; 552e57677b 2013-06-14 kinaba: 552e57677b 2013-06-14 kinaba: // BEGIN CUT HERE 552e57677b 2013-06-14 kinaba: #include <ctime> 552e57677b 2013-06-14 kinaba: double start_time; string timer() 552e57677b 2013-06-14 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 552e57677b 2013-06-14 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 552e57677b 2013-06-14 kinaba: { os << "{ "; 552e57677b 2013-06-14 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 552e57677b 2013-06-14 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 552e57677b 2013-06-14 kinaba: void verify_case(const string& Expected, const string& Received) { 552e57677b 2013-06-14 kinaba: bool ok = (Expected == Received); 552e57677b 2013-06-14 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 552e57677b 2013-06-14 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 552e57677b 2013-06-14 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 552e57677b 2013-06-14 kinaba: #define END verify_case(_, YetAnotherANDProblem().test(X, K, queries));} 552e57677b 2013-06-14 kinaba: int main(){ 552e57677b 2013-06-14 kinaba: 552e57677b 2013-06-14 kinaba: CASE(0) 552e57677b 2013-06-14 kinaba: int X_[] = {10, 14, 7}; 552e57677b 2013-06-14 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 552e57677b 2013-06-14 kinaba: int K = 1; 552e57677b 2013-06-14 kinaba: int queries_[] = {10, 14, 2, 4}; 552e57677b 2013-06-14 kinaba: vector <int> queries(queries_, queries_+sizeof(queries_)/sizeof(*queries_)); 552e57677b 2013-06-14 kinaba: string _ = "+-+-"; 552e57677b 2013-06-14 kinaba: END 552e57677b 2013-06-14 kinaba: CASE(1) 552e57677b 2013-06-14 kinaba: int X_[] = {30, 29, 27, 23, 15}; 552e57677b 2013-06-14 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 552e57677b 2013-06-14 kinaba: int K = 2; 552e57677b 2013-06-14 kinaba: int queries_[] = {28, 9, 16, 0, 12}; 552e57677b 2013-06-14 kinaba: vector <int> queries(queries_, queries_+sizeof(queries_)/sizeof(*queries_)); 552e57677b 2013-06-14 kinaba: string _ = "-++-+"; 552e57677b 2013-06-14 kinaba: END 552e57677b 2013-06-14 kinaba: CASE(2) 552e57677b 2013-06-14 kinaba: int X_[] = {5, 5, 5, 5, 5}; 552e57677b 2013-06-14 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 552e57677b 2013-06-14 kinaba: int K = 50; 552e57677b 2013-06-14 kinaba: int queries_[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 552e57677b 2013-06-14 kinaba: vector <int> queries(queries_, queries_+sizeof(queries_)/sizeof(*queries_)); 552e57677b 2013-06-14 kinaba: string _ = "-----+----"; 552e57677b 2013-06-14 kinaba: END 552e57677b 2013-06-14 kinaba: CASE(3) 552e57677b 2013-06-14 kinaba: int X_[] = {71258, 1257, 2592588, 2885851, 999928, 123456, 59881, 99999}; 552e57677b 2013-06-14 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 552e57677b 2013-06-14 kinaba: int K = 4; 552e57677b 2013-06-14 kinaba: int queries_[] = {72, 91, 10, 0, 27, 64, 8}; 552e57677b 2013-06-14 kinaba: vector <int> queries(queries_, queries_+sizeof(queries_)/sizeof(*queries_)); 552e57677b 2013-06-14 kinaba: string _ = "+--+-++"; 552e57677b 2013-06-14 kinaba: END 552e57677b 2013-06-14 kinaba: /* 552e57677b 2013-06-14 kinaba: CASE(4) 552e57677b 2013-06-14 kinaba: int X_[] = ; 552e57677b 2013-06-14 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 552e57677b 2013-06-14 kinaba: int K = ; 552e57677b 2013-06-14 kinaba: int queries_[] = ; 552e57677b 2013-06-14 kinaba: vector <int> queries(queries_, queries_+sizeof(queries_)/sizeof(*queries_)); 552e57677b 2013-06-14 kinaba: string _ = ; 552e57677b 2013-06-14 kinaba: END 552e57677b 2013-06-14 kinaba: CASE(5) 552e57677b 2013-06-14 kinaba: int X_[] = ; 552e57677b 2013-06-14 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 552e57677b 2013-06-14 kinaba: int K = ; 552e57677b 2013-06-14 kinaba: int queries_[] = ; 552e57677b 2013-06-14 kinaba: vector <int> queries(queries_, queries_+sizeof(queries_)/sizeof(*queries_)); 552e57677b 2013-06-14 kinaba: string _ = ; 552e57677b 2013-06-14 kinaba: END 552e57677b 2013-06-14 kinaba: */ 552e57677b 2013-06-14 kinaba: } 552e57677b 2013-06-14 kinaba: // END CUT HERE