Bit Counting Sequence

시간 제한2초메모리 제한1024 MB

요약
팝카운트 값의 수열이 주어질 때, 어떤 음이 아닌 정수 x부터 시작하는 연속한 정수들의 팝카운트와 같은지 판별하고 가장 작은 x를 구한다.
난이도

보통10점 중 7점

유형
비트 연산, 수학, 그리디
정답자
아직 제출이 없습니다

문제

For a non-negative integer xx, let p(x)p(x) be the number of ones in the binary representation of xx. For example, p(26)=3p(26) = 3 because 26=(11010)_226 = (11010)\_2.

You are given a sequence of nn integers (a_1,a_2,…,a_n)(a\_1, a\_2, \dots , a\_n). Your task is to determine whether there exists a non-negative integer xx such that (p(x),p(x+1),…,p(x+n−1))(p(x), p(x + 1), \dots , p(x + n - 1)) is equal to (a_1,a_2,…,a_n)(a\_1, a\_2, \dots , a\_n). Furthermore, if it exists, compute the smallest xx satisfying the condition.

입력

The first line of input contains one integer tt (1≤t≤10001 ≤ t ≤ 1000) representing the number of test cases. After that, tt test cases follow. Each of them is presented as follows.

The first line contains one integer nn (1≤n≤500,0001 ≤ n ≤ 500\\, 000). The second line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \dots , a\_n (0≤a_i≤600 ≤ a\_i ≤ 60 for all ii).

The sum of nn across all test cases in one input file does not exceed 500,000500\\, 000.

출력

For each test case, output the smallest non-negative integer xx satisfying the condition above. If there is no such xx, output -1 instead.

예제1

  1. 예제 1

    입력
    4
    5
    3 3 4 1 2
    3
    2 1 2
    2
    60 60
    2
    8 0
    
    예상 출력
    13
    3
    2305843009213693949
    -1