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;
}
相关推荐
UIU11410 小时前
P1481 魔族密码
c++·算法·动态规划
闻缺陷则喜何志丹17 小时前
【C++特性】四种类型转
c++·算法·类型转换
apcipot_rain20 小时前
C++转Java日记(02)
java·开发语言·c++
杜 硕20 小时前
从零开始的C++生活——第七讲——02
开发语言·c++·生活
Rabitebla1 天前
【Linux 底层原理】进程(十):进程等待详解---从僵尸进程到 wait、waitpid 与 status
linux·c++·笔记·学习·visual studio
西西弗Sisyphus1 天前
C++ 超时互斥锁(Timed Mutex):std::timed_mutex 与 std::unique_lock
c++
今天要早睡_1 天前
C++01_入门基础复习
开发语言·c++
Fuyo_11191 天前
C++ 运行时多态与虚函数机制全面剖析【面试重点】
开发语言·c++·面试
Chen_harmony2 天前
七、STL与string类
开发语言·c++
xxxiugou1232 天前
C++命名空间与引用全解析
开发语言·c++