A - Subarrays Beauty Gym - 101532A (二进制按位与累加)

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.

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

题意:对任意的区间(l,r)计算他们的按位与运算和。

思路:算每一位的贡献,按二进制压位进行计算

代码:

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for (int a = b; a <= c; a++)
#define RFOR(a, b, c) for (int a = b; a >= c; a--)
#define off ios::sync_with_stdio(0)
#define sc(a) scanf("%d",&a)
#define pr(a) printf("%d\n",a);
#define SC(n,m) scanf("%d%d",&n,&m)
bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; }

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;//1e10
const int mod = 1e9 + 7;
const int Maxn = 1e5 + 5;
const int M = Maxn * 20;
const double pi = acos(-1.0);
const double eps = 1e-8;

ll a[Maxn], num[31];

void solve() {
    int n;
    scanf("%d", &n);
    mem(num, 0);
    ll sum = 0;
    for (int i = 1; i <= n;i++) {
        scanf("%lld", &a[i]);
        for (int j = 0; j <= 30; j++) {
            if (a[i] >> j & 1) { // 判断每一位是不是1
                num[j]++;// 如果是,记录
                //cout << "before:" << ((ll)num[j] << j) << endl;
                sum += (ll)num[j] << j;//转回10进制累加
                //cout << num[j] << "---" << sum << "---" << endl << endl;
            }
            else num[j] = 0;//不是就归0, 与操作的特点
        }
    }
    printf("%lld\n", sum);
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    scanf("%d", &T);
    while (T--) {
        solve();
    }
    return 0;
}
 

猜你喜欢

转载自www.cnblogs.com/AlexLINS/p/12714414.html