zoukankan      html  css  js  c++  java
  • 2017中国大学生程序设计竞赛-哈尔滨站 H

    A Simple Stone Game

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


    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 a_i 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 b_i is the multiple of x where b_i 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 leq N leq 100000), indicating the number of piles of stones.

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


    The sum of N of all test cases is not exceed 5 * 10^5.
     
    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
    /*************************************************************************
        > File Name: H.cpp
        > Author: LyuCheng
        > Created Time: 2017-12-01 20:41
        > Description: 
            题意:有n堆石子,每次你可以选择一堆石子拿一个放到另一堆石子里,如果
                所有的所有的石子数不互质,那么游戏结束
            思路:问题的实质就是找到一个x是的b[1]%x+b[2]%x+...b[n]%x=0;也就是
                sum%x=0,那么分解sum的质因子,然后枚举判断
     ************************************************************************/
    
    #include <bits/stdc++.h>
    
    #define MAXN 123456
    #define LL long long
    #define INF 50000000005 
    
    using namespace std;
    
    bool prime[MAXN];
    LL p[MAXN];
    LL tol;
    int t;
    int n;
    LL a[MAXN];
    LL g;
    LL pos;
    LL fa[MAXN];
    LL sum;
    LL mod;
    LL res;
    vector<LL>v;
    
    inline void pre(){
        for(LL i=2;i<MAXN;i++){
            if(prime[i]==false)
                p[tol++]=i;
            for(LL j=0;j<tol&&i*p[j]<MAXN;j++){
                prime[i*p[j]]=true;
                if(i%p[j]==0)
                    break;
            }
        }
    }
    
    inline void div(LL x){
        memset(fa,0,sizeof fa);
        pos=0;
        for(LL i=0;i<tol&&p[i]*p[i]<=x;i++){
            if(x%p[i]==0){
                fa[pos++]=p[i];
                while(x%p[i]==0) x/=p[i];
            }
        }
        if(x>1)    fa[pos++]=x;
    }
    
    inline void init(){
        g=0;
        sum=0;
        v.clear();
        res=INF;
    }
    
    int main(){
        pre();
        scanf("%d",&t);
        while(t--){
            init();
            scanf("%d",&n);
            for(LL i=0;i<n;i++){
                scanf("%lld",&a[i]);
                sum+=a[i];
                g=__gcd(a[i],g);
            }
            if(n==1){
                puts("0");
                continue;
            }
            div(sum);    
            for(LL i=0;i<pos;i++){
                v.clear();
                mod=fa[i];            
                LL cnt=0;
                for(LL i=0;i<n;i++){
                    if(a[i]%mod!=0){
                        v.push_back(a[i]%mod);
                        cnt+=a[i]%mod;
                    }
                }
                sort(v.begin(),v.end());
                LL tol=cnt/mod;
                LL s=0;
                for(LL i=(LL)v.size()-1;i>=0;i--){
                    tol--;
                    s+=(LL)(mod-v[i]);
                    if(tol<=0) break;
                }
                res=min(res,s);    
            }
            printf("%lld
    ",res);
        }
        return 0;
    }
  • 相关阅读:
    vue简单分屏(1,4,9,16),全屏,还需要调整
    vue 中的Vuex实践
    常用工具类
    java Log日志规范
    spring中对象的注入方式
    不恰当的update语句使用主键和索引导致mysql死锁
    JPA规范及其它持久层框架
    webservice基本功能介绍
    Activiti工作流(二)之常用操作
    Activiti工作流(一)之基本操作介绍
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/7944420.html
Copyright © 2011-2022 走看看