CF1780B.GCD Partition

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 ∑j=lraj{\sum_{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 li≤ril_i \le r_i and for every 1≤j≤k−11 \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.

在吉良家时,仗助看到桌上有一张纸,上面写着一道题目。

题目内容如下:给定一个长度为 nn 的数组 aa。对这个数组执行以下操作:

  • 选择一个整数 k>1k \gt 1;
  • 将数组划分为 kk 个子段†^\dagger;
  • 分别计算这 kk 个子段的和,并将这些和写入另一个数组 bb(其中子段 (l,r)(l, r) 的和为 ∑j=lraj{\sum_{j = l}^{r}a_j});
  • 此划分的最终得分为 gcd⁡(b1,b2,…,bk)‡\gcd(b_1, b_2, \ldots, b_k)^\ddagger。

任务是找到一种划分方式,使得得分尽可能大。仗助对这道题很感兴趣,但编程能力不强。请帮助他找出可能的最大得分。

†^\dagger 数组划分为 kk 个子段,是指 kk 对数 (l1,r1),(l2,r2),…,(lk,rk)(l_1, r_1), (l_2, r_2), \ldots, (l_k, r_k),满足 li≤ril_i \le r_i,且对每个 1≤j≤k−11 \le j \le k - 1 都有 lj+1=rj+1l_{j + 1} = r_j + 1,同时 l1=1l_1 = 1、rk=nr_k = n。这些数对表示各子段。

‡^\ddagger gcd⁡(b1,b2,…,bk)\gcd(b_1, b_2, \ldots, b_k) 表示数组 bb 的最大公约数(GCD)。

输入格式

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

For each test case, the first line contains one integer nn (2≤n≤2⋅1052 \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 ($1 \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 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

对于每个测试用例,第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 数组 aa 的长度。

第二行包含 nn 个整数 a1,a2,a3,…,ana_1, a_2, a_3, \ldots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 数组 aa 本身。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

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

对于每个测试用例,输出一个整数——最优划分所能得到的最大得分。

输入输出样例

  • 输入#1

    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

    输出#1

    4
    1
    5
    3
    1
    21

说明/提示

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⁡(a1+a2,a3+a4)=gcd⁡(2+2,1+3)=gcd⁡(4,4)=4\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.

在第一个测试用例中,你可以选择 k=2k = 2,并将数组划分为子段 (1,2)(1, 2) 和 (3,4)(3, 4)。

此时该划分的得分为 gcd⁡(a1+a2,a3+a4)=gcd⁡(2+2,1+3)=gcd⁡(4,4)=4\gcd(a_1 + a_2, a_3 + a_4) = \gcd(2 + 2, 1 + 3) = \gcd(4, 4) = 4。

在第四个测试用例中,你可以选择 k=3k = 3,并将数组划分为子段 (1,2),(3,5),(6,6)(1, 2), (3, 5), (6, 6)。

该划分的得分为 gcd⁡(1+2,1+1+1,3)=3\gcd(1 + 2, 1 + 1 + 1, 3) = 3。

输入解题思路,AI测评打分。不知道怎么写?

首页