A - Subarrays Beauty gym 位运算 &
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
2
3
7 11 9
4
11 9 6 11
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 位运算 &的更多相关文章
- Gym 100818I Olympic Parade(位运算)
Olympic Parade http://acm.hust.edu.cn/vjudge/contest/view.action?cid=101594#problem/I [题意]: 给出N个数,找出 ...
- LeetCode编程训练 - 位运算(Bit Manipulation)
位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...
- 算法与数据结构基础 - 位运算(Bit Manipulation)
位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- 简简单单学会C#位运算
一.理解位运算 要学会位运算,首先要清楚什么是位运算?程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),位运算就是直接对在内存中的二进制数的每位进行运算操作 二.理解数字进制 上面 ...
- SQL Server时间粒度系列----第8节位运算以及设置日历数据表节假日标志详解
本文目录列表: 1.位运算 2.设置日历数据表节假日标志 3.总结语 4.参考清单列表 位运算 SQL Server支持的按位运算符有三个,分别为:按位与(&).按位或(|).按位异或 ...
- js中的位运算
按位运算符是把操作数看作一系列单独的位,而不是一个数字值.所以在这之前,不得不提到什么是"位": 数值或字符在内存内都是被存储为0和 1的序列,每个0和1被称之为1个位,比如说10 ...
- Java中的位运算
昨天去面试的时候做到了一道Java的位运算题目,发现有个运算符不懂:">>>",今天特地查了一下,并小结一下常见的位运算符号: ~ 按位非(NOT)(一元运算) ...
随机推荐
- Python网络编程之Socket的简单实现
一.引入 关于Python的网络编程,最基础莫过于socket了. socket,又称“套接字”,网络上的两个程序如果想要实现双向的数据通信,需要建立连接,这个连接的一端就称为一个socket. py ...
- T-SQL基础(四)之集合运算
三个运算符 T-SQL支持三个集合运算符:UNION.INTERSECT.EXCEPT. 集合运算符查询的一般形式如下: Query1 <set_operator> Query2 -- 这 ...
- springMVC_10拦截器
一,简介 拦截器概念和struts概念一致 实现拦截器 实现HandlerInterceptor接口 配置拦截器 <mvc:interceptors> <mvc:intercepto ...
- hihoCoder编程练习赛69
题目1 : 偶数长度回文子串 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个小写字母字符串,请判断它是否有长度为偶数的非空连续回文子串 输入 输入包含多组数据. ...
- CSS3使用transition属性实现过渡效果
transition属性目的是让css的一些属性(如background)的以平滑过渡的效果出现.它是一个合并属性,是由以下四个属性组合而成: transition-property:设置应用过渡的C ...
- 获取邮箱的DNS和MX 工具类
1.导入Maven DNS 包: <dependency> <groupId>dnsjava</groupId> <artifactId>dnsja ...
- Linux 进程调度的主要策略
1.Linux 下进程分为5种类别,分别是停止类.截止类.实时类.公平类.空闲类, 每种类别都有一个运行队列,每次调度时,就是先按照类别优先级排序,再按照每个类别内的最高优先级任务调度运行. 文件:c ...
- [Python][小知识][NO.3] Python 使用系统默认浏览器打开指定URL的网址
1.前言 一般用到的地方: GUI交互界面下,单击某个按钮实现打开指定网址. 某帮助菜单项目,需要跳转网页显示时. O.O 某XX程序,需要植入网页弹窗广告时... 2.方法 调用 webbrowse ...
- python 饥饿的小易(网易笔试题)
本周早些时候,学弟给我发了一道网易的笔试题,饥饿的小易,感觉有点意思-分享给大家 题目描述: 小易总是感觉饥饿,所以作为章鱼的小易经常出去寻找贝壳吃.最开始小易在一个初始位置x_0.对于小易所处的当前 ...
- Ubuntu搭建NFS服务器,NFS协议详细分析
目录 1. Ubuntu搭建NFS服务器 2. NFS协议分析 2.1 实验拓扑: 2.2 在kali抓包分析 1. Ubuntu搭建NFS服务器 NFS(Network FileSystem,网 ...