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. hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. #include <cstdio> #inclu ...

  2. 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件

    Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...

  3. leetcode:Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  4. HTML & CSS 小总结

    1. web 主机代理商 web hosting company, 让他们的服务器为你的页面服务2. 选择网站名字 例如: www.1234.com3. 寻找 把文件从电脑传到主机的途径4. 把新网站 ...

  5. Qt Assistant介绍

    简介 Qt Assistant也就是我们常说的Qt助手,是一款用于呈现在线文档的工具. 简介 一分钟学会使用 Qt参考文档 Qt Assistant详解 命令行选项 工具窗口 文档窗口 工具栏 菜单 ...

  6. 如何在Windows系统中配置Mysql群集(Mysql Cluster)

    Mysql群集(Cluster)简介 MySQL群集需要有一组计算机,每台计算机的角色可能是不一样的.MySQL群集中有三种节点:管理节点.数据节点和SQL节点.群集中的某计算机可 能是某一种节点,也 ...

  7. mysql-备份和还原(普通还原和binlog还原)

    1)备份 mysqldump -uroot -proot share -l -F > /tmp/share.sql 说明:-l 锁表 -F 刷新日志文件(相当于flush logs) 2)还原( ...

  8. kthread_stop引起的OOP

    1 使用kthread_create创建线程:     struct task_struct *kthread_create(int (*threadfn)(void *data), void *da ...

  9. HDU 1496 Equations 等式(二分+暴力,技巧)

    题意:给出4个数字a,b,c,d,求出满足算式a*x1^2+b*x2^2+c*x3^2+d*x4^2=0的 (x1,x2,x3,x4) 的组合数.x的范围[-100,100],四个数字的范围 [-50 ...

  10. 【JavaScript学习笔记】调用google搜索

    <html> <form method=get action="http://www.google.com/search"> <a href=&quo ...