HDU 6237 A Simple Stone Game(欧拉函数+贪心)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GYH0730/article/details/82946460

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

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/82946460
今日推荐