hdu6237(思维能力)

18 篇文章 0 订阅
 

A Simple Stone Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 419    Accepted Submission(s): 87


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
题意:玩一个游戏,有n堆石头,每堆石头有a[i]个,每次你能将一堆石头的一个放到其他堆,当存在一个x使得所有的a[i]%x==0,游戏结束,输出游戏进行的最小次数
数。
思路:首先考虑用欧拉函数分解质因数,然后枚举这些因子得到可能的x值,然后用a[i]%x得到的不为0的数字记录下来,最后就是在这里移动了,显然应该从大到小进行。统计答案时候就用当前的质因子减去模x后剩余的,最后取小就可以了。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
set<LL>s;
vector<LL>v;
LL a[maxn];
void init(LL n)
{
    for(LL i=2;i*i<=n;i++)
    {
        if(n&&n%i==0)
        {
            while(n%i==0)
            {
                n/=i;
            }
            s.insert(i);
        }
    }
    if(n>1) s.insert(n);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        s.clear();
        LL sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
        }
        init(sum);
        LL ans=1e10+10,sum1=0;
        for(autoit=s.begin();it!=s.end();it++)//枚举质因子
        {
            LL num1=*it;
            sum1=0;
            LL cnt=0;
            v.clear();
            for(int i=1;i<=n;i++)
            {
                LL num2=a[i]%num1;//%当前质因子
                if(num2==0) continue;
                v.push_back(num2);
                sum1+=num2;
            }
            sort(v.begin(),v.end());
            for(int i=(int)v.size()-1;i>=0;i--)
            {
               cnt+=num1-v[i];
               sum1-=num1;
               if(sum1<=0) break;
            }
            ans=min(ans,cnt);
        }
        printf("%lld\n",ans);
    }
    return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

童话ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值