HDU-6237:A Simple Stone Game(数论)

来源:互联网 发布:尼尔森数据分析报告 编辑:程序博客网 时间:2024/05/18 01:19

A Simple Stone Game

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


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

The second line contains N positive number, the ith number ai(1ai100000) indicating the number of stones of the ith 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
251 2 3 4 525 7
 

Sample Output
21

思路:可以考虑枚举石头数量总和的质因子x,更新答案。对于一个因子x,分别求出a[i]%x的值,那么最少移动次数就相当于把Σ(a[i]%x)剩下的石头移动到Σ(a[i]%x)/x个石堆中的次数,那么当然是选前(a[i]%x)/x大的石堆,然后把其它剩下的石头移到这个前(a[i]%x)/x大的石堆中,即ans=Σ(a[i]%x)-a[i]%x中前k大的a[i]%x。

#include<bits/stdc++.h>using namespace std;const int MAX=1e5+10;long long a[MAX];int all=0,n;int pr[MAX+100];        //素数表bool isp[MAX+10];void init(){    all = 0;    memset(isp,0,sizeof isp);    isp[1]=1;    for(int i=2;i<=MAX;i++)    {        if(!isp[i])pr[all++] = i;        for(int j=0;j<all;j++)        {            long long t = 1LL*pr[j]*i ;            if(t<=MAX)            {                isp[t] = true;                if(i%pr[j]==0)break;            }            else break;        }    }    return ;}struct cmp{    bool operator() (const int a,const int b) const{return a<b;}};long long slove(int prim){    long long sum=0;    priority_queue<int,vector<int>,cmp>p;    for(int i=1;i<=n;i++)    {        sum+=a[i]%prim;        if(a[i]%prim)p.push(a[i]%prim);    }    int t=sum/prim;    while(t--)    {        sum-=p.top();        p.pop();    }    return sum;}int main(){    init();    int T;cin>>T;    while(T--)    {        scanf("%d",&n);        long long sum=0;        for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum+=a[i];        sort(a+1,a+n+1);        long long ans=sum-a[n];        for(int i=0;i<all;i++)        {            if(sum%pr[i]==0)ans=min(ans,slove(pr[i]));        }        printf("%lld\n",ans);    }    return 0;}




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 江湖风云录把王老爷子杀了怎么办 练扫踢胫骨旁边的肌肉受伤了怎么办 四个月宝宝没抱住摔了头部怎么办 老公老是跟年轻的小姑娘聊天怎么办 老婆出轨老公想离婚又舍不得怎么办 孕妇打完无痛分娩针就想睡觉怎么办 熟食店开空调菜品吹的很干怎么办 不锈钢锅在液化气烧了发黄怎么办 在小镇门面卤菜店不好卖怎么办? 被辣椒辣到嘴唇了该怎么办 沁园净水机不制水指示灯不亮怎么办 太辣了辣得胃疼怎么办 出现连接问题或mmi码无效怎么办 存折丢了怎么办卡号也不记得了 车内皮子被烂苹果腐蚀有印怎么办 锅被腐蚀后变黑色应该怎么办 后厨炉灶里的炉芯进水了怎么办 小儿九个月老是流黄鼻子该怎么办 肉炖的老了不烂怎么办 吃了凉东西现在一直打嗝应该怎么办 喝了很多水还是觉得口渴怎么办 刚买的猪肝没洗直接炒了怎么办 四个多月的宝宝吃了脏东西怎么办 狗吃了脏东西拉稀呕吐怎么办 五个月宝宝怕吃药导致奶不喝怎么办 蒸锅锅盖吸住了怎么办锅比锅盖要大 豇豆没熟孕妇吃了中毒怎么办 孩子积食拉不出粑粑憋的直哭怎么办 2岁宝宝总是半夜拉粑粑怎么办 金毛拉很臭的稀粑粑怎么办 点餐系统登录后没有菜单怎么办? 环亚在线微交易亏了钱怎么办 钢管舞报了教练班觉得学不会怎么办 微信上聊天被外国人给骗了该怎么办 微信冒充朋友骗走我钱怎么办 凉皮调料水鸡精味精放多了怎么办 吃了地屈孕酮后月经不干不净怎么办 藕片用热水炒后变色了怎么办? 外汇延期收款忘了报告了怎么办 怀孕不小心吃了马生菜怎么办 高压锅的皮圈很容易坏是怎么办