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;
}
相关推荐
·present·7 小时前
CSAPP(lecture 4_01)
c语言·c++
FPGA小迷弟8 小时前
用 Claude Code 重构遗留系统:从评估到落地的完整实践指南
c++·python·ai·plotly·django·ai编程·claude
程序员-King.8 小时前
【ROS1】Noetic 开发工作流程与常用命令
c++·ubuntu·ros
云深麋鹿8 小时前
C++ | 基于红黑树实现mymap与myset
开发语言·c++
傲世仙尊10 小时前
string增删查改-insert-erase-replace与find加substr组合技
c++
·present·11 小时前
CSAPP(lecture 3)
c++
别动我齐刘海12 小时前
机器人 C++ / 系统联调补强2——异常处理、Protobuf
linux·c++·人工智能·学习·算法·机器学习·机器人
basketball61612 小时前
C++ 中字符串与常见类型的转换总结
开发语言·c++
等我调个Bug12 小时前
C++智能指针通俗讲解
开发语言·c++
kyle~14 小时前
时间同步 --- PTP 与 Scheduled Action Command
c++·数码相机·机器人·传感器