2017中国大学生程序设计竞赛-哈尔滨站 H - A Simple Stone Game

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=2;i<MAXN;i++){
        if(prime[i]==false)
            p[tol++]=i;
        for(LL j=0;j<tol&&i*p[j]<MAXN;j++){
            prime[i*p[j]]=true;
            if(i%p[j]==0)
                break;
        }
    }
}

inline void div(LL x){
    memset(fa,0,sizeof fa);
    pos=0;
    for(LL i=0;i<tol&&p[i]*p[i]<=x;i++){
        if(x%p[i]==0){
            fa[pos++]=p[i];
            while(x%p[i]==0) x/=p[i];
        }
    }
    if(x>1)    fa[pos++]=x;
}

inline void init(){
    g=0;
    sum=0;
    v.clear();
    res=INF;
}

int main(){
    pre();
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d",&n);
        for(LL i=0;i<n;i++){
            scanf("%lld",&a[i]);
            sum+=a[i];
            g=__gcd(a[i],g);
        }
        if(n==1){
            puts("0");
            continue;
        }
        div(sum);
        for(LL i=0;i<pos;i++){
            v.clear();
            mod=fa[i];
            LL cnt=0;
            for(LL i=0;i<n;i++){
                if(a[i]%mod!=0){
                    v.push_back(a[i]%mod);
                    cnt+=a[i]%mod;
                }
            }
            sort(v.begin(),v.end());
            LL tol=cnt/mod;
            LL s=0;
            for(LL i=(LL)v.size()-1;i>=0;i--){
                tol--;
                s+=(LL)(mod-v[i]);
                if(tol<=0) break;
            }
            res=min(res,s);
        }
        printf("%lld\n",res);
    }
    return 0;
}
时间: 2024-07-31 04:52:29

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

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

A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description After he has learned how to play Nim game, Bob begins to try another ston

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

Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description A permutation p1,p2,...,pn of 1,2,...,n is called a lucky permutatio

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha&#39;s staff 思维

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 题意:在笛卡尔坐标系下,画一个面积至少为  n 的简单多边形,每次只能画一条边或者一个格子的对角线,问至少要画几条. 解法:如果一个斜着的矩形长宽分别是 a,b,那么它的面积是 2ab.最优解肯定是离 sqrt(n/2)很近的位置.想想 n=5 时答案为什么是7 然后在那个小范围内枚举一下就好了.我给一张做题时画的图 #include <bits/stdc++.h> using namesp

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha&#39;s staff(几何找规律)

Problem Description "You shall not pass!"After shouted out that,the Force Staff appered in CaoHaha's hand.As we all know,the Force Staff is a staff with infinity power.If you can use it skillful,it may help you to do whatever you want.But now,hi

2017中国大学生程序设计竞赛 - 女生专场(dp)

Building Shops Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 701 Accepted Submission(s): 265 Problem Description HDU's n classrooms are on a line ,which can be considered as a number line. Eac

2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)

Happy Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1146    Accepted Submission(s): 491 Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single

2017中国大学生程序设计竞赛 - 女生专场 Deleting Edges(思维+最短路)

Deleting Edges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 567    Accepted Submission(s): 210 Problem Description Little Q is crazy about graph theory, and now he creates a game about grap

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/7398272.html 先考虑dp求01串的不同子序列的个数. dp[i][j]表示用前i个字符组成的以j为结尾的01串个数. 如果第i个字符为0,则dp[i][0] = dp[i-1][1] + dp[i-1][0] + 1,dp[i][1] = dp[i-1][1] 如果第i个字符为1,则dp[i][1

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6153 A Secret KMP,思维

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给了串s和t,要求每个t的后缀在在s中的出现次数,然后每个次数乘上对应长度求和. 解法:关键在于想到把s和t都翻转之后,把t求next,然后用t去匹配s,在匹配过程中把fail指针跳到的地方加1,但是还没完,最后需要反向遍历第二个串将大串对小串的贡献加上去就可以了. 这道题是很多现场AC的代码是有漏洞的,比如bazbaba,bazbaba这个答案是34,但是很多现场AC的代码会输出31.