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. 在hive中查询导入数据表时FAILED: SemanticException [Error 10096]: Dynamic partition strict mode requires at least one static partition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict

    当我们出现这种情况时 FAILED: SemanticException [Error 10096]: Dynamic partition strict mode requires at least ...

  2. redis外部访问

    1.redis的搭建这里就不做描述的了,可以参考我的另外一个博客. http://www.cnblogs.com/ll409546297/p/6993778.html 2.说明一下我们在其他服务器上面 ...

  3. springboot 常用依赖

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  4. define的误用

    #define LIGHT_SPEED 3e8 // m/sec (in a vacuum)

  5. 这样的SQL居然能执行

    select /*! distinct   cities.id from cities  join countries on cities.id = countries.id limit 10 */;

  6. Linux命令应用大词典-第8章 日期和时间

    8.1 cal:显示日历信息 8.2 date:显示和设置系统日期和时间 8.3 hwclock:查看和设置硬件时钟 8.4 clock:查看和设置硬件时钟 8.5 clockdiff:主机之间测量时 ...

  7. 第四模块:网络编程进阶&数据库开发 第2章·MySQL数据库开发

    01-MySQL开篇 02-MySQL简单介绍 03-不同平台下安装MySQL 04-Windows平台MySQL密码设置与破解 05-Linux平台MySQL密码设置与破解 06-Mac平台MySQ ...

  8. java 实现redis缓存

    由于项目加载时请求数据量过大,造成页面加载很慢.采用redis作缓存,使二次访问时页面,直接取redis缓存. 1.redis连接参数 2.连接redis,设置库 3.配置文件开启缓存 4.mappe ...

  9. 【WXS】简要介绍说明

    WXS(WeiXin Script)是小程序的一套脚本语言. WXS有二种写法: 1) 以<wxs>标签书写脚本: 语法: <wxs module="[String]&qu ...

  10. [模板]非递归线段树(zkw的变异版本)

    类似于zkw,但空间只用两倍,zkw要4倍. 链接 可以下传标记,打熟后很好码. #include <set> #include <cmath> #include <cs ...