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;
}
相关推荐
挖矿大亨2 小时前
C++中深拷贝与浅拷贝的原理
开发语言·c++·算法
Bruce_kaizy2 小时前
c++图论——生成树之Kruskal&Prim算法
c++·算法·图论
雾岛听蓝3 小时前
C++:模拟实现string类
开发语言·c++
XFF不秃头3 小时前
力扣刷题笔记-合并区间
c++·笔记·算法·leetcode
编程之路,妙趣横生3 小时前
STL(七) unordered_set 与 unordered_map 基本用法 + 模拟实现
c++
寂柒3 小时前
c++--
c++
wregjru4 小时前
【读书笔记】Effective C++ 条款3:尽可能使用const
开发语言·c++
历程里程碑5 小时前
滑动窗口秒解LeetCode字母异位词
java·c语言·开发语言·数据结构·c++·算法·leetcode
Tandy12356_5 小时前
手写TCP/IP协议栈——TCP结构定义与基本接口实现
c语言·网络·c++·网络协议·tcp/ip·计算机网络
Helibo445 小时前
2025年12月gesp3级题解
数据结构·c++·算法