Artifact Content
Not logged in

Artifact 8a48c9e039e2e851a4842abfeb521c989e61cdec


#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 XorCards { public:
	long long numberOfWays(vector<long long> number, long long limit)
	{
		LL total = 0;
		LL req = 0;
		for(int k=62; k>=0; --k)
			if(limit & (1LL<<k))
			{
				total += sub(number, k, req);
				req |= 1LL << k;
			}
		total += sub(number, 0, req);
		return total;
	}

	LL sub(vector<LL> num, int k, LL req)
	{
		for(auto& v : num) v>>=k;
		return exact_equal(num, req>>k);
	}

	LL exact_equal(const vector<LL>& xs, LL target)
	{
		int H = 50;
		int W = xs.size();
		vector<vector<int>> M(H, vector<int>(W));
		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
			M[y][x] = (xs[x]>>y)&1;
		vector<int> V(H);
		for(int y=0; y<H; ++y)
			V[y] = (target>>y)&1;
		return num_solution(H, W, M, V);
	}

	LL num_solution(int H, int W, vector<vector<int>>& M, vector<int>& V)
	{
		int skipx = 0;
		int y = 0;
		for(int x=0; y<H && x<W; ++x)
		{
			if(M[y][x] == 0) {
				bool found = false;
				for(int yy=y+1; yy<H; ++yy)
					if(M[yy][x] == 1) {
						swap(M[y], M[yy]);
						swap(V[y], V[yy]);
						found = true;
						break;
					}
				if(!found) {
					++skipx;
					continue;
				}
			}

			for(int yy=y+1; yy<H; ++yy)
				if(M[yy][x] == 1) {
					for(int xx=x; xx<W; ++xx)
						M[yy][xx] ^= M[y][xx];
					V[yy] ^= V[y];
				}
			++y;
		}
		for(int yy=y; yy<H; ++yy)
			if(V[yy] == 1)
				return 0;
		return 1LL << skipx;
	}
};

// 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 long long& Expected, const long long& 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(_, XorCards().numberOfWays(number, limit));}
int main(){

CASE(0)
	long long number_[] = {1,2};
	  vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); 
	long long limit = 2LL; 
	long long _ = 3LL; 
END
CASE(1)
	long long number_[] = {5,5};
	  vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); 
	long long limit = 3LL; 
	long long _ = 2LL; 
END
CASE(2)
	long long number_[] = {1,2,3,4,5,6,7};
	  vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); 
	long long limit = 5LL; 
	long long _ = 96LL; 
END
CASE(3)
	long long number_[] = {123, 456, 789, 147, 258, 369, 159, 357};
	  vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); 
	long long limit = 500LL; 
	long long _ = 125LL; 
END
CASE(4)
	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};
	  vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); 
	long long limit = 1000000000000000LL; 
	long long _ = 4294967296LL; 
END
CASE(5)
	long long number_[] = {1000000000000000, 999999999999999};
	  vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); 
	long long limit = 65535LL; 
	long long _ = 2LL; 
END
CASE(6)
	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};
	  vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); 
	long long limit = 225387446LL; 
	long long _ = -1LL; 
END
CASE(7)
long long number_[] = {0};
	  vector<long long> number(number_, number_+sizeof(number_)/sizeof(*number_)); 
	long long limit = 1LL; 
	long long _ = 2LL; 
END

}
// END CUT HERE