首页 > 其他分享 >B. GCD Partition

B. GCD Partition

时间:2023-01-27 17:34:07浏览次数:43  
标签:10 le GCD Partition score array ldots gcd

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)$的形式。因此可以枚举两段的分界点来求最大值。

  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

标签:10,le,GCD,Partition,score,array,ldots,gcd
From: https://www.cnblogs.com/onlyblues/p/17069077.html

相关文章

  • gcd(a, b) = gcd(b, a % b)的证明
    \[\hugegcd(a,b)=gcd(b,a\modb)的证明\]首先假设存在\(a,b,c\)这三个数,若其满足\(a=q*b+c\),证明:\((a,b)=(b,a\modb)\)证明:\(\because\)首先可以由\(......
  • CF1780B GCD Partition
    从\(k\)的角度出发,可以发现一定存在某种\(k=2\)的划分方式,使得最终获得的分数最大。为什么呢?假定划分为\(k=3\)时取得了最大分数,即\(\gcd(b_1,b_2,b_3)=g......
  • 扩展欧几里得算法 exgcd
    凌:前言\(\tt\#include~"\)\(\tt{\small\texttt{扩展欧几里得算法-}}OI~Wiki\)\(\tt"\)\(\tt\#include~"\)\(\tt{\small\texttt{关于}}~exgcd~{\small\texttt{求得......
  • [ARC144E]GCD of Path Weights
    ProblemStatementYouaregivenadirectedgraph$G$with$N$verticesand$M$edges.Theverticesarenumbered$1,2,\ldots,N$.The$i$-thedgeisdirected......
  • GCD辗转相除的经典套路
      2543.判断一个点是否可以到达-力扣(Leetcode)前两个移动很像辗转相除法(这个套路在Codeforces上已经出烂了)<br>后两个移动可以让 g 乘上$2^k$classS......
  • Flink消费Kafka:Timeout of 60000ms expired before the position for partition tv_lo
    Timeoutof60000msexpiredbeforethepositionforpartitiontv_log-1couldbedetermined大概意思:消费kafka,在某个分区连接超时超时了60000ms这个时候要检查:C:\Win......
  • exgcd & 线性同余方程
    前置芝士裴蜀定理exgcdexgcd即扩展欧几里得定理,常用来求解\(ax+by=gcd(a,b)\)的可行解问题推导过程:考虑我们有:​ \(ax+by=gcd(a,b)\)——裴蜀定理​ \(a_......
  • leetcode 每日一题 gcd+枚举
    1819.序列中不同最大公约数的数目给你一个由正整数组成的数组nums。数字序列的最大公约数定义为序列中所有整数的共有约数中的最大整数。  例如,序列[4,6,16]......
  • Number of Great Partitions
    NumberofGreatPartitionsYouaregivenanarray nums consistingofpositive integersandaninteger k .Partitionthearrayintotwoorderedgroups suc......
  • abc254 F - Rectangle GCD
    题意:给定等长的正整数组\(a[],b[]\),它们确定了一个矩阵\(A_{i,j}=a_i+b_j\)。\(q\)次询问,回答矩阵中一个矩形区域内所有数的\(\gcd\)\(n,q\le2e5\)思路:差分,绝对......