Good Pairs

All submissions for this problem are available.
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given N integers: A_{1}, A_{2}, ..., A_{N}. You need to count the number of pairs of indices (i, j) such that 1 ≤ i < j ≤ N and A_{i}  A_{j} ≤ max(A_{i}, A_{j}).
Note: A_{i}  A_{j} refers to bitwise OR.
Input
 The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
 The first line of each testcase contains a single integer: N
 The second line of each testcase contains N integers: A_{1}, A_{2}, ..., A_{N}.
Output
For each test case, output a single line containing the answer for that test case.
Constraints
 1 ≤ T ≤ 20
 1 ≤ N ≤ 10^{6}
 0 ≤ A_{i} ≤ 10^{6}
 1 ≤ Sum of N over all test cases ≤ 10^{6}
Subtasks
Subtask #1 (20 points): 1 ≤ N ≤ 10^{3}
 Original constraints
Example
Input: 1 3 1 2 3 Output: 2
Explanation
There are three possible pairs of indices which satisfy 1 ≤ i < j ≤ N: (1, 2), (1, 3) and (2, 3). Let us see which of those satisfy A_{i}  A_{j} ≤ max(A_{i}, A_{j}):
 (1, 2): A_{1}  A_{2} = 1  2 = (01)_{2}  (10)_{2} = (11)_{2} = 3. But max(A_{1}, A_{2}) = max(1, 2) = 2, and 3 ≤ 2 is not correct. Hence this is not a valid pair.
 (1, 3): A_{1}  A_{3} = 1  3 = (01)_{2}  (11)_{2} = (11)_{2} = 3. And max(A_{1}, A_{3}) = max(1, 3) = 3, and 3 ≤ 3 is correct. Hence this is a valid pair.
 (2, 3): A_{2}  A_{3} = 2  3 = (10)_{2}  (11)_{2} = (11)_{2} = 3. And max(A_{2}, A_{3}) = max(2, 3) = 3, and 3 ≤ 3 is correct. Hence this is a valid pair.
So there are a total of 2 valid pairs, and hence the answer is 2.
Author:  arpa 
Editorial  https://discuss.codechef.com/problems/MAXOR 
Tags  arpa, dp+bitmask, ltime50 
Date Added:  28072017 
Time Limit:  4 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.5, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 