HDU 6237/2017CCPC哈尔滨 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): 524    Accepted Submission(s): 107

Problem Description

After he has learned how toplay 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 ai stoneson the i thpile. On one turn, the player can move exactly one stone from one pile toanother pile. After one turn, if there exits a number x(x>1) such that for each pile bi isthe multiple of x where bi is the number of stone of thethis pile now), the game will stop. Now you need to help Bob to calculate theminimum turns he need to stop this boring game. You can regard that 0 is the multiple of anypositive number.

 

 

Input

The first line is the numberof test cases. For each test case, the first line contains one positive number N(1≤N≤100000), indicating the number ofpiles of stones.

The second line contains
N positive number, the i th number ai(1≤ai≤100000) indicating the number ofstones of the i th pile.


The sum of
N ofall test cases is not exceed 5∗105.

 

 

Output

For each test case, output ainteger donating the answer as described above. If there exist a satisfiednumber 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


【题意】


现在有n堆石子,且已知每堆石子的数量,每次操作你可以从任一堆中取一颗石子放到另一堆中,问最少操作几次使得每一堆石子的数量均是某一个数的倍数。


【思路】


显然,最后对于任意的a[i],满足a[i]%x==0,易得x是每一堆石子数的因子,那么x也是所有石子数量总和的因子。


那么我们只要分解总和的质因子,然后暴力枚举质因子求出最小的操作数即可。


具体细节见代码。


【PS】 注意用long long型


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 100005;
const ll mod = 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

int n;
ll a[maxn];
vector<ll>vec;
vector<ll>mp;

int main()
{
    rush()
    {
        scanf("%d",&n);
        vec.clear();
        ll sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%I64d",&a[i]);
            sum+=a[i];
        }
        for(ll i=2;i*i<=sum;i++)           //求出总和的质因子
        {
            if(sum%i==0)
            {
                vec.push_back(i);
                while(sum%i==0)
                {
                    sum/=i;
                }
            }
        }
        if(sum>1) vec.push_back(sum);
        sort(vec.begin(),vec.end());
        ll ans=1e18;
        for(int i=0;i<vec.size();i++)           //枚举质因子
        {
            sum=0;
            mp.clear();
            for(int j=0;j<n;j++)
            {
                int tmp=a[j]%vec[i];
                if(tmp)
                {
                    mp.push_back(tmp);
                    sum+=tmp;                 //距离倍数的总差距
                }
            }
            sort(mp.begin(),mp.end());
            ll cnt=0;
            //从接近倍数的石子堆开始考虑,不从小的开始考虑的原因是不确定这堆是否被拿去石子
            for(int j=mp.size()-1;j>=0;j--)   
            {
                cnt+=vec[i]-mp[j];            //变成倍数需要几颗石子,即操作次数
                sum-=vec[i];                  //每次能使总差距减少vec[i]
                if(sum<=0) break;
            }
            ans=min(ans,cnt);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值