#P17332. 「TPOI-2B」AND and Subsequence

    ID: 19745 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>数学贪心并查集洛谷原创O2优化位运算洛谷月赛

「TPOI-2B」AND and Subsequence

Problem Description

Given a sequence aa of length nn. Define one operation as follows: choose an interval [l,r][l,r], let x=al&al+1&⋯&arx=a_l\&a_{l+1}\&\cdots\&a_r. For any ii such that l≤i≤rl \le i \le r, set ai←ai−xa_i\leftarrow a_i-x. Ask: what is the minimum number of operations needed to make all numbers in the sequence become 00?

Here, &\& denotes bitwise AND.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, which denotes the number of test cases.

For each test case:

The first line contains a positive integer nn.

The second line contains nn non-negative integers aia_i.

Output Format

For each test case, output one integer per line, which is the answer.

4
4
1 2 3 1
4
1 2 2 2
4
0 1 1 0
4
1 0 2 4
3
2
1
3

Hint

For 20%20\% of the testdata, it is guaranteed that 1≤T,n≤81\le T,n\le 8.

For 40%40\% of the testdata, it is guaranteed that 1≤n≤5001\le n\le 500.

For another 15%15\% of the testdata, it is guaranteed that ai∈{0,1}a_i\in\{0,1\}.

For another 15%15\% of the testdata, it is guaranteed that aia_i are generated uniformly at random within the value range.

For 100%100\% of the testdata, it is guaranteed that 1≤T≤501\le T\le 50, 1≤n,∑n≤1051\le n,\sum n\le 10^5, 0≤ai≤231−10\le a_i\le 2^{31}-1.

Translated by ChatGPT 5