HDU 6237 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): 3217    Accepted Submission(s): 712


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 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 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(1≤N≤100000), indicating the number of piles of stones.

The second line contains N positive number, the ith number ai(1≤ai≤100000) indicating the number of stones of the ith pile.


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

 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

Source

2017中国大学生程序设计竞赛-哈尔滨站-重现赛(感谢哈理工)

题意:n堆石子,已知每一堆石头的数量,每次操作可以取任意一堆的一颗石子放到另一堆中,问最少多少次操作使得每一堆的石子的数量都为某一个数x的倍数

思路:x肯定为石头总量sum的因子,我们可以枚举sum的所有质因子,对于每一个质因子x,每一堆石头的数量都对x取余,然后从大到小排序,每次都移动余数小的来填充余数大的,记录最小值

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100005;
ll a[MAXN],b[MAXN];
ll phi[MAXN];
int cnt;
bool cmp(ll a,ll b)
{
    return a > b;
}
void eular(long long n)
{
    for(ll i = 2; i * i <= n; i++) {
        if(n % i == 0)
            phi[cnt++] = i;
        while(n % i == 0)
            n /= i;
    }
    phi[cnt++] = n;
}
int main(void)
{
    int T,n,k;
    ll sum,ans,tot,temp;
    scanf("%d",&T);
    while(T--) {
        sum = 0;
        scanf("%d",&n);
        for(int i = 1; i <= n; i++) {
            scanf("%I64d",&a[i]);
            sum += a[i];
        }
        cnt = 0;
        eular(sum);
        ans = 1e10 + 10;
        for(int i = 0; i < cnt; i++) {
            tot = 0;
            for(int j = 1; j <= n; j++) {
                b[j] = a[j] % phi[i];
                tot += b[j];
            }
            temp = 0;
            k = 1;
            sort(b + 1,b + n + 1,cmp);
            while(k <= n && tot > 0) {
                temp += phi[i] - b[k];
                tot -= phi[i];
                k++;
            }
            ans = min(ans,temp);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值