#855. 异或和

题目链接

#855. 异或和

给定一个长度为 n 的数组 a1,a2,...,an
请你求出下面式子的模1e9+7的值。

i=1n1j=i+1n(aiXORaj)

输入格式

第一行一个数字 n

接下来一行 n 个整数 a1,a2,,an

输出格式

一行一个整数表示答案。

样例输入

3 1 2 3

样例输出

6

数据规模

所有数据保证 2n300000,0ai<260

解题思路

思维

计算任意两个数的异或和,将每一位为 1 的数的个数记录下来,然后按位计算该位的贡献

  • 时间复杂度:O(60n)

代码

// %%%Skyqwq #include <bits/stdc++.h> // #define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=3e5+5,mod=1e9+7; int n,a[60],res; int main() { help; cin>>n; for(int i=1;i<=n;i++) { LL x; cin>>x; for(int j=0;j<60;j++) a[j]+=(x>>j&1); } for(int i=0;i<60;i++) res=(1ll*res+1ll*(1ll<<i)%mod*a[i]%mod*(n-a[i])%mod)%mod; cout<<res; return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16212956.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示