You are given an array a consisting of n integers. A subarray (l, r) from array a is defined as non-empty sequence of consecutive elements al, al + 1, ..., ar.

The beauty of a subarray (l, r) is calculated as the bitwise AND for all elements in the subarray:

Beauty(l, r) = al & al + 1 & al + 2 & ... & ar

Your task is to calculate the summation of the beauty of all subarrays (l, r) (1 ≤ l ≤ r ≤ n):

Input

The first line contains an integer T, where T is the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 105), where n is the size of the array a.

The second line of each test case contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106), giving the array a.

Output

For each test case, print a single line containing the summation of the beauty of all subarrays in the given array.

Example

Input
2
3
7 11 9
4
11 9 6 11
Output
40
48

Note

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

A bitwise AND takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits, by multiplying them. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1  ×  1 = 1); otherwise, the result is 0 (1  ×  0 = 0 and 0  ×  0 = 0). This operation exists in all modern programming languages, for example in language C++ and Java it is marked as &.

In the first test case, the answer is calculated as summation of 6 subarrays as follow:

Beauty(1, 1) + Beauty(l, 2) + Beauty(1, 3) + Beauty(2, 2) + Beauty(2, 3) + Beauty(3, 3) (7) + (7 & 11) + (7 & 11 & 9) + (11) + (11 & 9) + (9) = 40 这个题目呢,看完题解就觉得不难了,我开始觉得这个比较难,因为对于位运算不是很熟悉,现在明白了,这种位运算最后转化成二进制去求解比较好,这个有个要求就是只对于连续的才去进行位运算,所以我们就把这个每一个数的位运算的值求出来,然后再把连续的进行位运算。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
int num[maxn][]; int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = ; i <= n; i++)
{
int x;
scanf("%d", &x);
for (int j = ; j <= ; j++)
{
num[i][j] = (x >> j) & ;
}
}
int cnt = ;
ll ans = ;
for (int j = ; j <= ; j++)
{
cnt = ;
for (int i = ; i <= n; i++)
{
if (num[i][j]) cnt++;
else
{
ans += 1ll * cnt*(cnt + ) / * (1ll << j);
cnt = ;
}
}
if (cnt) ans += 1ll * cnt*(cnt + ) / * (1ll << j);
}
printf("%I64d\n", ans);
}
return ;
}

A - Subarrays Beauty gym 位运算 &的更多相关文章

  1. Gym 100818I Olympic Parade(位运算)

    Olympic Parade http://acm.hust.edu.cn/vjudge/contest/view.action?cid=101594#problem/I [题意]: 给出N个数,找出 ...

  2. LeetCode编程训练 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

  3. 算法与数据结构基础 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

  4. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

  5. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  6. 简简单单学会C#位运算

    一.理解位运算 要学会位运算,首先要清楚什么是位运算?程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),位运算就是直接对在内存中的二进制数的每位进行运算操作 二.理解数字进制 上面 ...

  7. SQL Server时间粒度系列----第8节位运算以及设置日历数据表节假日标志详解

    本文目录列表: 1.位运算 2.设置日历数据表节假日标志 3.总结语 4.参考清单列表   位运算   SQL Server支持的按位运算符有三个,分别为:按位与(&).按位或(|).按位异或 ...

  8. js中的位运算

    按位运算符是把操作数看作一系列单独的位,而不是一个数字值.所以在这之前,不得不提到什么是"位": 数值或字符在内存内都是被存储为0和 1的序列,每个0和1被称之为1个位,比如说10 ...

  9. Java中的位运算

    昨天去面试的时候做到了一道Java的位运算题目,发现有个运算符不懂:">>>",今天特地查了一下,并小结一下常见的位运算符号: ~  按位非(NOT)(一元运算) ...

随机推荐

  1. sql server查询语句条件判断字段值是否为NULL

    判断字段是否为null select * from table where c is null    select * from table where c is not null 判断字段是否为空 ...

  2. win10 uwp 商业游戏 1.2.1

    上一个游戏已经告诉大家如何写多个游戏,现在继续写这个无聊的游戏 希望大家在看这篇文章之前先看win10 uwp 商业游戏,在这个文章告诉了大家如何创建游戏. 修改数值 可以从上一篇的博客的游戏看到升级 ...

  3. 动态编程(Dynamic Programming)

    本文素材来自视频,请自备梯子观看:What Is Dynamic Programming and How To Use It Dynamic Programming:动态编程分为如下几步: 将复杂问题 ...

  4. 第五讲 smart qq poll包处理 以及 私聊 群聊消息收发

    发送 poll包 public static void Login_PostPoll() { try { string url = "http://d1.web2.qq.com/channe ...

  5. echarts地图 禁止高亮显示(转载)

    转载来源: https://blog.csdn.net/john1337/article/details/54666759 案例一:在实际应用中希望(没有数据的省会)禁止高亮颜色,解决方案: mapC ...

  6. Java集合性能分析-疯狂Java讲义

    一.各Set实现类的性能分析 HashSet和TreeSet是Set的两个典型实现.HashSet的性能总是比TreeSet好(特别是最常用的添加.查询元素等操作),因为TreeSet需要额外的红黑树 ...

  7. JDK的动态代理-----为接口进行代理

    JDK的动态代理是必须掌握的,动态代理的好处就不用我多说了吧 :) 小弟最近在研究mybatis的源码实现,就开始了解mybatis的Mapper代理机制,为什么接口不用实现类也能代理? 好了,废话不 ...

  8. 使用eclipse初步学习vue.js基础==》v-for的使用 ②

    一.步骤演示 1. 新建一个jsp文件 2. 把vue.js放到Web的js目录下 3. 在jsp中引入vue.js <script src="${pageContext.reques ...

  9. 【资料】Mac OS X 10.9虚拟机镜像及tools

    原文链接 http://bbs.itheima.com/thread-141793-1-1.html 1.首先解压文件,两个同时选中,右键进行解压. 2.打开虚拟机VMware workstation ...

  10. blfs(systemd版本)学习笔记-为桌面环境构建xorg服务

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! lfs准备使用桌面环境,首先需要构建xorg服务 xorg服务项目地址:http://www.linuxfromscratch. ...