f0a8c39350 2013-09-16 kinaba: #include <iostream> f0a8c39350 2013-09-16 kinaba: #include <sstream> f0a8c39350 2013-09-16 kinaba: #include <iomanip> f0a8c39350 2013-09-16 kinaba: #include <vector> f0a8c39350 2013-09-16 kinaba: #include <string> f0a8c39350 2013-09-16 kinaba: #include <map> f0a8c39350 2013-09-16 kinaba: #include <set> f0a8c39350 2013-09-16 kinaba: #include <algorithm> f0a8c39350 2013-09-16 kinaba: #include <numeric> f0a8c39350 2013-09-16 kinaba: #include <iterator> f0a8c39350 2013-09-16 kinaba: #include <functional> f0a8c39350 2013-09-16 kinaba: #include <complex> f0a8c39350 2013-09-16 kinaba: #include <queue> f0a8c39350 2013-09-16 kinaba: #include <stack> f0a8c39350 2013-09-16 kinaba: #include <cmath> f0a8c39350 2013-09-16 kinaba: #include <cassert> f0a8c39350 2013-09-16 kinaba: #include <tuple> f0a8c39350 2013-09-16 kinaba: using namespace std; f0a8c39350 2013-09-16 kinaba: typedef long long LL; f0a8c39350 2013-09-16 kinaba: typedef complex<double> CMP; f0a8c39350 2013-09-16 kinaba: f0a8c39350 2013-09-16 kinaba: class XorCards { public: f0a8c39350 2013-09-16 kinaba: long long numberOfWays(vector<long long> number, long long limit) f0a8c39350 2013-09-16 kinaba: { f0a8c39350 2013-09-16 kinaba: LL total = 0; f0a8c39350 2013-09-16 kinaba: LL req = 0; f0a8c39350 2013-09-16 kinaba: for(int k=62; k>=0; --k) f0a8c39350 2013-09-16 kinaba: if(limit & (1LL<<k)) f0a8c39350 2013-09-16 kinaba: { f0a8c39350 2013-09-16 kinaba: total += sub(number, k, req); f0a8c39350 2013-09-16 kinaba: req |= 1LL << k; f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: total += sub(number, 0, req); f0a8c39350 2013-09-16 kinaba: return total; f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: f0a8c39350 2013-09-16 kinaba: LL sub(vector<LL> num, int k, LL req) f0a8c39350 2013-09-16 kinaba: { f0a8c39350 2013-09-16 kinaba: for(auto& v : num) v>>=k; f0a8c39350 2013-09-16 kinaba: return exact_equal(num, req>>k); f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: f0a8c39350 2013-09-16 kinaba: LL exact_equal(const vector<LL>& xs, LL target) f0a8c39350 2013-09-16 kinaba: { f0a8c39350 2013-09-16 kinaba: int H = 50; f0a8c39350 2013-09-16 kinaba: int W = xs.size(); f0a8c39350 2013-09-16 kinaba: vector<vector<int>> M(H, vector<int>(W)); f0a8c39350 2013-09-16 kinaba: for(int y=0; y<H; ++y) f0a8c39350 2013-09-16 kinaba: for(int x=0; x<W; ++x) f0a8c39350 2013-09-16 kinaba: M[y][x] = (xs[x]>>y)&1; f0a8c39350 2013-09-16 kinaba: vector<int> V(H); f0a8c39350 2013-09-16 kinaba: for(int y=0; y<H; ++y) f0a8c39350 2013-09-16 kinaba: V[y] = (target>>y)&1; f0a8c39350 2013-09-16 kinaba: return num_solution(H, W, M, V); f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: f0a8c39350 2013-09-16 kinaba: LL num_solution(int H, int W, vector<vector<int>>& M, vector<int>& V) f0a8c39350 2013-09-16 kinaba: { f0a8c39350 2013-09-16 kinaba: int skipx = 0; f0a8c39350 2013-09-16 kinaba: int y = 0; f0a8c39350 2013-09-16 kinaba: for(int x=0; y<H && x<W; ++x) f0a8c39350 2013-09-16 kinaba: { f0a8c39350 2013-09-16 kinaba: if(M[y][x] == 0) { f0a8c39350 2013-09-16 kinaba: bool found = false; f0a8c39350 2013-09-16 kinaba: for(int yy=y+1; yy<H; ++yy) f0a8c39350 2013-09-16 kinaba: if(M[yy][x] == 1) { f0a8c39350 2013-09-16 kinaba: swap(M[y], M[yy]); f0a8c39350 2013-09-16 kinaba: swap(V[y], V[yy]); f0a8c39350 2013-09-16 kinaba: found = true; f0a8c39350 2013-09-16 kinaba: break; f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: if(!found) { f0a8c39350 2013-09-16 kinaba: ++skipx; f0a8c39350 2013-09-16 kinaba: continue; f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: f0a8c39350 2013-09-16 kinaba: for(int yy=y+1; yy<H; ++yy) f0a8c39350 2013-09-16 kinaba: if(M[yy][x] == 1) { f0a8c39350 2013-09-16 kinaba: for(int xx=x; xx<W; ++xx) f0a8c39350 2013-09-16 kinaba: M[yy][xx] ^= M[y][xx]; f0a8c39350 2013-09-16 kinaba: V[yy] ^= V[y]; f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: ++y; f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: for(int yy=y; yy<H; ++yy) f0a8c39350 2013-09-16 kinaba: if(V[yy] == 1) f0a8c39350 2013-09-16 kinaba: return 0; f0a8c39350 2013-09-16 kinaba: return 1LL << skipx; f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: }; f0a8c39350 2013-09-16 kinaba: f0a8c39350 2013-09-16 kinaba: // BEGIN CUT HERE f0a8c39350 2013-09-16 kinaba: #include <ctime> f0a8c39350 2013-09-16 kinaba: double start_time; string timer() f0a8c39350 2013-09-16 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f0a8c39350 2013-09-16 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f0a8c39350 2013-09-16 kinaba: { os << "{ "; f0a8c39350 2013-09-16 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f0a8c39350 2013-09-16 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f0a8c39350 2013-09-16 kinaba: void verify_case(const long long& Expected, const long long& Received) { f0a8c39350 2013-09-16 kinaba: bool ok = (Expected == Received); f0a8c39350 2013-09-16 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f0a8c39350 2013-09-16 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } f0a8c39350 2013-09-16 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f0a8c39350 2013-09-16 kinaba: #define END verify_case(_, XorCards().numberOfWays(number, limit));} f0a8c39350 2013-09-16 kinaba: int main(){ f0a8c39350 2013-09-16 kinaba: f0a8c39350 2013-09-16 kinaba: CASE(0) f0a8c39350 2013-09-16 kinaba: long long number_[] = {1,2}; f0a8c39350 2013-09-16 kinaba: vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); f0a8c39350 2013-09-16 kinaba: long long limit = 2LL; f0a8c39350 2013-09-16 kinaba: long long _ = 3LL; f0a8c39350 2013-09-16 kinaba: END f0a8c39350 2013-09-16 kinaba: CASE(1) f0a8c39350 2013-09-16 kinaba: long long number_[] = {5,5}; f0a8c39350 2013-09-16 kinaba: vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); f0a8c39350 2013-09-16 kinaba: long long limit = 3LL; f0a8c39350 2013-09-16 kinaba: long long _ = 2LL; f0a8c39350 2013-09-16 kinaba: END f0a8c39350 2013-09-16 kinaba: CASE(2) f0a8c39350 2013-09-16 kinaba: long long number_[] = {1,2,3,4,5,6,7}; f0a8c39350 2013-09-16 kinaba: vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); f0a8c39350 2013-09-16 kinaba: long long limit = 5LL; f0a8c39350 2013-09-16 kinaba: long long _ = 96LL; f0a8c39350 2013-09-16 kinaba: END f0a8c39350 2013-09-16 kinaba: CASE(3) f0a8c39350 2013-09-16 kinaba: long long number_[] = {123, 456, 789, 147, 258, 369, 159, 357}; f0a8c39350 2013-09-16 kinaba: vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); f0a8c39350 2013-09-16 kinaba: long long limit = 500LL; f0a8c39350 2013-09-16 kinaba: long long _ = 125LL; f0a8c39350 2013-09-16 kinaba: END f0a8c39350 2013-09-16 kinaba: CASE(4) f0a8c39350 2013-09-16 kinaba: long long number_[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; f0a8c39350 2013-09-16 kinaba: vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); f0a8c39350 2013-09-16 kinaba: long long limit = 1000000000000000LL; f0a8c39350 2013-09-16 kinaba: long long _ = 4294967296LL; f0a8c39350 2013-09-16 kinaba: END f0a8c39350 2013-09-16 kinaba: CASE(5) f0a8c39350 2013-09-16 kinaba: long long number_[] = {1000000000000000, 999999999999999}; f0a8c39350 2013-09-16 kinaba: vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); f0a8c39350 2013-09-16 kinaba: long long limit = 65535LL; f0a8c39350 2013-09-16 kinaba: long long _ = 2LL; f0a8c39350 2013-09-16 kinaba: END f0a8c39350 2013-09-16 kinaba: CASE(6) f0a8c39350 2013-09-16 kinaba: long long number_[] = {892821358,637693537,31021825,911417798,989152798,284478611,929786194,624328131,349242118,86796344,350424823,336624178,632226779,266072173,568692508,711852205,512468333,579346169,469280619,720923652,106128825,932999125,57927219,490494892,140637785,916813689,793075515,469189529,107539985,793418872,865662420,451333923,555332979,862325752,89326522,290303848,701668240,319909799,418021685,730702785,515002318,381415044,165516801,881368921,604262389,333836176,678197867,988945968,594771926,214252143}; f0a8c39350 2013-09-16 kinaba: vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); f0a8c39350 2013-09-16 kinaba: long long limit = 225387446LL; f0a8c39350 2013-09-16 kinaba: long long _ = -1LL; f0a8c39350 2013-09-16 kinaba: END f0a8c39350 2013-09-16 kinaba: CASE(7) f0a8c39350 2013-09-16 kinaba: long long number_[] = {0}; f0a8c39350 2013-09-16 kinaba: vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); f0a8c39350 2013-09-16 kinaba: long long limit = 1LL; f0a8c39350 2013-09-16 kinaba: long long _ = 2LL; f0a8c39350 2013-09-16 kinaba: END f0a8c39350 2013-09-16 kinaba: f0a8c39350 2013-09-16 kinaba: } f0a8c39350 2013-09-16 kinaba: // END CUT HERE