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. 【百度之星2014~初赛(第二轮)解题报告】Chess

    声明 笔者近期意外的发现 笔者的个人站点http://tiankonguse.com/ 的非常多文章被其他站点转载.可是转载时未声明文章来源或參考自 http://tiankonguse.com/ 站 ...

  2. char图表

    首先看一下chart图表相应的各个属性: 要想使用chart图表,首先须要安装MSChart.exe:安装完后,工具箱里仍然没有,此时要在web.Config文件中加入以下代码: <span s ...

  3. 老生常谈的Javascript作用域问题

    在前端学习中,作用域这个问题一直被广泛提起,什么是作用域,什么又是作用域链?在Javascript中,怎么去理解这些概念都是学好这门语言的关键,所以在学习前端开发的过程中,我需要也很有必要去学习和总结 ...

  4. 未能加载文件或程序集“System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”

    最近用vs2012发布程序,然后将更新后的程序文件部署到服务器上,由于服务器上本来有此系统,所以只更新了修改的文件 . 进行系统登录时提示:未能加载文件或程序集“System.Web.Extensio ...

  5. Application 可以存储全局变量

    Application.Lock(); Application["Name"]="小亮" Application.UnLock(); Response.Writ ...

  6. mysql中常用的语句整理

    mysql中常用的语句: 1:创建带自增长的主键的表 DROP TABLE IF EXISTS user_login ; CREATE TABLE user_login (   user_id INT ...

  7. DTO学习系列之AutoMapper(一)

    一.前言 DTO(Data Transfer Object)数据传输对象,注意关键字“数据”两个字,并不是对象传输对象(Object Transfer Object),所以只是传输数据,并不包含领域业 ...

  8. UVa 679 小球下落 简单模拟题,树

    题目大意:给你一个完全二叉树,并且给他们编号,编号规则为左子树为2*k,右子树为2*k+1,每一个节点 上都有一个开关,初始时开关都处于关闭状态,小球碰到节点就会改变该点的开关的状态.然后给你I个小球 ...

  9. 安装laravel

    # 安装laravel 安装composer #安装 curl -sS https://getcomposer.org/installer | php #添加到PATH sudo mv compose ...

  10. 使用yii2实现读写分离(MySQL主从数据库)

    读写分离(Read/Write Splitting). 1.原理:让主数据库(master)处理事务性增.改.删操作(INSERT.UPDATE.DELETE),而从数据库(slave)处理SELEC ...