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.

∑1≤i<j≤n(ai∧aj)+(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

http://en.wikipedia.org/wiki/Bitwise_operation

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.

1≤n≤100000,0≤ai≤1000000001≤n≤100000,0≤ai≤100000000

Output

Print the answer in one line.

Sample input and output

Sample Input Sample Output
2
1 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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + ;
int num[MAXN][];
int a[];
int p[MAXN];
typedef long long LL;
int main(){
int n;
while(~scanf("%d", &n)){
int x;
memset(num, , sizeof(num));
for(int i = ; i <= n; i++){
scanf("%d", &x);
for(int j = ; j < ; j++){
num[i][j] = num[i - ][j] + x % ;
x = x / ;
}
}
LL ans = ;
for(int i = ; i <= n; i++){
int temp = , pos = -;
for(int j = ; j < ; j++){
a[j] = num[i][j] - num[i - ][j];
// printf("%d ", a[j]);
}//puts("");
for(int j = ; j < ; j++){
int cnt = ;
if(a[j]){
cnt += (i - - num[i - ][j]);
cnt += i - ;
cnt += num[i - ][j];
}
else{
cnt += num[i - ][j];
cnt += num[i - ][j];
}
ans += (LL)cnt * ( << j);
}
}
printf("%lld\n", ans);
}
return ;
}

Hug the princess(思维,位运算)的更多相关文章

  1. Codeforces Round #672 (Div. 2) B. Rock and Lever题解(思维+位运算)

    题目链接 题目大意 给你一个长为n(n<=1e5)的数组,让你求有多少对a[i]和a[j] (i!=j)满足a[i]&a[j]>a[i]^a[j] 题目思路 这些有关位运算的题目肯 ...

  2. Codeforces Round #299 (Div. 2) B. Tavas and SaDDas【DFS/*进制思维/位运算/一个数为幸运数,当且仅当它的每一位要么是4,要么是7 ,求小于等于n的幸运数个数】

    B. Tavas and SaDDas time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. CodeForces - 1230D(思维+位运算)

    题意 https://vjudge.net/problem/CodeForces-1230D 要组建一个小组,要求小组中每个人都不比所有人强,当一个人懂得一个算法但是另一个不懂那么前者认为他比后者强. ...

  4. 新疆大学ACM新生赛(公开赛) E.异或 (思维,位运算)

    题意:RT 题解: \(i\ mod \ k=0\),即所有事\(k\)的倍数的位置都要进行异或,根据异或的性质,我们知道如果相同的异或的数个数是偶数的话,得出的结果是\(0\),所以每次询问,我们判 ...

  5. Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维

    & -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...

  6. 【洛谷4424】[HNOI/AHOI2018] 寻宝游戏(位运算思维题)

    点此看题面 大致题意: 给你\(n\)个\(m\)位二进制数.每组询问给你一个\(m\)位二进制数,要求你从\(0\)开始,依次对于这\(n\)个数进行\(and\)或\(or\)操作,问有多少种方案 ...

  7. Divide by Zero 2021 and Codeforces Round #714 (Div. 2) B. AND Sequences思维,位运算 难度1400

    题目链接: Problem - B - Codeforces 题目 Example input 4 3 1 1 1 5 1 2 3 4 5 5 0 2 0 3 0 4 1 3 5 1 output 6 ...

  8. cdoj第13th校赛初赛H - Hug the princess

    http://acm.uestc.edu.cn/#/contest/show/54 H - Hug the princess Time Limit: 3000/1000MS (Java/Others) ...

  9. Java编程的逻辑 (4) - 整数的二进制表示与位运算

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

随机推荐

  1. [Regular Expressions] Find Repeated Patterns

    Regular Expression Quantifiers allow us to identify a repeating sequence of characters of minimum an ...

  2. codeforces C. Cd and pwd commands 执行命令行

    执行命令来改变路径 cd 并显示路径命令 pwd 一个节目的 抽样: input 7 pwd cd /home/vasya pwd cd .. pwd cd vasya/../petya pwd ou ...

  3. 消息摘要算法-HMAC算法

    一.简述 mac(Message Authentication Code.消息认证码算法)是含有密钥散列函数算法.兼容了MD和SHA算法的特性,并在此基础上加上了密钥.因此MAC算法也常常被称作HMA ...

  4. javascript紧接上一张for循环的问题,我自己的理解

    这类问题,通常都是在for循环里,根据i的变化作为dom的下标来作当前dom的点击事件, 预期是,每个点击事件都对应相应的i下标,但是问题是,每次点击的都是最后一次节点的dom. 原因就是其实我们作点 ...

  5. encodeURI与encodeURIComponent的区别

    webservice输出时选择的格式与Content-Type报文头有关 encodeURI与encodeURIComponent的区别:后者会将URI进行编码(包括"://")

  6. mongodb 详解 error:10061 由于目标计算机积极拒绝,无法连接解决方法

    mongodb下载地址(32位):下载地址 自己选择版本 建立如下与mongodb并行的两个文件夹data和log. 然后建立mongo.config. 在mongo.config配置文件中输入: # ...

  7. C#图像处理(2):给图片加白边

    C#图片处理给图片添加白边: /// <summary> /// 在图片上方加入白边 /// </summary> /// <param name="Img&q ...

  8. web 调用OPC HRESULT:0x80070005 (E_ACCESSDENIED))

    除了配置DCOM外,还需要在web.config里面添加设置系统管理员权限的帐号和密码<identity impersonate="true" userName=" ...

  9. VC++ try catch (转)

    VC++ try catch (转)   以前都是用try{} catch(-){}来捕获C++中一些意想不到的异常, 今天看了Winhack的帖子才知道,这种方法在VC中其实是靠不住的.例如下面的代 ...

  10. hadoop之MapReduce WordCount分析

    MapReduce的设计思想 主要的思想是分而治之(divide and conquer),分治算法. 将一个大的问题切分成很多小的问题,然后在集群中的各个节点上执行,这既是Map过程.在Map过程结 ...