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;
}
相关推荐
tankeven5 分钟前
HJ151 模意义下最大子序列和(Easy Version)
c++·算法
fengenrong29 分钟前
20260325
开发语言·c++
BestOrNothing_201530 分钟前
从C++结构体、类到 PID 控制器:运动控制初学者如何理解 C++ 工程代码
c++·面向对象·pid·运动控制·.h与.cpp·struct与class
㓗冽40 分钟前
2026.03.27(第三天)
数据结构·c++·算法
SWAGGY..1 小时前
【C++初阶】:(5)内存管理
java·c++·算法
liulilittle2 小时前
SQLite3增删改查(C
c语言·开发语言·数据库·c++·sqlite
CVer儿2 小时前
c++的移动语义
c++
逻辑君2 小时前
Research in Brain-inspired Computing [7]-带关节小人(3个)推箱的类意识报告
c++·人工智能·神经网络·机器学习
txinyu的博客3 小时前
解析muduo源码之 HttpResponse.h & HttpResponse.cc
c++
小白学习记录555553 小时前
vs2019无法自动补全QT代码
c++