6bf49a0393 2012-12-20 kinaba: #include <iostream> 6bf49a0393 2012-12-20 kinaba: #include <sstream> 6bf49a0393 2012-12-20 kinaba: #include <iomanip> 6bf49a0393 2012-12-20 kinaba: #include <vector> 6bf49a0393 2012-12-20 kinaba: #include <string> 6bf49a0393 2012-12-20 kinaba: #include <map> 6bf49a0393 2012-12-20 kinaba: #include <set> 6bf49a0393 2012-12-20 kinaba: #include <algorithm> 6bf49a0393 2012-12-20 kinaba: #include <numeric> 6bf49a0393 2012-12-20 kinaba: #include <iterator> 6bf49a0393 2012-12-20 kinaba: #include <functional> 6bf49a0393 2012-12-20 kinaba: #include <complex> 6bf49a0393 2012-12-20 kinaba: #include <queue> 6bf49a0393 2012-12-20 kinaba: #include <stack> 6bf49a0393 2012-12-20 kinaba: #include <cmath> 6bf49a0393 2012-12-20 kinaba: #include <cassert> 6bf49a0393 2012-12-20 kinaba: using namespace std; 6bf49a0393 2012-12-20 kinaba: typedef long long LL; 6bf49a0393 2012-12-20 kinaba: typedef long double LD; 6bf49a0393 2012-12-20 kinaba: typedef complex<LD> CMP; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: static const unsigned MODVAL = 1000000007; 6bf49a0393 2012-12-20 kinaba: struct mint 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: unsigned val; 6bf49a0393 2012-12-20 kinaba: mint():val(0){} 6bf49a0393 2012-12-20 kinaba: mint(int x):val(x%MODVAL) {} 6bf49a0393 2012-12-20 kinaba: mint(unsigned x):val(x%MODVAL) {} 6bf49a0393 2012-12-20 kinaba: mint(LL x):val(x%MODVAL) {} 6bf49a0393 2012-12-20 kinaba: }; 6bf49a0393 2012-12-20 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 6bf49a0393 2012-12-20 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 6bf49a0393 2012-12-20 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 6bf49a0393 2012-12-20 kinaba: mint operator+(mint x, mint y) { return x+=y; } 6bf49a0393 2012-12-20 kinaba: mint operator-(mint x, mint y) { return x-=y; } 6bf49a0393 2012-12-20 kinaba: mint operator*(mint x, mint y) { return x*=y; } 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 6bf49a0393 2012-12-20 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } 6bf49a0393 2012-12-20 kinaba: mint operator/(mint x, mint y) { return x/=y; } 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: class DefectiveAddition { public: 6bf49a0393 2012-12-20 kinaba: int count(vector <int> cards, int n) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: int n_msb = 0; 6bf49a0393 2012-12-20 kinaba: while( (1<<n_msb+1) <= n ) {} 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: vector<int> cc(30); 6bf49a0393 2012-12-20 kinaba: vector<int> more; 6bf49a0393 2012-12-20 kinaba: for(int i=0; i<cards.size(); ++i) { 6bf49a0393 2012-12-20 kinaba: for(int k=0; k<=29; ++k) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: if((1<<k)<=cards[i] && cards[i]<(1<<k+1)) 6bf49a0393 2012-12-20 kinaba: cc[k].push_back(cards[i]); 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: mint p = 0; 6bf49a0393 2012-12-20 kinaba: for(int k=29; k>=0; --k) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: int BIT = (n>>k)&1; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: int up = 0; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: vector<int> rel; 6bf49a0393 2012-12-20 kinaba: for(int i=0; i<cards.size(); ++i) 6bf49a0393 2012-12-20 kinaba: if( (1<<k) <= cards[i] ) { 6bf49a0393 2012-12-20 kinaba: if( (1<<k+1) <= cards[i] ) 6bf49a0393 2012-12-20 kinaba: up ^= cards[i]; 6bf49a0393 2012-12-20 kinaba: if( (1<<k) & cards[i] ) 6bf49a0393 2012-12-20 kinaba: rel.push_back(i); 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: // all take above 6bf49a0393 2012-12-20 kinaba: if( (up>>k+1) == (n>>k+1) ) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: for(int ri=0; ri<rel.size(); ++ri) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: int i = rel[ri]; 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: return p.val; 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: }; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: // BEGIN CUT HERE 6bf49a0393 2012-12-20 kinaba: #include <ctime> 6bf49a0393 2012-12-20 kinaba: double start_time; string timer() 6bf49a0393 2012-12-20 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 6bf49a0393 2012-12-20 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 6bf49a0393 2012-12-20 kinaba: { os << "{ "; 6bf49a0393 2012-12-20 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 6bf49a0393 2012-12-20 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 6bf49a0393 2012-12-20 kinaba: void verify_case(const int& Expected, const int& Received) { 6bf49a0393 2012-12-20 kinaba: bool ok = (Expected == Received); 6bf49a0393 2012-12-20 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 6bf49a0393 2012-12-20 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 6bf49a0393 2012-12-20 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 6bf49a0393 2012-12-20 kinaba: #define END verify_case(_, DefectiveAddition().count(cards, n));} 6bf49a0393 2012-12-20 kinaba: int main(){ 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: CASE(0) 6bf49a0393 2012-12-20 kinaba: int cards_[] = {2,3}; 6bf49a0393 2012-12-20 kinaba: vector <int> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); 6bf49a0393 2012-12-20 kinaba: int n = 2; 6bf49a0393 2012-12-20 kinaba: int _ = 3; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(1) 6bf49a0393 2012-12-20 kinaba: int cards_[] = {1,2,3}; 6bf49a0393 2012-12-20 kinaba: vector <int> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); 6bf49a0393 2012-12-20 kinaba: int n = 1; 6bf49a0393 2012-12-20 kinaba: int _ = 6; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(2) 6bf49a0393 2012-12-20 kinaba: int cards_[] = {4, 5, 7, 11}; 6bf49a0393 2012-12-20 kinaba: vector <int> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); 6bf49a0393 2012-12-20 kinaba: int n = 6; 6bf49a0393 2012-12-20 kinaba: int _ = 240; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(3) 6bf49a0393 2012-12-20 kinaba: int cards_[] = {1,2,3,4,5,6,7,8,9,10}; 6bf49a0393 2012-12-20 kinaba: vector <int> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); 6bf49a0393 2012-12-20 kinaba: int n = 15; 6bf49a0393 2012-12-20 kinaba: int _ = 1965600; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(4) 6bf49a0393 2012-12-20 kinaba: int cards_[] = {1,2,3,4,5,6,7,8,9,10}; 6bf49a0393 2012-12-20 kinaba: vector <int> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); 6bf49a0393 2012-12-20 kinaba: int n = 16; 6bf49a0393 2012-12-20 kinaba: int _ = 0; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(5) 6bf49a0393 2012-12-20 kinaba: int cards_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; 6bf49a0393 2012-12-20 kinaba: vector <int> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); 6bf49a0393 2012-12-20 kinaba: int n = 1; 6bf49a0393 2012-12-20 kinaba: int _ = 949480669; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: /* 6bf49a0393 2012-12-20 kinaba: CASE(6) 6bf49a0393 2012-12-20 kinaba: int cards_[] = ; 6bf49a0393 2012-12-20 kinaba: vector <int> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); 6bf49a0393 2012-12-20 kinaba: int n = ; 6bf49a0393 2012-12-20 kinaba: int _ = ; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(7) 6bf49a0393 2012-12-20 kinaba: int cards_[] = ; 6bf49a0393 2012-12-20 kinaba: vector <int> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); 6bf49a0393 2012-12-20 kinaba: int n = ; 6bf49a0393 2012-12-20 kinaba: int _ = ; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: */ 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: // END CUT HERE