博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hug the princess(思维,位运算)
阅读量:5033 次
发布时间:2019-06-12

本文共 2457 字,大约阅读时间需要 8 分钟。

Hug the princess

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit
 Status

There is a sequence with nn elements. Assuming they are a1,a2,,ana1,a2,⋯,an.

Please calculate the following expession.

1i<jn(aiaj)+(ai|aj)+(ai&aj)∑1≤i<j≤n(ai∧aj)+(ai|aj)+(ai&aj)

In the expression above, ^ | & is bit operation. If you don’t know bit operation, you can visit

to get some useful information.

Input

The first line contains a single integer nn, which is the size of the sequence.

The second line contains nn integers, the ithith integer aiai is the ithith element of the sequence.

1n100000,0ai1000000001≤n≤100000,0≤ai≤100000000

Output

Print the answer in one line.

Sample input and output

Sample Input Sample Output
21 2
6

Hint

Because the answer is so large, please use long long instead of int. Correspondingly, please use %lld instead of %d to scanf and printf.

Large input. You may get Time Limit Exceeded if you use "cin" to get the input. So "scanf" is suggested.

Likewise, you are supposed to use "printf" instead of "cout".

题解:

让求这个表达式,我们可以把求出每一位的前缀和,然后再根据每一位找出前面是1,是0的当前位的个数;然后加上cnt * (1 << j)就好了

代码:

#include
#include
#include
#include
using namespace std;const int MAXN = 1e5 + 100;int num[MAXN][35];int a[35];int p[MAXN];typedef long long LL;int main(){ int n; while(~scanf("%d", &n)){ int x; memset(num, 0, sizeof(num)); for(int i = 1; i <= n; i++){ scanf("%d", &x); for(int j = 0; j < 33; j++){ num[i][j] = num[i - 1][j] + x % 2; x = x / 2; } } LL ans = 0; for(int i = 1; i <= n; i++){ int temp = 0, pos = -1; for(int j = 0; j < 33; j++){ a[j] = num[i][j] - num[i - 1][j]; // printf("%d ", a[j]); }//puts(""); for(int j = 0; j < 33; j++){ int cnt = 0; if(a[j]){ cnt += (i - 1 - num[i - 1][j]); cnt += i - 1; cnt += num[i - 1][j]; } else{ cnt += num[i - 1][j]; cnt += num[i - 1][j]; } ans += (LL)cnt * (1 << j); } } printf("%lld\n", ans); } return 0;}

 

 

转载于:https://www.cnblogs.com/handsomecui/p/5470680.html

你可能感兴趣的文章
D3.js系列——初步使用、选择元素与绑定数据
查看>>
Go笔记(1):Go环境的安装
查看>>
菜鸟学IT之python3关于列表,元组,字典,集合浅认识!
查看>>
gulp + gulp-better-rollup + rollup 构建 ES6 开发环境
查看>>
关于position
查看>>
android之bitmap存入数据库发生内存溢出
查看>>
Hadoop HDFS Explorer连接Windows上的HDFS
查看>>
C语言转义字符基础总结
查看>>
第四阶段 08_Linux DHCP自动获取IP
查看>>
vue
查看>>
[转]分布式计算框架综述
查看>>
WorldWind源码剖析系列:窗口定制控件类WorldWindow
查看>>
配置Oracle透明网关用以连接 SQLServer经验总结
查看>>
MySQL导入数据遇到Error Number: 1467 Failed to read auto-increment value from storage engine错误...
查看>>
常见样式兼容性问题整理
查看>>
关于Mybatis-Plus的一些话题
查看>>
常用模块——os模块/sys模块/os.path模块/random模块/shutil模块
查看>>
Linux命令(十五) 打包或解压文件 tar
查看>>
Android开发学习之路-关于Exception
查看>>
hdu5384(trie树)
查看>>