hdu 6237 A Simple Stone Game

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 ai stones on the i th 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 bi is the multiple of x where bi 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(1N100000) , indicating the number of piles of stones.

The second line contains N positive number, the i th number ai(1ai100000) indicating the number of stones of the i th pile.


The sum of N of all test cases is not exceed 5105 .
 

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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int a[maxn];
ll sum,ans,tp1,tp2,tp3;
queue<ll> que;
vector<ll> nm;
void del(ll n)
{
    for(ll i=2;i*i<=n;i++)
        if((!(n%i))&&(n>=i))
        {
            while(!(n%i))
                n/=i;
            que.push(i);
        }
    if(n!=1)
        que.push(n);
}
int main()
{
    int t,n,i;
    scanf("%d",&t);
    while(t--&&scanf("%d",&n)!=EOF)
    {
        for(sum=0,i=0;i<n;i++)
        {
            scanf("%d",a+i);
            sum+=a[i];
        }
        del(sum);
        ans=1e10+5;
        while(que.size())
        {
            tp1=que.front();
            que.pop();
            nm.clear();
            tp2=tp3=0;
            for(i=0;i<n;i++)
                if(a[i]%tp1)
                {
                    nm.push_back(a[i]%tp1);
                    tp2+=a[i]%tp1;
                }
            sort(nm.begin(),nm.end());
            for(vector<ll>::iterator it=(nm.end()-1);it>=nm.begin();it--)
            {
                tp3+=tp1-*it;
                tp2-=tp1;
                if(tp2<=0)
                    break;
            }
            ans=min(ans,tp3);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值