P3917 异或序列

题目链接

按位考虑,计算有多少个区间异或值使得该位为1,因为该位为1才有贡献。

对每一位异或前缀,记录1,0的个数,易知最终结果是01的个数相乘。

cpp 复制代码
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	
	int n;
	cin >> n;

	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}    

	ll ans = 0;
	for (int i = 0; i < 30; i++) {
		int cnt[2]{1, 0};   //单独的1也有贡献,所以0的个数初始化为1
		int x = 0;

		for (int j = 0; j < n; j++) {
			x ^= a[j] >> i & 1;
			cnt[x]++;
		}
		ans += 1LL * cnt[0] * cnt[1] * (1 << i);
	}

	cout << ans << "\n";

    return 0;
}
相关推荐
m0_3807438716 分钟前
C++中的freopen的用法实例详解
开发语言·c++
jimy11 小时前
polymorph.cpp里面的“析构函数、智能指针”问题
开发语言·c++
朝朝辞暮i2 小时前
C++ 第 6 课:switch —— 多状态选择
开发语言·c++·算法
朝朝辞暮i2 小时前
C++ 第 4 课:if / else —— 让程序自己做决定
开发语言·c++
苏打豆2 小时前
Pinocchio源码阅读——刚体运动物理量空间变换及求导
c++·线性代数·算法·矩阵·机器人
m0_380743872 小时前
Qt中导航栏实现的详细指南
开发语言·c++
估值探索者3 小时前
【Python量化策略实战 #02】多因子合成mom和vol两因子打分合成
开发语言·c++·python·数据挖掘·c#
思麟呀3 小时前
位运算:从一个整数到一个集合
c语言·数据结构·c++·算法
All for pursuit.4 小时前
【分治-3】347.前K个高频元素
数据结构·c++·算法·leetcode
头发够用的程序员4 小时前
TensorRT 自定义算子插件实战(一):从零手写 customScaledTanh
c++·人工智能·pytorch·python·深度学习·边缘计算·jetson