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;
}

 

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/7944420.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值