题目来源: Ural 1318
给出n个互不相等的整数A[0] - A[n-1],选A[i]同A[j]进行异或运算(结果都 > 0无符号),对结果取lg(以10为底)并取整后记为L[i,j],求n个数之间两两运算得到的L[i,j]之和。
 

 
例如:1 10 30,1 xor 10 = 11,10 xor 30 = 20,1 xor 30 = 31。Sum = Lg(11) * 2 + Lg(20) * 2 + Lg(30) * 2 = (1 + 1 + 1) * 2 = 6(取整)。
由于i j同j i算作2个不同的,因此最终结果要 * 2。
Input
第1行:1个数N,表示整数的数量。(2 <= N <= 50000)
第2 - N + 1行:每行个1数A[i](1 <= A[i] <= 10^18)
Output
输出两两异或后取Lg(以10为底)并取整后的和。

首先建出二进制trie,然后由数据范围知log10(A[i] xor A[j])在0到19,可以枚举每个数和log10的每个取值在trie上查询出现次数
#include<cstdio>
typedef long long i64;
i64 _(){
i64 x=;
int c=getchar();
while(c<)c=getchar();
while(c>)x=x*+c-,c=getchar();
return x;
}
const int N=*;
int n;
i64 a[];
int ch[N][],sz[N],ptr=;
i64 ts[],p10[];
void ins(i64 x){
int w=;
for(int i=;~i;i--){
int d=x>>i&;
if(!ch[w][d])ch[w][d]=++ptr;
w=ch[w][d];
++sz[w];
}
}
int find(i64 x,i64 y){
int s=,w=;
for(int i=;~i;i--){
int d1=x>>i&,d2=y>>i&;
if(d2)s+=sz[ch[w][d1^d2^]];
w=ch[w][d1^d2];
}
return s;
}
int main(){
n=_();
for(int i=;i<n;i++){
a[i]=_();
ins(a[i]);
}
p10[]=;
for(int i=;i<=;i++)p10[i]=p10[i-]*;
for(int i=;i<n;i++){
for(int j=;j<=;j++)ts[j]+=find(a[i],p10[j]);
}
ts[]=1ll*n*n-ts[];
for(int i=;i>;i--)ts[i]-=ts[i-];
ts[]-=n;
i64 ans=;
for(int i=;i<=;i++)ans+=(i-)*ts[i];
printf("%lld",ans);
return ;
}
 

51nod 1150 Logarithm的更多相关文章

  1. 【51Nod 1244】莫比乌斯函数之和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...

  2. 51Nod 1268 和为K的组合

    51Nod  1268  和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...

  3. 51Nod 1428 活动安排问题

    51Nod   1428  活动安排问题 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活 ...

  4. 51Nod 1278 相离的圆

    51Nod 1278 相离的圆 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278 1278 相离的圆 基 ...

  5. 【51Nod 1501】【算法马拉松 19D】石头剪刀布威力加强版

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1501 dp求出环状不连续的前缀和,剩下东西都可以算出来,比较繁琐. 时间 ...

  6. 【51Nod 1622】【算法马拉松 19C】集合对

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1622 简单题..直接暴力快速幂 #include<cstdio&g ...

  7. 【51Nod 1616】【算法马拉松 19B】最小集合

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1616 这道题主要是查询一个数是不是原有集合的一个子集的所有数的gcd. ...

  8. 【51Nod 1674】【算法马拉松 19A】区间的价值 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1674 对区间分治,统计\([l,r]\)中经过mid的区间的答案. 我的 ...

  9. 随便玩玩系列之一:SPOJ-RNG+51nod 算法马拉松17F+51nod 1034 骨牌覆盖v3

    先说说前面的SPOJ-RNG吧,题意就是给n个数,x1,x2,...,xn 每次可以生成[-x1,x1]范围的浮点数,把n次这种操作生成的数之和加起来,为s,求s在[A,B]内的概率 连续形的概率 假 ...

随机推荐

  1. Java 中的数组操作

    前言 在Java中,有很多封装好的类可以用来操纵数组(排序,复制等等),使得数组使用起来非常的方便.这就是高级语言带来的好处. 代码示例 - 一维数组 package test; import jav ...

  2. 199. Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  3. C特殊浮点值NaN

    特殊浮点值NaN(Not-a-Number),例如asin()函数返回反正弦值,所以输入参数不能大于1,否则函数返回NaN值,printf()显示为nan,NaN或类似形式.

  4. hilbert矩阵 分类: 数学 2015-07-31 23:03 2人阅读 评论(0) 收藏

    希尔伯特矩阵 希尔伯特矩阵是一种数学变换矩阵 Hilbert matrix,矩阵的一种,其元素A(i,j)=1/(i+j-1),i,j分别为其行标和列标. 即: [1,1/2,1/3,--,1/n] ...

  5. Android 客户端和服务器 json交互

    http://www.cnblogs.com/jyan/articles/2544974.html 1.JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. ...

  6. mysql数据库从库同步延迟的问题

    在从服务器上执行show slave status;可以查看到很多同步的参数,我们需要特别注意的参数如下,希望文章对各位会有所帮助. 在从服务器上执行show slave status;可以查看到很多 ...

  7. InputStreamReader和OutputStreamWriter

    public class ClientSocket {   public static void main(String args[]) {         String host = "1 ...

  8. linux 大并发下 内核优化

     To support over 500k users, you *need* - A 64 bits hardware/kernel (AMD64, Opterons) - At least 8GB ...

  9. Softmax回归(使用theano)

    # coding:utf8 import numpy as np import cPickle import theano import os import theano.tensor as T cl ...

  10. lucene 索引流程整理笔记

    索引的原文档(Document). 为了方便说明索引创建过程,这里特意用两个文件为例: 文件一:Students should be allowed to go out with their frie ...