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. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  2. OpenCV 3.2 Tracking 物体跟踪

    跟踪就是在连续视频帧中定位物体,通常的跟踪算法包括以下几类: 1. Dense Optical Flow 稠密光流 2. Sparse Optical Flow 稀疏光流 最典型的如KLT算法(Kan ...

  3. php webservice 可能存在的坑

    场景: 今天在生产机器上调用webservice失败 报 ...failed to load external entity... wget一下地址发现报500错误  把wsdl去掉再wget 发现就 ...

  4. InnoDB锁冲突案例演示

      Preface       As we know,InnoDB is index organized table.InnoDB engine supports row-level lock bas ...

  5. OSG-简单模型控制

    本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...

  6. Linux命令应用大词典-第19章 文件系统管理

    19.1 mkfs:创建Linux文件系统 19.2 mke2fs:创建ext2.3.4文件系统 19.3 mkfs.ext4:创建ext4文件系统 19.4 mkfs.ext3:创建ext3文件系统 ...

  7. DataSet转化为DataTable

    . DataTable dt = ds.Tables[]; . DataTable dt = dao.FillTables("GetOptions_DKI_City_HCPName" ...

  8. 深入理解Java 8 Lambda(类库篇——Streams API,Collectors和并行)

    转载:http://zh.lucida.me/blog/java-8-lambdas-inside-out-library-features/ 关于 深入理解 Java 8 Lambda(语言篇——l ...

  9. [leetcode-693-Binary Number with Alternating Bits]

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

  10. 业务迁移---web

    #本文是做记录使用,不做为任何参考文档# 迁移代码 将源代码scp至新的server上 搭建服务 yum安装nginx服务 yum install nginx #yum安装 service nginx ...