Hug the princess(思维,位运算)

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 + 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;
}
时间: 2024-07-28 21:37:16

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

UESTC--1041--Hug the princess(位运算)

 Hug the princess Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu Submit Status 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

Codeforces Round #613 (Div. 2) D - Dr. Evil Underscores(思维,位运算)

?? ?? ?? 题意:对于一个数组,求一个数字与数组每个元素异或之后的最大值最小,求这个最大值 又是位运算,,题目给出数组元素范围在2^30以内,二进制最多30位,从最高位开始贪心,如果此位置的数组元素有的是1有的是0,最后肯定取1,否则取0,还有就是分组讨论,因为每个bit位只能满足原数组中一部分元素异或后为1 #define int ll vector<int>a; int solve(vector<int>v,int bit) { if(bit<=0||v.size(

【位运算生成枚举序列】

如果遇到这样的枚举情况,一个数组a[6],  每个值只能取0或者1,共有64种情况,我们需要一次枚举出来. 方法1:从0到63一次转换成二进制填充进去(麻烦). 方法2:位运算实现 代码: int main(void) { int cnt=0; for(int k=0; k<64; k++){ //枚举0---63 for(int j=0; j<6; j++) //6个数组位 if(k&(1<<j)) printf("1 "); //可以在此处将1填充到

位运算及其应用实例(1)

摘要 位运算是C/C++中的基本运算之一,即便是这样,它对大多数程序员来说是一个比较陌生的运算——大多数程序员很少使用位运算.本篇先简要介绍基本的位运算操作符及其用法(何时使用),然后介绍位运算符的几个典型应用: (1)      三种不用临时变量交换两个整数的实例,并分析每个实例的优缺点 (2)      进制转换,通过位运算实现将十进制数按二进制和十六进制输出,并得出一个通用的,用于将十进制按照2的n次方进制输出的程序. (3)      给出利用位运算实现的计算整数的二进制表示中有多少个1

位运算

位运算的实际应用场景 http://blog.csdn.net/zmazon/article/details/8262185

POJ 1781 In Danger Joseph环 位运算解法

Joseph环,这次模固定是2.假设不是固定模2,那么一般时间效率是O(n).可是这次由于固定模2,那么能够利用2的特殊性,把时间效率提高到O(1). 规律能够看下图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" > 具体具体解析请看大师Knuth的Concrete m

位运算总结&amp;拾遗

JavaScript 位运算总结&拾遗 最近补充了一些位运算的知识,深感位运算的博大精深,此文作为这个系列的总结篇,在此回顾下所学的位运算知识和应用,同时也补充下前文中没有提到的一些位运算知识. 把一个数变为大于等于该数的最小的2的幂 一个数为2的幂,那么该数的二进制码只有最高位是1. 根据这个性质,我们来举个栗子,比如有数字10,转为二进制码后为: 1 0 1 0 我们只需把 0 bit的位置全部用1填充,然后再把该二进制码加1就ok了.而x | (x + 1)正好可以把最右边的0置为1,可是

Java I/O : Bit Operation 位运算

Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter    :BYSocket 泥瓦匠喜欢Java,文章总是扯扯Java. I/O 基础,就是二进制,也就是Bit. 一.Bit与二进制 什么是Bit(位)呢?位是CPU处理或者数据存储最小的单元.类似于很小很小的开关,一开一关,表示为1或者0.所以,这就是计算机处理任何数据的"细胞",要谨记.

mysql位运算

1.MOD(X1,X2) 求余运算,返回余数同"%" 2.X1 DIV X2 除法运算返回商,同"/" 3.如果除数为0,那么结果为NULL. 4.<=>和= 是一样的,也是用来判断操作数是否相等的.不同的是<=>可以用来判断null,=不能判断null. 例:select null<=>null 结果1 逻辑运算符 1.与&&或and:所有操作数不为0且不为null时,结果为1,任何一操作数为0,结果为0,存在一