CF1780B.GCD Partition

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

GCD Partition

While at Kira's house, Josuke saw a piece of paper on the table with a task written on it.

The task sounded as follows. There is an array aa of length nn. On this array, do the following:

  • select an integer k>1k \gt 1;
  • split the array into kk subsegments ^\dagger;
  • calculate the sum in each of kk subsegments and write these sums to another array bb (where the sum of the subsegment (l,r)(l, r) is umj=lraj{ um_{j = l}^{r}a_j});
  • the final score of such a split will be gcd(b1,b2,,bk)\gcd(b_1, b_2, \ldots, b_k)^\ddagger.

The task is to find such a partition that the score is maximum possible. Josuke is interested in this task but is not strong in computer science. Help him to find the maximum possible score.

^\dagger A division of an array into kk subsegments is kk pairs of numbers (l1,r1),(l2,r2),,(lk,rk)(l_1, r_1), (l_2, r_2), \ldots, (l_k, r_k) such that liril_i \le r_i and for every 1jk11 \le j \le k - 1 lj+1=rj+1l_{j + 1} = r_j + 1, also l1=1l_1 = 1 and rk=nr_k = n. These pairs represent the subsegments.

^\ddagger gcd(b1,b2,,bk)\gcd(b_1, b_2, \ldots, b_k) stands for the greatest common divisor (GCD) of the array bb.

Input

The first line contains a single number tt (1t1041 \le t \le 10^4) — the number of test cases.

For each test case, the first line contains one integer nn (2n21052 \le n \le 2 \cdot 10^5) — the length of the array aa.

The second line contains nn integers a1,a2,a3,,ana_1, a_2, a_3, \ldots, a_n (1ai1091 \le a_i \le 10^9) — the array aa itself.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case print a single integer — the maximum score for the optimal partition.

Note

In the first test case, you can choose k=2k = 2 and split the array into subsegments (1,2)(1, 2) and (3,4)(3, 4).

Then the score of such a partition will be equal to $\gcd(a_1 + a_2, a_3 + a_4) = \gcd(2 + 2, 1 + 3) = \gcd(4, 4) = 4$.

In the fourth test case, you can choose k=3k = 3 and split the array into subsegments (1,2),(3,5),(6,6)(1, 2), (3, 5), (6, 6).

The split score is gcd(1+2,1+1+1,3)=3\gcd(1 + 2, 1 + 1 + 1, 3) = 3.

Samples

6
4
2 2 1 3
2
1 2
3
1 4 5
6
1 2 1 1 1 3
10
12 30 37 88 12 78 89 17 2 12
6
7 7 7 7 7 7
4
1
5
3
1
21

在线编程 IDE

建议全屏模式获得最佳体验