geeksforgeeks@ Find sum of different corresponding bits for all pairs (Bit manipulation)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=387
Find sum of different corresponding bits for all pairs
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array of N integers, A1, A2 ,…, AN. Find sum of f(Ai, Aj) for all pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 109+7.
Input:
The first line of each input consists of the test cases. The description of T test cases is as follows:
The first line of each test case contains the size of the array, and the second line has the elements of the array.
Output:
In each separate line print sum of all pairs for (i, j) such that 1 ≤ i, j ≤ N and return the answer modulo 109+7.
Constraints:
1 ≤ T ≤ 70
1 ≤ N ≤ 100
-2,147,483,648 ≤ A[i] ≤ 2,147,483,647
Example:
Input:
2
2
2 4
3
1 3 5
Output:
4
8
Working:
A = [1, 3, 5]
We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8
import java.util.*;
import java.lang.*;
import java.io.*; class GFG { public static int difBits(int a, int b) { int tot = 0; for(int i=0; i<32; ++i) {
int mask = (1 << i);
int ai = (a & mask) >> i;
int bi = (b & mask) >> i; tot = (ai == bi)? tot: tot+1;
} return tot;
} public static int func(int[] arr) { int n = arr.length;
int rs = 0; for(int i=0; i<n; ++i) {
for(int j=i+1; j<n; ++j) {
rs += difBits(arr[i], arr[j]);
}
}
rs *= 2;
return rs;
} public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int times = in.nextInt(); for(int i=0; i<times; ++i) {
int n = in.nextInt();
int[] arr = new int[n];
for(int j=0; j<n; ++j) {
arr[j] = in.nextInt();
}
System.out.println(func(arr));
}
}
}
geeksforgeeks@ Find sum of different corresponding bits for all pairs (Bit manipulation)的更多相关文章
- geeksforgeeks@ Minimum sum partition (Dynamic Programming)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...
- BitHacks
备份文件时看到的.我以前居然下过这东西. 2016-12-4 12:05:52更新 纯文本格式真棒.假如使用word写的我能拷过来格式还不乱?? Markdown真好. Bit Hacks By Se ...
- 2015弱校联盟(2) - J. Usoperanto
J. Usoperanto Time Limit: 8000ms Memory Limit: 256000KB Usoperanto is an artificial spoken language ...
- OpenMP初步(英文)
Beginning OpenMP OpenMP provides a straight-forward interface to write software that can use multipl ...
- ural 1114,计数dp
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1114 题意:N个盒子,a个红球,b个蓝球,把求放到盒子中去,没有任何限制,有多少种放法. ...
- Bit Twiddling Hacks
http://graphics.stanford.edu/~seander/bithacks.html Bit Twiddling Hacks By Sean Eron Andersonseander ...
- ping and traceroute(tracert)
1.ping程序简单介绍 这个程序是Mike Muuss编写的.目的是測试另外一台机子是否可达. 运用的协议就是ICMP.运用的是ICMP的回显应答和请求回显两个类型.曾经呢.能ping通说明可以进行 ...
- [BZOJ1112] [POI2008] 砖块Klo (treap)
Description N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另一柱.仓库无限大. 现在希望用最小次 ...
- 用C语言实现Ping程序功能
本文转载自:http://www.ibm.com/developerworks/cn/linux/network/ping/ ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具.p ...
随机推荐
- [Golang] base64加密与解密
首先解释以下什么是base64(来自百度百科): Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045-RFC2049,上面有MIME的详细规范.Base64 ...
- 无法创建链接服务器 "(null)" 的 OLE DB 访问接口 "Microsoft.Ace.OLEDB.12.0" 的实例。
--开启导入功能 exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc ...
- leetcode:Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【转】Android之内存泄漏调试学习与总结
大家有或经常碰到OOM的问题,对吧?很多这样的问题只要一出现相信大家的想法跟小马的一样,就是自己的应用:优化.优化.再优化!而且如果出现类似于OOM这样级别的问题,根本就不好处理,LogCat日志中显 ...
- PDO(PHP Data Object),Mysqli,以及对sql注入等问题的解决
这篇是上一篇 http://www.cnblogs.com/charlesblc/p/5987951.html 的续集. 看有的文章提到mysqli和PDO都支持多重查询,所以下面的url会造成表数据 ...
- Open Explorer Plugin for Eclipse (eclipse 插件 在eclipse里面打开文件目录)
就是在eclipse里面直接打开文件所在的目录地址 只要将下面的jar 文件放到你的 “$ECLIPSE_HOME/plugins” 下面,重启eclipse就ok了 要想卸载的话 停止eclip ...
- UVa 11137 (完全背包方案数) Ingenuous Cubrency
题意:用13.23……k3这些数加起来组成n,输出总方案数 d(i, j)表示前i个数构成j的方案数则有 d(i, j) = d(i-1, j) + d(i, j - i3) 可以像01背包那样用滚动 ...
- OC获取文件(夹)的代码行数
/* 考察NSString NSArray NSFileManager */ #import <Foundation/Foundation.h> /* 计算单个文件的代码行数 path:文 ...
- 以一个上传文件的例子来说 DistributedFileSystem
public class UploadAndDown { public static void main(String[] args) { UploadAndDown uploadAndDown = ...
- Context上下文对象(抄书的)
Servlet上下文 ServletContext 上下文接口 ServletContext接口 每一个应用都有唯一的一个上下文对象,即为ServletContext对象 Servl ...