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)的更多相关文章

  1. geeksforgeeks@ Minimum sum partition (Dynamic Programming)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...

  2. BitHacks

    备份文件时看到的.我以前居然下过这东西. 2016-12-4 12:05:52更新 纯文本格式真棒.假如使用word写的我能拷过来格式还不乱?? Markdown真好. Bit Hacks By Se ...

  3. 2015弱校联盟(2) - J. Usoperanto

    J. Usoperanto Time Limit: 8000ms Memory Limit: 256000KB Usoperanto is an artificial spoken language ...

  4. OpenMP初步(英文)

    Beginning OpenMP OpenMP provides a straight-forward interface to write software that can use multipl ...

  5. ural 1114,计数dp

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1114 题意:N个盒子,a个红球,b个蓝球,把求放到盒子中去,没有任何限制,有多少种放法. ...

  6. Bit Twiddling Hacks

    http://graphics.stanford.edu/~seander/bithacks.html Bit Twiddling Hacks By Sean Eron Andersonseander ...

  7. ping and traceroute(tracert)

    1.ping程序简单介绍 这个程序是Mike Muuss编写的.目的是測试另外一台机子是否可达. 运用的协议就是ICMP.运用的是ICMP的回显应答和请求回显两个类型.曾经呢.能ping通说明可以进行 ...

  8. [BZOJ1112] [POI2008] 砖块Klo (treap)

    Description N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另一柱.仓库无限大. 现在希望用最小次 ...

  9. 用C语言实现Ping程序功能

    本文转载自:http://www.ibm.com/developerworks/cn/linux/network/ping/ ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具.p ...

随机推荐

  1. Spring高级事务管理难点剖析

    1Spring事务传播行为 所谓事务传播行为就是多个事务方法相互调用时,事务如何在这些方法间传播.Spring支持7种事务传播行为 PROPAGATION_REQUIRED(加入已有事务) 如果当前没 ...

  2. js与flash结合使用

    最近,做个了一个falsh和js 通信的小东西. flash负责接收参数和返回结果.js负责处理信息,接收返回结果,将结果返回到服务器端. 听着很复杂,做起来页面还是很简单的.用的技术还是不少的,fl ...

  3. Perl date time

    use Time::HiRes qw(time);use POSIX qw(strftime); my $t = time;my $date = strftime "%Y%m%d %H:%M ...

  4. easyui datagrid列中使用tooltip

    要实现这样一个效果:数据加载到DATAGRID中,鼠标移至某一列时,会弹出tooltip提示框. 最初的实现方法: { field: 'Reply', title: '备注', width: 220, ...

  5. UIImagePicker照片选择器

    UIImagePickerController 1.+(BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType ...

  6. .net remoting 客户端与服务端绑定事件,一部电脑当服务器,另一部当客户端,发布后没法接收远程错误信息。

    可以是用下面代码抛出远程错误,客户端和服务端都要设置,因为服务端事件回调时角色变成了远程客户端了. RemotingConfiguration.CustomErrorsMode = CustomErr ...

  7. js屏弊错误

    <SCRIPT language=javascript> <!-- window.onerror=function(){return true;} // --> </SC ...

  8. dede栏目调用大全

    A:侧边栏常用的当前栏目的父栏目调用(5.7) 1.在include/common.inc.php增加函数 function getTopCategoryName($cid=0) { global $ ...

  9. (六)6.18 cnn 的反向传导算法

    本文主要内容是 CNN 的 BP 算法,看此文章前请保证对CNN有初步认识,可参考Neurons Networks convolutional neural network(cnn). 网络表示 CN ...

  10. Android 高仿腾讯旗下app的 皮肤加载技术

    http://www.cnblogs.com/punkisnotdead/p/4968851.html 以前写的这篇文章 可以高仿出 知乎 新浪微博等 绝大多数app的换肤技术,但是遗漏了腾讯的效果, ...