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 of length . On this array, do the following:
- select an integer ;
- split the array into subsegments ;
- calculate the sum in each of subsegments and write these sums to another array (where the sum of the subsegment is );
- the final score of such a split will be .
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.
A division of an array into subsegments is pairs of numbers such that and for every , also and . These pairs represent the subsegments.
stands for the greatest common divisor (GCD) of the array .
Input
The first line contains a single number () — the number of test cases.
For each test case, the first line contains one integer () — the length of the array .
The second line contains integers () — the array itself.
It is guaranteed that the sum of over all test cases does not exceed .
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 and split the array into subsegments and .
Then the score of such a partition will be equal to .
In the fourth test case, you can choose and split the array into subsegments .
The split score is .
解题思路
可以证明最优解必定出现在将序列分成两段的情况中。
假如将序列分成段,那么此时的答案是,有,这是因为对于的任意一个公约数,都一定能整除,即也是的公约数。因此就有。以此类推,为了得到最大值最终一定会变成的形式。因此可以枚举两段的分界点来求最大值。
补充另外一个方法证明。设,那么。,。
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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17069077.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效