結果

問題 No.1240 Or Sum of Xor Pair
ユーザー 👑 potato167potato167
提出日時 2022-03-08 23:42:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 2,594 bytes
コンパイル時間 2,423 ms
コンパイル使用メモリ 207,996 KB
実行使用メモリ 15,564 KB
最終ジャッジ日時 2023-10-01 06:21:24
合計ジャッジ時間 8,891 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 7 ms
4,380 KB
testcase_04 AC 6 ms
4,380 KB
testcase_05 AC 6 ms
4,380 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 5 ms
4,388 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 27 ms
4,380 KB
testcase_11 AC 33 ms
4,376 KB
testcase_12 AC 51 ms
5,136 KB
testcase_13 AC 51 ms
4,688 KB
testcase_14 AC 55 ms
4,964 KB
testcase_15 AC 258 ms
11,664 KB
testcase_16 AC 238 ms
11,732 KB
testcase_17 AC 248 ms
11,460 KB
testcase_18 AC 231 ms
11,724 KB
testcase_19 AC 226 ms
11,472 KB
testcase_20 AC 258 ms
11,496 KB
testcase_21 AC 225 ms
11,648 KB
testcase_22 AC 257 ms
11,768 KB
testcase_23 AC 218 ms
11,376 KB
testcase_24 AC 259 ms
11,384 KB
testcase_25 AC 202 ms
11,096 KB
testcase_26 AC 254 ms
11,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 28 ms
6,772 KB
testcase_30 AC 25 ms
7,784 KB
testcase_31 AC 50 ms
15,564 KB
testcase_32 AC 130 ms
10,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=2100000000;
const ll mod=998244353;
#define rep(i,a) for (int i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}

ll calc(vector<ll> &p){
	ll n=p.size();
	vector<ll> D(18,n);
	for(ll x:p){
		rep(i,18) if(x&(1<<i)) D[i]--;
	}
	ll ans=((1ll<<18)-1ll)*n*(n-1);
	ans/=2;
	rep(i,18){
		ans-=((1ll<<i))*((D[i]*(D[i]-1))/2);
	}
	return ans;
}
ll calc_2(vector<ll> &p,vector<ll> &q){
	ll n=p.size();
	ll ans=0;
	vector<ll> D(18);
	for(ll x:p){
		rep(i,18) if(x&(1<<i)) D[i]++;
	}
	for(ll x:q){
		rep(i,18){
			if(x&(1<<i)) ans+=n*(1ll<<i);
			else ans+=D[i]*(1ll<<i);
		}
	}
	return ans;
}
ll g(int D,ll X,vector<ll> &l,vector<ll> &r);
ll f(int D,ll X,vector<ll> &p);
void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	ll N,X;
	cin>>N>>X;
	vector<ll> A(N);
	rep(i,N) cin>>A[i];
	cout<<f(18,X,A)<<"\n";
}
ll f(int D,ll X,vector<ll> &p){
	int n=p.size();
	if(D<0) return 0;
	if(n<2) return 0;
	vector<ll> l;
	vector<ll> r;
	rep(i,n){
		if(p[i]&(1<<D)){
			r.push_back(p[i]);
		}else l.push_back(p[i]);
	}
	if(X&(1<<D)){
		return calc(l)+calc(r)+g(D-1,X,l,r);
	}
	return f(D-1,X,r)+f(D-1,X,l);
}
ll g(int D,ll X,vector<ll> &l,vector<ll> &r){
	int n=l.size(),m=r.size();
	if(n==0||m==0||D==-1) return 0;
	vector<ll> l0,l1,r0,r1;
	rep(i,n){
		if(l[i]&(1<<D)) l1.push_back(l[i]);
		else l0.push_back(l[i]);
	}rep(i,m){
		if(r[i]&(1<<D)) r1.push_back(r[i]);
		else r0.push_back(r[i]);
	}
	if(X&(1<<D)){
		return calc_2(l0,r0)+calc_2(l1,r1)+g(D-1,X,l1,r0)+g(D-1,X,l0,r1);
	}
	return g(D-1,X,l0,r0)+g(D-1,X,l1,r1);
}
0