codeforces #864 div2 B
GCD Partition
这道题首先要解决一个问题,要把区间分成几块,可以证明分成两块是更优
首先我们假设把区间分成了m(>= 2)块 b1, b2, b3, ...,bm,则答案是gcd(b1, b2, b3,..., bm),则b1,b2是 gcd(b1, b2, b3,..., bm)的倍数,那么b1 + b2也是gcd(b1, b2, b3,..., bm)的倍数,所以gcd(b1, b2, b3,..., bm)<=gcd(b1 + b2, b3,..., bm),所以依此类推,区间数越少,越优,所以k = 2是最优的
然后我们可以处理一个前缀和,枚举一下分段点,求一下gcd,找出最大值即可
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int a[N];
LL s[N];
LL gcd(LL a, LL b)
{
if(b == 0) return a;
return gcd(b, a % b);
}
void solve()
{
int n;
cin >> n;
for(int i = 1; i <= n; ++ i)
{
scanf("%d", &a[i]);
s[i] = s[i - 1] + a[i];
}
LL ans = 0;
for(int i = 1; i <= n - 1; ++ i)
ans = max(ans, gcd(s[i], s[n] - s[i]));
cout << ans << endl;
}
int main()
{
int t;
cin >> t;
while(t --) solve();
}
codeforces #864 div2 B的更多相关文章
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces 864 E. Fire(背包+思维)
题目链接:http://codeforces.com/contest/864/problem/E 题解:这题一看就很像背包但是这有3维限制也就是说背包取得先后也会对结果有影响.所以可以考虑sort来降 ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
- codeforces round367 div2.C (DP)
题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...
随机推荐
- requests高级用法、代理池搭建
requests高级用法 1.自动携带cookie的session对象 # session对象---->已经模拟登录上了一些网站--->单独把cookie 取出来 import reque ...
- ZIM|一站式接入,打通 RTC 和 IM 组合拳
从用户信息.用户心跳到用户间私人与聊天室通信,IM 一直是互联网世界中不可或缺的基础建设之一.早在连麦和直播诞生之前,IM 就已是在通讯领域内服役多年的老兵,而随着线上音视频的兴起,IM 不仅没有没落 ...
- [Spring+SpringMVC+Mybatis]框架学习笔记(四):Spring实现AOP
上一章:[Spring+SpringMVC+Mybatis]框架学习笔记(三):Spring实现JDBC 下一章:[Spring+SpringMVC+Mybatis]框架学习笔记(五):SpringA ...
- Centos7中Jar快速启动脚本
Centos7中Jar快速启动脚本 创建一个文本,将以下脚本内容复制到文本当中,重命名文本后缀为.sh 注意:根据自己的项目进行更改相关内容,对应注释已说明 #!/bin/sh APP_NAME=ma ...
- 简单运维oceanbase
简单运维oceanbase 数据库集群参数修改 直连 proxy 连接 手动修改参数 show parameters like '%xx%' ; alter system set xxx='xx'; ...
- SpringBoot3集成Kafka
目录 一.简介 二.环境搭建 1.Kafka部署 2.Kafka测试 3.可视化工具 三.工程搭建 1.工程结构 2.依赖管理 3.配置文件 四.基础用法 1.消息生产 2.消息消费 五.参考源码 标 ...
- 老问题了:idea中使用maven archetype新建项目时卡住
背景 作为一个后端Java打工人,idea就是最重要的打饭工具.创建项目,熟悉吧,但是,这么多年下来,因为idea换了版本,电脑换了等等,我还是时不时遇到根据maven archetype新建mave ...
- pycharm+anaconda的关联
Pycharm+anaconda的关联 关联好处:Pycharm和anaconda关联后,pycharm可以直接调用anaconda中已安装好的模块,而anaconda里安装和卸载模块都比较方便. 关 ...
- 《Linux基础》08. 日志管理 · 备份与恢复
@ 目录 1:日志管理 1.1:系统常用日志 1.2:日志管理 1.2.1:日志服务 1.2.2:配置文件 1.2.3:自定义日志管理 1.3:日志轮替 1.3.1:轮替服务 1.3.2:配置文件 1 ...
- 谁家面试往死里问 Swagger 啊?
大家好,我是小富- 前言 说个挺奇葩的事,有个老铁给我发私信吐槽了一下它的面试经历,他去了个国企单位面试,然后面试官跟他就Swagger的问题聊了半个多小时.额- 面试嘛这些都不稀奇,总能遇到是千奇百 ...