UESTC - 1041 Hug the princess(位运算&转换)好题
来源:互联网 发布:淘宝东西会自动下架吗 编辑:程序博客网 时间:2024/06/06 09:44
Description
There is a sequence with n elements. Assuming they are a1,a2,⋯,an.
Please calculate the following expession.
∑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
http://en.wikipedia.org/wiki/Bitwise_operation
to get some useful information.
Input
The first line contains a single integer n, which is the size of the sequence.
The second line contains n integers, the ith integer ai is the ith element of the sequence.
1≤n≤100000,0≤ai≤100000000
Output
Print the answer in one line.
Sample Input
2
1 2
Sample Output
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”.
Source
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>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++){for(int j = 0; j < 33; j++){a[j] = num[i][j] - num[i - 1][j];}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;}
- UESTC - 1041 Hug the princess(位运算&转换)好题
- UESTC--1041--Hug the princess(位运算)
- CDOJ 1041 Hug the princess
- CDOJ 1041 Hug the princess 【思维】
- The 13th UESTC Programming Contest Preliminary——Hug the princess
- UESTC 1709 Binary Operations (位运算)
- hdoj The Romantic Hero 4901 (DP 位运算) 好题
- The lonely hug shirt
- UESTC 84 Binary Operations(位运算+DP)
- UESTC - 1045 Lovely princess (模拟)水
- zzulioj--1832--贪吃的松鼠(位运算好题)
- zoj--3870--Team Formation(位运算好题)
- HDU - 5666 Segment (大数位运算)好题
- 1042 - Secret Origins (位运算好题)
- 逻辑代数,按位运算(UESTC 491,Tricks in Bits)
- UESTC - 1252 24点游戏 (DFS)好题
- 位运算,大小写转换
- 经典好题!杭电OJ--Ignatius and the Princess I
- Oracle数据库-SQL及操作符
- JSP-JSP的生命周期
- 十九、适配器设计模式
- android studio NDK 开发初探
- JAVA8 Lambda表达式完全解析
- UESTC - 1041 Hug the princess(位运算&转换)好题
- 自言自语3
- 使用 Intel HAXM 为 Android 模拟器加速,媲美真机
- Xftp5中文文件乱码
- 带你系统学习GCD(二)
- python3+scapy扫描获取局域网主机ip和mac
- Notes wk10 sensor & lbs
- Android的监听滞后性
- BestCoder Round #82 (div.1) 1002 HDU 5677 dp-类似多重背包的思想