B. GCD Partition

B. 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 $a$ of length $n$. On this array, do the following:

  • select an integer $k > 1$;
  • split the array into $k$ subsegments $^\dagger$;
  • calculate the sum in each of $k$ subsegments and write these sums to another array $b$ (where the sum of the subsegment $(l, r)$ is ${\sum_{j = l}^{r}a_j}$);
  • the final score of such a split will be $\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 $k$ subsegments is $k$ pairs of numbers $(l_1, r_1), (l_2, r_2), \ldots, (l_k, r_k)$ such that $l_i \le r_i$ and for every $1 \le j \le k - 1$ $l_{j + 1} = r_j + 1$, also $l_1 = 1$ and $r_k = n$. These pairs represent the subsegments.

$^\ddagger$ $\gcd(b_1, b_2, \ldots, b_k)$ stands for the greatest common divisor (GCD) of the array $b$.

Input

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

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

The second line contains $n$ integers $a_1, a_2, a_3, \ldots, a_n$ ($1 \le a_i \le 10^9 $) — the array $a$ itself.

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

Output

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

Example

input

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

output

4
1
5
3
1
21

Note

In the first test case, you can choose $k = 2$ and split the array into subsegments $(1, 2)$ and $(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 = 3$ and split the array into subsegments $(1, 2), (3, 5), (6, 6)$.

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

 

解题思路

  可以证明最优解必定出现在将序列分成两段的情况中。

  假如将序列分成$m > 2$段,那么此时的答案是$\gcd(b_1, b_2, \ldots, b_m)$,有$\gcd(b_1, b_2, \ldots, b_m) \le \gcd(b_1 + b_2, b_3, \ldots, b_m)$,这是因为对于$b_1, \ldots, b_m$的任意一个公约数$d$,都一定能整除$b_1 + b_2, b_3, \ldots, b_m$,即也是$b_1 + b_2, b_3, \ldots, b_m$的公约数。因此就有$\gcd(b_1, b_2, \ldots, b_m) \le \gcd(b_1 + b_2, b_3, \ldots, b_m)$。以此类推,为了得到最大值最终一定会变成$\gcd(b_1, b_2)$的形式。因此可以枚举两段的分界点来求最大值。

  补充另外一个方法证明$\gcd({a,b,c}) \leq \gcd({a+b,c})$。设$a = px, \ b = py, \ c = pz$,那么$a + b = p(x+y)$。$\gcd({a,b,c}) = \gcd({px,py,pz}) = p$,$\gcd({a+b,c}) = \gcd({p(x+y),pz}) = p \cdot \gcd({(x+y),z}) \geq p = \gcd({a,b,c})$。

  AC代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 2e5 + 10;
 7 
 8 LL s[N];
 9 
10 LL gcd(LL a, LL b) {
11     return b ? gcd(b, a % b) : a;
12 }
13 
14 void solve() {
15     int n;
16     scanf("%d", &n);
17     for (int i = 1; i <= n; i++) {
18         // 因为写成scanf("%d", s + i)被hack了,由于有多组测试数据,因此要保证s[i]的64位均为0
19         scanf("%lld", s + i);
20         s[i] += s[i - 1];
21     }
22     LL ret = 1;
23     for (int i = 1; i < n; i++) {
24         ret = max(ret, gcd(s[i], s[n] - s[i]));
25     }
26     printf("%lld\n", ret);
27 }
28 
29 int main() {
30     int t;
31     scanf("%d", &t);
32     while (t--) {
33         solve();
34     }
35     
36     return 0;
37 }

 

参考资料

  Codeforces Round #846 (Div. 2) — Tutorial:https://codeforces.com/blog/entry/111841

  CF1708B-solution:https://www.luogu.com.cn/blog/NK-TBW2765/cf1708b-solution

posted @ 2023-01-27 17:16  onlyblues  阅读(56)  评论(0编辑  收藏  举报
Web Analytics