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)(一元运算) ...
随机推荐
- 介绍一款文档神器:pandoc
http://pandoc.org/ 因为工作需要,将一批markdown的文档转换成word文档,找来找去,这个pandoc真是神器 啊,推荐给大家 If you need to convert f ...
- [日常] HTTP的媒体类型
HTTP的媒体类型 1.MIME类型的数据格式标签(MultIpurpose Internet Mail Extension) 2.最初用于电子邮件系统之间搬移,多用途互联网邮件扩展 3.MIME类型 ...
- 16-pymysql模块的使用
[转]16-pymysql模块的使用 本节重点: pymysql的下载和使用 execute()之sql注入 增.删.改:conn.commit() 查:fetchone.fetchmany.fetc ...
- HTML 介绍及标签
HTML htyper text markup language 即超文本标记语言 超文本: 就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素. 标记语言: 标记(标签)构成的语言. 标签 ...
- cf932E. Team Work(第二类斯特灵数 组合数)
题意 题目链接 Sol 这篇题解写的非常详细 首先要知道第二类斯特灵数的一个性质 \[m^n = \sum_{i = 0}^m C_{n}^i S(n, i) i!\] 证明可以考虑组合意义:\(m^ ...
- GIS开发之计算四参数,七参数
一.四参数 想要通过控制点计算四参数,首先需要知道四参数的相关原理,推荐这篇文章: http://www.docin.com/p-1197326043.html 根据上面的计算公式,使用最小二乘法计算 ...
- ViewPager实现滑动翻页效果
实现ViewPager的滑动翻页效果可以使用ViewPager的setPageTransformer方法,如下: import android.content.Context; import andr ...
- CSS的基本语法
W3School离线手册(2017.03.11版)下载:https://pan.baidu.com/s/1c6cUPE7jC45mmwMfM6598A CSS(层叠样式表) ...
- Android 组件的三种点击事件写法
一.准备工作 1.项目结构 2.布局文件(activity_main.xml) <?xml version="1.0" encoding="utf-8"? ...
- ie6常见的兼容性问题
1.<!DOCTYPE HTML>文档类型的声明. 产生条件:IE6浏览器,当我们没有书写这个文档声明的时候,会触发IE6浏览器的怪异解析现象: 解决办法:书写文档声明. 2.不同浏览器当 ...