A Simple Stone Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1526    Accepted Submission(s): 346

Problem Description
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 N piles of stones. There is a_i stones on the ith
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) such that for each pile b_i is the multiple of x where b_i
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 0 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 \leq N \leq 100000), indicating the number of piles of stones.

The second line contains N positive number, the ith number a_i (1 \leq a_i \leq 100000) indicating the number of stones of the ith pile.

The sum of N of all test cases is not exceed 5 * 10^5.

 
Output
For each test case, output a integer donating the answer as described above. If there exist a satisfied number x initially, you just need to output 0. 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
/*************************************************************************
> File Name: H.cpp
> Author: LyuCheng
> Created Time: 2017-12-01 20:41
> Description:
题意:有n堆石子,每次你可以选择一堆石子拿一个放到另一堆石子里,如果
所有的所有的石子数不互质,那么游戏结束
思路:问题的实质就是找到一个x是的b[1]%x+b[2]%x+...b[n]%x=0;也就是
sum%x=0,那么分解sum的质因子,然后枚举判断
************************************************************************/ #include <bits/stdc++.h> #define MAXN 123456
#define LL long long
#define INF 50000000005 using namespace std; bool prime[MAXN];
LL p[MAXN];
LL tol;
int t;
int n;
LL a[MAXN];
LL g;
LL pos;
LL fa[MAXN];
LL sum;
LL mod;
LL res;
vector<LL>v; inline void pre(){
for(LL i=;i<MAXN;i++){
if(prime[i]==false)
p[tol++]=i;
for(LL j=;j<tol&&i*p[j]<MAXN;j++){
prime[i*p[j]]=true;
if(i%p[j]==)
break;
}
}
} inline void div(LL x){
memset(fa,,sizeof fa);
pos=;
for(LL i=;i<tol&&p[i]*p[i]<=x;i++){
if(x%p[i]==){
fa[pos++]=p[i];
while(x%p[i]==) x/=p[i];
}
}
if(x>) fa[pos++]=x;
} inline void init(){
g=;
sum=;
v.clear();
res=INF;
} int main(){
pre();
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(LL i=;i<n;i++){
scanf("%lld",&a[i]);
sum+=a[i];
g=__gcd(a[i],g);
}
if(n==){
puts("");
continue;
}
div(sum);
for(LL i=;i<pos;i++){
v.clear();
mod=fa[i];
LL cnt=;
for(LL i=;i<n;i++){
if(a[i]%mod!=){
v.push_back(a[i]%mod);
cnt+=a[i]%mod;
}
}
sort(v.begin(),v.end());
LL tol=cnt/mod;
LL s=;
for(LL i=(LL)v.size()-;i>=;i--){
tol--;
s+=(LL)(mod-v[i]);
if(tol<=) break;
}
res=min(res,s);
}
printf("%lld\n",res);
}
return ;
}

2017中国大学生程序设计竞赛-哈尔滨站 H - A Simple Stone Game的更多相关文章

  1. 2017中国大学生程序设计竞赛-哈尔滨站 Solution

    A - Palindrome 题意:给出一个字符串,找出其中有多少个子串满足one-half-palindromic 的定义 思路:其实就是找一个i, j  使得 以i为中轴的回文串长度和以j为中轴的 ...

  2. HDU6237-A Simple Stone Game-找素因子(欧拉函数)-2017中国大学生程序设计竞赛-哈尔滨站-重现赛

    A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  3. HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)

    A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  4. HDU6235-Permutation-水题-2017中国大学生程序设计竞赛-哈尔滨站-重现赛

    Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tot ...

  5. HDU 6235.Permutation (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)

    Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tot ...

  6. 2017中国大学生程序设计竞赛-哈尔滨站 A - Palindrome

    Palindrome Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tota ...

  7. 【2017中国大学生程序设计竞赛-哈尔滨站】B - K-th Number

    原题: 题意: 给你一个长度为N的正整数组A,对于这个数组的所有子区间,若长度小于k则不管它,若长度大于等于k则取第k大放入数组B 问你B中第M大的数是谁 一眼序列分治,然而没思路 数据结构?能想到从 ...

  8. HDU 6273.Master of GCD-差分数组 (2017中国大学生程序设计竞赛-杭州站-重现赛(感谢浙江理工))

    Super-palindrome 题面地址:http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf 这道题是差分数组的题目,线 ...

  9. HDU 6154 - CaoHaha's staff | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    /* HDU 6154 - CaoHaha's staff [ 构造,贪心 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 整点图,每条线只能连每个方格的边或者对角线 问面积大于n的 ...

随机推荐

  1. nodejs 事件机制

    node 事件机制   一 三种定时器 NodeJS中有三种类型的定时器:超时时间.时间间隔.即时定时器 1.超时时间:setTimeout(callback,delayMilliSeconds,[a ...

  2. vue2组件之间双向数据绑定问题

    最近在使用element-ui的dialog组件二次封装成独立组件使用时,子组件需要将关闭dialog状态返回给父组件,简单的说就是要实现父子组件之间的数据双向绑定问题. 大致代码如下: 1,父组件 ...

  3. 「专题训练」Air Raid(HDU-1151)

    题目 在一个城市里有\(n\)个地点和\(k\)条道路,道路是无环的(也就是说一定可以二分染色--回路长度为偶数0),现在伞兵需要去n个地点视察,只能沿着路的方向走,问最少需要多少伞兵. 分析 这是什 ...

  4. mybati缓存机制之二级缓存配置

    二级缓存配置 在MyBatis的配置文件中开启二级缓存. <setting name="cacheEnabled" value="true"/> 在 ...

  5. 【SpringCloud 】第八篇: 消息总线(Spring Cloud Bus)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  6. An Adaptive Color-Based Particle Filter--粒子滤波

    粒子滤波跟踪的具体步骤如下: 1. Resampling the particles to avoid degeneracy 2. Propagate  each particles accordin ...

  7. 统计学习三:2.K近邻法代码实现(以最近邻法为例)

    通过上文可知k近邻算法的基本原理,以及算法的具体流程,kd树的生成和搜索算法原理.本文实现了kd树的生成和搜索算法,通过对算法的具体实现,我们可以对算法原理有进一步的了解.具体代码可以在我的githu ...

  8. *.hbm.xml作用是什么

    实体与表的映射关系通过XML来描述的文件.在 hibernate.cfg.xml中管理,在项目启动的时候加载到内存. hbm指的是hibernate的映射文件 映射文件也称映射文档,用于向Hibern ...

  9. 使用cout进行格式化

    以下内容摘自木缥缈的博客 使用cout进行格式化 ostream插入运算符将值转换为文本格式.在默认情况下,格式化值的方式如下. * 对于char值,如果它代表的是可打印字符,则将被作为一个字符显示在 ...

  10. lintcode-187-加油站

    187-加油站 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i],并且从第_i_个加油站前往第_i_+1个加油站需要消耗汽油cost[i]. 你有一辆油箱容量无限大的汽车,现在要从 ...