#pragma GCC optimize ("Ofast") #include using namespace std; void*wmem; char memarr[96000000]; template inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template void sortA_L(int N, T1 a[], void *mem = wmem){ sort(a, a+N); } template void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){ int i; pair*arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first = a[i]; arr[i].second = b[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first; b[i] = arr[i].second; } } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } int N; int X; int A[200000]; int v[200000]; long long res; long long solve(int n, int a[], int m, int b[], int bt){ int i; int j; long long n1; long long n2; long long m1; long long m2; long long c1; long long c2; long long res = 0; if(bt == -1 || n == 0 || m == 0){ return res; } for(i=(0);i<(n);i++){ v[i] = a[i] % (1<<(bt+1)); } sortA_L(n,v,a); for(i=(0);i<(m);i++){ v[i] = b[i] % (1<<(bt+1)); } sortA_L(m,v,b); for(i=(0);i<(n);i++){ if(((a[i]) &(1<<(bt)))){ break; } } n1 = i; n2 = n - i; for(i=(0);i<(m);i++){ if(((b[i]) &(1<<(bt)))){ break; } } m1 = i; m2 = m - i; if(((X) &(1<<(bt)))){ int k; for(k=(0);k<(18);k++){ c1 = c2 = 0; for(i=(0);i<(n1);i++){ if(!((a[i]) &(1<<(k)))){ c1++; } } for(i=(0);i<(m1);i++){ if(!((b[i]) &(1<<(k)))){ c2++; } } res += (n1 * m1 - c1 * c2) *(1<<(k)); } for(k=(0);k<(18);k++){ c1 = c2 = 0; for(i=(0);i<(n2);i++){ if(!((a[n1+i]) &(1<<(k)))){ c1++; } } for(i=(0);i<(m2);i++){ if(!((b[m1+i]) &(1<<(k)))){ c2++; } } res += (n2 * m2 - c1 * c2) *(1<<(k)); } res += solve(n1, a, m2, b+m1, bt-1); res += solve(n2, a+n1, m1, b, bt-1); } else{ res += solve(n1, a, m1, b, bt-1); res += solve(n2, a+n1, m2, b+m1, bt-1); } return res; } int main(){ int i; wmem = memarr; int*a; int*b; long long res; rd(N); rd(X); { int ZIeRIny5; for(ZIeRIny5=(0);ZIeRIny5<(N);ZIeRIny5++){ rd(A[ZIeRIny5]); } } walloc1d(&a,N); walloc1d(&b,N); for(i=(0);i<(N);i++){ a[i] = b[i] = A[i]; } res = solve(N,a,N,b,18); for(i=(0);i<(N);i++){ res -= A[i]; } res /= 2; wt_L(res); wt_L('\n'); return 0; } // cLay varsion 20200926-1 // --- original code --- // int N, X, A[2d5], v[2d5]; // ll res; // // ll solve(int n, int a[], int m, int b[], int bt){ // int i, j; ll n1, n2, m1, m2, c1, c2; // ll res = 0; // // if(bt == -1 || n == 0 || m == 0) return res; // // rep(i,n) v[i] = a[i] % (1<<(bt+1)); // sortA(n,v,a); // rep(i,m) v[i] = b[i] % (1<<(bt+1)); // sortA(m,v,b); // // rep(i,n) if(BIT_ith(a[i],bt)) break; // n1 = i; // n2 = n - i; // // rep(i,m) if(BIT_ith(b[i],bt)) break; // m1 = i; // m2 = m - i; // // if(BIT_ith(X, bt)){ // rep(k,18){ // c1 = c2 = 0; // rep(i,n1) if(!BIT_ith(a[i],k)) c1++; // rep(i,m1) if(!BIT_ith(b[i],k)) c2++; // res += (n1 * m1 - c1 * c2) * BIT_ith(k); // } // rep(k,18){ // c1 = c2 = 0; // rep(i,n2) if(!BIT_ith(a[n1+i],k)) c1++; // rep(i,m2) if(!BIT_ith(b[m1+i],k)) c2++; // res += (n2 * m2 - c1 * c2) * BIT_ith(k); // } // res += solve(n1, a, m2, b+m1, bt-1); // res += solve(n2, a+n1, m1, b, bt-1); // } else { // res += solve(n1, a, m1, b, bt-1); // res += solve(n2, a+n1, m2, b+m1, bt-1); // } // // return res; // } // // { // int *a, *b; // ll res; // rd(N,X,A(N)); // walloc1d(&a,N); // walloc1d(&b,N); // rep(i,N) a[i] = b[i] = A[i]; // res = solve(N,a,N,b,18); // rep(i,N) res -= A[i]; // res /= 2; // wt(res); // }