【HDU - 6237】A Simple Stone Game(贪心,思维,素因子分解,数学)

题干:

After he has learned how to play Nim game, Bob begins to try another stone game which seems much easier.

The game goes like this: one player starts the game with NN piles of stones. There is aiai stones on the iith pile. On one turn, the player can move exactly one stone from one pile to another pile. After one turn, if there exits a number x(x>1)x(x>1) such that for each pile bibi is the multiple of xx where bibi is the number of stone of the this pile now), the game will stop. Now you need to help Bob to calculate the minimum turns he need to stop this boring game. You can regard that 00 is the multiple of any positive number.

Input

The first line is the number of test cases. For each test case, the first line contains one positive number N(1≤N≤100000)N(1≤N≤100000), indicating the number of piles of stones.

The second line contains NN positive number, the iith number ai(1≤ai≤100000)ai(1≤ai≤100000) indicating the number of stones of the iith pile.


The sum of NN of all test cases is not exceed 5∗1055∗105.

Output

For each test case, output a integer donating the answer as described above. If there exist a satisfied number xx initially, you just need to output 00. It's guaranteed that there exists at least one solution.

Sample Input

2
5
1 2 3 4 5
2
5 7

Sample Output

2
1

题目大意:

玩一个游戏,有n堆石头,每堆石头有a[i]个,每次你能将一堆石头的一个放到其他堆,当存在一个x使得所有的a[i]%x==0,游戏结束,输出游戏进行的最小次数。(n,a[i]<=1e5)

解题报告:

首先这个x可以进行缩减,如果x的倍数是可以的,那x一定是可以的,也就是说我们只需要考虑x是质数就可以了。

那么1e5个数,每个数的质因子还是特别的多的,其实我们也不需要考虑这么多数字,想想他既然有解,说明他x一定可以整数所有数,所以我们不需要分别看这n个数,只需要看看,这n个数的和就可以了。不难发现这个和<=1e10,所以我们可以先求和,然后对和进行质因子分解(不超过10个),然后枚举这些质因子。

对于接下来的处理,首先对枚举的这个数x取模一下,因为剩下的那些数都是没啥用的,然后可以贪心构造,每次都让小的摞到高的上,这样一定可以保证移动次数最少,其实不难发现不可能存在某一堆中移动>x个石子到另一堆上,因为这样相当于没有省略,所以其中x那一部分可以不用挪动,又因为保证有解,所以一定是可以移动过去的。另一种方法是因为给定了总和,给定了x,我们就知道最终要凑出几个堆来,假设需要凑出k堆,所以我们对%x后的值排个序,可以贪心选取前k大的元素,看还需要凑多少出来即可。(代码实现没有讨论的这么麻烦,但是感觉这题还是需要一个严谨的思维过程的wjhnb)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1e5 + 5;
bool is[MAX];
ll a[MAX],b[MAX];
int cnt;
ll prime[MAX],ans;
void init() {memset(is,1,sizeof is);is[1]=is[0]=0;for(int i = 2; i<MAX; i++) {if(is[i] == 0) continue;prime[++cnt] = i;for(int j = i+i; j<MAX; j+=i)  is[j]=0;}
}
vector<ll> vv;
void fj(ll x) {for(int i = 1; i<=cnt; i++) {if(x % prime[i] == 0) {vv.pb(prime[i]);while(x%prime[i] == 0) x /= prime[i];}}if(x>1) vv.pb(x);
}
int main()
{init();int T,n;cin>>T;while(T--) {scanf("%d",&n);ll sum = 0;vv.clear();ans=1e18;for(int i = 1; i<=n; i++) scanf("%lld",a+i),sum += a[i];fj(sum);for(auto x : vv) {ll tmp = 0,tt=0;for(int i = 1; i<=n; i++) {b[i] = a[i]%x;tt += b[i];}sort(b+1,b+n+1);ll zu = tt/x;for(int i = n; i>=n-zu+1; i--) tmp += x - (b[i]);ans = min(ans,tmp); }printf("%lld\n",ans);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439756.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

换种方法学操作系统,轻松入门Linux内核

计算机已成为现代人日常工作、学习和生活中必不可少的工具。操作系统是计算机之魂&#xff0c;作为用户使用计算机的接口&#xff0c;它负责调度执行各个用户程序&#xff0c;使计算机完成特定的任务&#xff1b;作为计算机硬件资源的管理者&#xff0c;它负责协调计算机中各类…

Apollo进阶课程㊶丨Apollo实战——本机演示实战

原文链接&#xff1a;进阶课程㊶丨Apollo实战——本机演示实战 Apollo是一个开放的、完整的、安全的平台&#xff0c;将帮助汽车行业及自动驾驶领域的合作伙伴结合车辆和硬件系统&#xff0c;快速搭建一套属于自己的自动驾驶系统。 上周阿波君为大家详细介绍了「进阶课程㊵丨A…

【HDU - 5965】扫雷(dp)

题干&#xff1a; 扫雷游戏是晨晨和小璐特别喜欢的智力游戏&#xff0c;她俩最近沉迷其中无法自拔。 该游戏的界面是一个矩阵&#xff0c;矩阵中有些格子中有一个地雷&#xff0c;其余格子中没有地雷。 游戏中&#xff0c;格子可能处于己知和未知的状态。如果一个己知的格子中…

java常见异常类图(分类了Error/RuntimeExecption、check Exception)

Error&#xff1a;表示由JVM所侦测到的无法预期的错误&#xff0c;由于这是属于JVM层次的严重错误&#xff0c;导致JVM无法继续执行&#xff0c;因此&#xff0c;这是不可捕捉到的&#xff0c;无法采取任何恢复的操作&#xff0c;顶多只能显示错误信息。Exception&#xff1a;表…

Apollo进阶课程㊷丨Apollo实战——车辆与循迹驾驶能力实战

原文链接&#xff1a;进阶课程㊷丨Apollo实战——车辆与循迹驾驶能力实战 循迹自动驾驶是指让车辆按照录制好的轨迹线进行自动驾驶&#xff0c;其涉及到自动驾驶中最基本的底盘线控能力、定位能力、控制能力&#xff0c;是自动驾驶系统的一个最小子集。 上周阿波君为大家详细介…

【HDU - 5961】传递(图,思维,暴力,枚举点)

题干&#xff1a; 我们称一个有向图G是传递的&#xff0c;当且仅当对任意三个不同的顶点a,,若G中有 一条边从a到b且有一条边从b到c ,则G中同样有一条边从a到c。 我们称图G是一个竞赛图&#xff0c;当且仅当它是一个有向图且它的基图是完全图。换句 话说&#xff0c;将完全图每…

Java--对象内存布局

在HotSpot虚拟机中&#xff0c;对象在内存中的存储布局可以分为3块区域&#xff1a;对象头部、实例数据、对齐填充。 一、对象头部Header的布局 Mark WordClass 指针在32位系统下&#xff0c;上面两部分各占4B; 在64位系统中&#xff0c;Mark Work占4B&#xff0c;class指针在…

Apollo进阶课程㊸丨Apollo实战——障碍物感知和路径规划能力实战

原文链接;进阶课程㊸丨Apollo实战——障碍物感知和路径规划能力实战 环境感知在自动驾驶汽车应用中占据了核心地位。一辆车要实现自动驾驶&#xff0c;障碍物感知是最基础也是最核心的功能。 上周阿波君为大家详细介绍了「进阶课程㊷丨Apollo实战——车辆与循迹驾驶能力实战」…

3.1)深度学习笔记:机器学习策略(1)

目录 1&#xff09;Why ML Strategy 2&#xff09;Orthogonalization 3&#xff09;Single number evaluation metric 4&#xff09;Satisficing and optimizing metrics 5&#xff09;训练/开发/测试集划分&#xff08;Train/dev/test distributions&#xff09; 6&…

【HDU - 5968】异或密码(思维,STLmap)

题干&#xff1a; 晨晨在纸上写了一个长度为N的非负整数序列{aiai}。对于这个序列的一个连续子序列{al,al1,…&#xff0c;aral,al1,…&#xff0c;ar}晨晨可以求出其中所有数异或的结果 alxoral1xor...xoraralxoral1xor...xorar其 中xor表示位异或运算&#xff0c;对应C、C、…

接口和抽象类是否继承了Object

我们先看一下Java的帮助文档对于Object的描述&#xff1a; Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. Object 类是类层次结构的根类。每个类都使用 …

3.2)深度学习笔记:机器学习策略(2)

目录 1&#xff09;Carrying out error analysis 2&#xff09;Cleaning up Incorrectly labeled data 3&#xff09;Build your first system quickly then iterate 4&#xff09;Training and testing on different distributios 5&#xff09;Bias and Variance with m…

【CodeForces - 674B 】Bear and Two Paths(贪心,思维,水题)

题干&#xff1a; 第一行给出N和M代表有N个难度1~N的题目&#xff0c;M代表有M个约束。接下来M行&#xff0c;每行两个数代表这一个约束的两个题目。 代表难度的数字越大&#xff0c;题目越难。现在要求你将N个题目分成不重不漏的两组(div1和div2)&#xff0c;要求1每组不能为…

4.1)深度卷积网络:卷积神经网络基础

目录 1&#xff09;Computer vision 2&#xff09;Edge detection example&#xff08;理解&#xff09; 3&#xff09;More edge detection 4&#xff09;Padding&#xff08;重点&#xff09; 5&#xff09;Strided convolutions&#xff08;重点&#xff09; 6&#x…

【2019icpc南京站网络赛 - H】Holy Grail(最短路,spfa判负环)

题干&#xff1a; As the current heir of a wizarding family with a long history,unfortunately, you find yourself forced to participate in the cruel Holy Grail War which has a reincarnation of sixty years.However,fortunately,you summoned a Caster Servant wi…

4.2)深度卷积网络:实例研究

目录 1&#xff09;Why look at case studies? 2&#xff09;Classic networks&#xff08;理解&#xff09; 3&#xff09;ResNets&#xff08;理解&#xff09; 4&#xff09;Why ResNets work?&#xff08;经典&#xff09; 5&#xff09;Networks in Networks and 1…

10种常见的软件架构模式

有没有想过要设计多大的企业规模系统&#xff1f;在主要的软件开发开始之前&#xff0c;我们必须选择一个合适的体系结构&#xff0c;它将为我们提供所需的功能和质量属性。因此&#xff0c;在将它们应用到我们的设计之前&#xff0c;我们应该了解不同的体系结构。 什么是架构模…

4.3)深度卷积网络:目标检测

目录 1&#xff09;Object localization&#xff08;重点&#xff09; 2&#xff09;Landmark detection 3&#xff09;Object detection 4&#xff09;Convolutional implementation of sliding windows 5&#xff09;Bounding box prediction&#xff08;重点&#xff0…

【牛客 - 82B】区间的连续段(贪心,建图,倍增)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/82/B 来源&#xff1a;牛客网 给你一个长为n的序列a和一个常数k 有m次询问&#xff0c;每次查询一个区间[l,r]内所有数最少分成多少个连续段&#xff0c;使得每段的和都 < k 如果这一次查询无解…

苹果手机PD快充电压电流全程详解

iphone PD充电策略&#xff0c;一共分为5个阶段。 第①阶段&#xff1a;iphone X电量为0%处于关机状态&#xff0c;这时插入PD充电器iphone会检测是否支持apple 5V2.4A协议&#xff0c;是的话会以5V2.4A进行充电。苹果原厂29w / 61w / 87w PD充电器都是自带 apple 5V2.4A 协议…