CCPC 2017 哈尔滨 H题

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld
题目描述 
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.
输入描述:
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 a_i (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.
输出描述:
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.
示例1
输入

2
5
1 2 3 4 5
2
5 7
输出

2
1

题意:就是给出一数列,每次可以随便从i个数上减1在k上加1,要使得他们公约数不为1最少移多少次。
也就是让他们变成他们和sum的质因数的倍数。那为什么是sum的质因数呢?sum的质因数就是这个数列可能的质因数(因为可以随便移,极端就是移到同一堆上)。质因数是为什么。假设是4的倍数我们其实可以看做是2的倍数。按照这个思想其实都可以转换为质数。也就是质因数了。我们求出质因数后,用每一个质因数去对每个数求余。并且记求余的和为sum_y,如果sum_y模该质数不等于0那么就无法移动成题意。当等于0时,我们把求余后的数列从大到小排列。然后从sum_y/(该素数)+1开始累加直到出现0退出。所加得数就是移动成该质数倍数的所需次数。

#include<bits/stdc++.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;

const int MAX =100000+50;

ll su[MAX],a[MAX],b[MAX];
int prime_factor(ll sum){
    int index = 0;
    for(int i = 2 ; i*i <= sum ; i++){
        if(sum%i==0){
            su[index++] = i;
            while(sum%i==0) sum /= i;
        }   
    }
    if (sum!=0) su[index++] = sum;
    return index;
}
bool cmp(int a,int b){
    return a>b;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin >> n;
        ll sum = 0;
        for(int i = 1 ; i <= n ; i++){
            cin >> a[i];
            sum += a[i];
        }
        int s_num = prime_factor(sum);
        ll ans = 9999999999999;
        for(int i = 0 ; i < s_num ; i++){
            sum = 0 ;
            ll cnt = 0;
            for(int k = 1 ; k <= n ; k++){
                b[k] = a[k] % su[i];
                sum += b[k];
            }
            if(sum%su[i]!=0) continue;
            sort(b+1,b+n+1,cmp);
            for(int k = sum/su[i]+1 ; k <= n ; k++){
                cnt += b[k];
                if( b[k] == 0 ) break;
            }
            ans = min(ans,cnt);
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值