题面

给定一个长度为 $N$ 的整数序列:$A_1, A_2, \ldots, A_n$。

您可以执行以下操作 $0 \sim K$ 次:

选择两个整数 $i$ 和 $j$,满足 $i \ne j$ 并且 $1 \le i, j \le N$。令 $A_i$ 加上 $1$,令 $A_j$ 减去 $1$,可能产生负的元素。

计算在执行完操作后,整除 $A$ 中每个元素的最大可能正整数。这里正整数 $x$ 整除整数 $y$ 当且仅当存在一个整数 $z$,使得 $y=xz$。

思路

容易发现\(n\)很小,且答案\(ans\)一定为序列的权值和的因子,又发现,无论经过多少次操作后,总和均不变。考虑枚举\(ans\),记录下每一个值在当前情况下的余数,排序后用双指针找需要把所以余数都归零的操作次数,是否大于k判合法性即可

code

点击查看代码
#include <bits/stdc++.h>
using namespace std;
int read() {
int a=0,f=0;
char c=getchar();
for(; c<'0'||c>'9'; c=getchar())if(c=='-')f=1;
for(; c>='0'&&c<='9'; c=getchar())a=a*10+c-'0';
return f?-a:a;
}
const int N=5e2+10;
int n,sum=0,ans=0,k,kk;
int p[N],b[N];
int main() {
n=read(),k=read(),kk=k;
for(int i=1; i<=n; ++i) p[i]=read(),sum+=p[i];
// cout<<sum<<endl;
for(int i=1; i<=sum; ++i) {
// cout<<"sy "<<i<<" "<<sum<<" "<<sum%i<<endl;
if(sum%i!=0) continue;
int x=sum/i;
// cout<<"i: "<<i<<endl;
k=kk;
for(int j=1; j<=n; ++j) {
b[j]=p[j]%i;
}
sort(b+1,b+1+n);
bool flag=0;
int kun=0,l=1,r=n;
// cout<<"b: ";
// for(int d=1; d<=n; ++d) {
// cout<<b[d]<<" ";
// }
// cout<<endl;
while(l<=r) {
// cout<<"b: ";
// for(int d=1; d<=n; ++d) {
// cout<<b[d]<<" ";
// }
// cout<<endl;
int fl=b[l],fr=b[r],gl=fl,gr=i-fr;
// cout<<i<<" "<<l<<" "<<r<<" "<<gl<<" "<<gr<<" "<<k<<" "<<sum<<endl;
if(b[l]==0) {
l++;
continue;
}
if(fl+fr==i) {
k-=fl;
if(k<0) {
flag=1;
break;
}
l++,r--;
continue;
}
if(gl>gr) {
k-=gr;
b[l]-=gr;
r--;
if(k<0) {
flag=1;
break;
}
continue;
}
if(gl<gr) {
k-=gl;
b[r]+=gl;
l++;
if(k<0) {
flag=1;
break;
}
continue;
}
}
// cout<<"i: "<<flag<<endl;
if(!flag) {
ans=max(ans,i);
}
}
cout<<ans;
return 0;
}
/*
8 7
1 7 5 6 8 2 6 5
*/

ABC136 E - Max GCD 题解的更多相关文章

  1. HDU5726:GCD——题解

    题目:hdu的5726 (我原博客的东西,正好整理过来,属于st表裸题) (可以看出我当时有多么的菜--) 这道题写了一遍,然而蒟蒻的我的时间爆炸了-- 于是看了一下学长的代码(顺便在此处%一下学长) ...

  2. 洛谷 P2568 GCD 题解

    原题链接 庆祝一下:数论紫题达成成就! 第一道数论紫题.写个题解庆祝一下吧. 简要题意:求 \[\sum_{i=1}^n \sum_{j=1}^n [gcd(i,j)==p] \] 其中 \(p\) ...

  3. Hdoj 1003.Max Sum 题解

    Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...

  4. BZOJ2820:YY的GCD——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2820 Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...

  5. CC DGCD:Dynamic GCD——题解

    https://vjudge.net/problem/CodeChef-DGCD https://www.codechef.com/problems/DGCD 题目大意: 给一颗带点权的树,两个操作: ...

  6. CH 4302 Interval GCD 题解

    题意 给定一个长度为N的数列A,以及M条指令 (N≤5* 10^5, M<=10^5),每条指令可能是以下两种之一: "C l r d",表示把 A[l],A[l+1],-, ...

  7. gcd 题解

    gcd Little White learned the greatest common divisor, so she plan to solve a problem: given \(x, n,\ ...

  8. ABC136E Max GCD

    Thinking about different ways of thinking. --- LzyRapx 题目 思路比较容易想到. Observations: 每次操作过后和不变. 枚举和的因子 ...

  9. [洛谷2257]YY的GCD 题解

    整理题目转化为数学语言 题目要我们求: \[\sum_{i=1}^n\sum_{i=1}^m[gcd(i,j)=p]\] 其中 \[p\in\text{质数集合}\] 这样表示显然不是很好,所以我们需 ...

  10. luoguP3128 [USACO15DEC]最大流Max Flow 题解(树上差分)

    链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了 ...

随机推荐

  1. @NotNull,@NotBlank,@NotEmpty注解的区别

    开发中常看见@NotNull,@NotBlank,@NotEmpty三个注解,但却没有深入了解过,下面介绍一下他们的应用场景和区别 @NotNull:主要用在基本数据类型上(Int,Integer,D ...

  2. 禅道服务崩溃 Can't init tc log

      0.环境 禅道 版本12.4.3 数据库 10.1.22-MariaDB SQL 服务器 192.168.0.82 centos 7 step 1.问题 磁盘爆满后,禅道在启动时报数据库 Can' ...

  3. MongoDB升级

    因业务需要所以需要对mongoDB客户端jar包升级,在此记录一些过程 1.jar包替换,引入依赖 3.2.2: mongo-java-driver     |     4.6.1: mongo-dr ...

  4. javase_note

    我上班摸鱼重新学习java基础做的笔记,从面向对象开始 面向对象基础 类与对象 人类.鸟类.鱼类...所谓类,就是对一类事物的描述 对象是某一类事物实际存在的每个个体,因此也称为实例 类是抽象概念,对 ...

  5. [656] C4 Scions Of Destiny Opcodez

    [656] C4 Scions Of Destiny Client 00 SendProtocolVersion 01 MoveBackwardToLocation 02 Say 03 Request ...

  6. [Swift]使用Alamofire传递参数时报错

    p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1) } span.s1 { font-variant-ligatures: no-c ...

  7. AFNI 教程 步骤5:统计和建模

    第一部分 时间序列 用AFNI打开fMRI数据, Graph按钮可以打开信号界面,中心的信号是该像素的信号随着时间的变化图,m 可以显示更少的体素,M可以显示更多的体素.V 可以浏览整个图像,+ 可以 ...

  8. 2023-03-01 fatal: unable to access 'https://github.com/top-think/think/': OpenSSL SSL_read: Connection was reset, errno 10054

    问题描述:在thinkphp官网拉取tp5项目文件时报错: fatal: unable to access 'https://github.com/top-think/think/': OpenSSL ...

  9. 003. html篇之《表单》

    html篇之<表单> 一.结构 <form action="url" method="post" name=""> ...

  10. 解决命令行窗口执行godoc报错

    在cmd命令行窗口执行godoc报错: 'godoc' is not recognized as an internal or external command,operable program or ...