tags:[数学][预处理]
题解:
我们用一种巧妙的预处理姿势:记录下每一个数位上分别出现了多少个1。
如果第i个数位上出现了cnt[i]个1,那么,在这个数位上产生的“差异值”
为:2*cnt[i]*(n-cnt[i])。sigma (2*cnt[i]*(n-cnt[i])) 即为最终答案。
同类题:
1. 给n个数字,求所有数对异或值之和。[n<=100000]
2. CF766E [在树上这种预处理姿势]

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int MOD = 10000007;
int T, n, x, Time = 0;
int cnt[40];
int main()
{
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
memset(cnt, 0, sizeof(cnt));
for(int i=1;i<=n;i++)
{
scanf("%d", &x);
int tmp = 0;
while(x)
{
tmp ++;
if(x & 1)
{
cnt[tmp] ++;
}
x /= 2;
}
}
int res = 0;
for(int i=1;i<40;i++)
{
res += cnt[i] * (n - cnt[i]);
res %= MOD;
}
res = 2 * res % MOD;
printf("Case %d: %d\n", (++Time), res);
}
}

  

SPOJ - BITDIFF: Bit Difference [神妙の预处理]的更多相关文章

  1. Spoj-BITDIFF Bit Difference

    Given an integer array of N integers, find the sum of bit differences in all the pairs that can be f ...

  2. SPOJ LGLOVE 7488 LCM GCD Love (区间更新,预处理出LCM(1,2,...,n))

    题目连接:http://www.spoj.com/problems/LGLOVE/ 题意:给出n个初始序列a[1],a[2],...,a[n],b[i]表示LCM(1,2,3,...,a[i]),即1 ...

  3. SPOJ REPEATS 后缀数组

    题目链接:http://www.spoj.com/problems/REPEATS/en/ 题意:首先定义了一个字符串的重复度.即一个字符串由一个子串重复k次构成.那么最大的k即是该字符串的重复度.现 ...

  4. 【SPOJ】7258. Lexicographical Substring Search(后缀自动机)

    http://www.spoj.com/problems/SUBLEX/ 后缀自动机系列完成QAQ...撒花..明天or今晚写个小结? 首先得知道:后缀自动机中,root出发到任意一个状态的路径对应一 ...

  5. BZOJ2226: [Spoj 5971] LCMSum

    题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...

  6. 【SPOJ】Transposing is even more fun!

    题意: 给出a.b 表示按先行后列的方式储存矩阵 现在要将其转置 可以交换两个点的位置 求最小操作次数 题解: 储存可以将其视为拉成一条链 设a=5.b=2 则在链上坐标用2^***(a,b)表示为( ...

  7. BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树

    2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...

  8. 【spoj SEQN】【hdu 3439】Sequence

    题意: 给出n.m.k 求C(n,k)*H(n-k)%m的值 H(n-k)为错排公式 题解: 先算H(n-k) 计算H(n)有个通式: H(n)=(-1)^n+((-1)^(n-1))n+((-1)^ ...

  9. What exactly is the difference between WndProc and DefaultWndProc?

    Control.DefWndProc Sends the specified message to the default window procedure. 参数说明:m:The Windows M ...

随机推荐

  1. Lucky7(容斥原理)

    Problem Description When ?? was born, seven crows flew in and stopped beside him. In its childhood, ...

  2. Zigbee折腾之旅:(一)CC2530最小系统

    最近在倒腾Zigbee,准备参加物联网全国大赛,学校有给我们发Zigbee开发板,但是对于喜欢折腾的我来说,用开发板还是不过瘾,起码也得知道怎么去画一块板子.于是乎,在百度一番后就有了下面这篇文章. ...

  3. flume-sink报错 java.lang.IllegalStateException: close() called when transaction is OPEN - you must either commit or rollback first

    1. 确认代码无误(根据情况修改,表示若获得不了数据不会自动commit或者rollback): Event event = channel.take(); if (event == null) { ...

  4. linux系统端口查看和占用的解决方案

    使用netstat命令查看端口占用情况 netstat -tln 查看什么程序占用端口 netstat -tlnp 过滤端口 netstat -tlnp |grep  8080 杀掉进程 kill - ...

  5. Hibernate执行流程和关系映射

    一.Hibernate的执行流程 hibernate作为一个ORM框架,它封装了大量数据库底层的sql语句操作的方法,这样在执行hibernate的过程中理解hibernate的执行流程很有必要. 由 ...

  6. Python入门教程(3)

    人生苦短,我学Pyhton Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于199 ...

  7. Spring Cache扩展:注解失效时间+主动刷新缓存

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  8. mvc4中的过滤器

    过滤器(Filter)把附加逻辑注入到MVC框架的请求处理.实现了交叉关注. 交叉关注:用于整个应用程序,又不适合放在某个局部位置的功能. 过滤器是.NET的注解属性(Attribute),它们对请求 ...

  9. 变量 || 基本数据类型 || if、while语句

    变量名:只能由数字.字母.下划线组成且不能以数字开头:变量名不可以是python内部的关键字   基本数据类型:数字.字符串.布尔值(True/False)   [if条件语句] if 条件:     ...

  10. 基于 Koa平台Node.js开发的KoaHub.js获取/设置会话功能代码

    koa-session2 Middleware for Koa2 to get/set session use with custom stores such as Redis or mongodb ...