Sum of XOR Functions
Sum of XOR Functions
题目
有一个序列
思路
位运算的题,我们对于每一位进行考虑,会发现构成了很多个
设
对于
我们用
用
也就是说,表示对于
我们发现当我们预处理完成这些数组时,就可以
接下来就是
主要思路到此结束。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define int long long
const int MOD = 998244353;
const int MAXN = 3e5 + 7;
int n,a[MAXN];
int cnt[MAXN][2],sum[MAXN][2];
int pre[MAXN];
int ans = 0;
signed main(){
scanf("%lld", &n);
for(int i = 1;i <= n;i++) scanf("%lld", &a[i]);
for(int w = 0;w <= 31;w++){
memset(cnt,0,sizeof(cnt));
memset(sum,0,sizeof(sum));
memset(pre,0,sizeof(pre));
cnt[0][0] = 1;
for(int i = 1;i <= n;i++){
pre[i] = pre[i - 1];
cnt[i][0] = cnt[i - 1][0];
cnt[i][1] = cnt[i - 1][1];
sum[i][0] = sum[i - 1][0];
sum[i][1] = sum[i - 1][1];
pre[i] += (a[i] >> w) & 1;
if(pre[i] & 1) cnt[i][1]++,sum[i][1] += i;
else cnt[i][0]++,sum[i][0] += i;
}
for(int i = 1;i <= n;i++){
if(pre[i] & 1) ans = (ans + ((cnt[i - 1][0] * i - sum[i - 1][0]) % MOD) * (1 << w) % MOD) % MOD;
else ans = (ans + ((cnt[i - 1][1] * i - sum[i - 1][1]) % MOD) * (1 << w) % MOD) % MOD;
ans = ans % MOD;
}
}
cout<<ans;
return 0;
}
本文来自博客园,作者:wyl123ly,转载请注明原文链接:https://www.cnblogs.com/wyl123ly/p/18436384
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】