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): 797    Accepted Submission(s): 162


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 i th 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 i th number ai(1ai100000) indicating the number of stones of the i th 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
  
  
2 5 1 2 3 4 5 2 5 7
 

Sample Output
  
  
2 1
 
Source
 


题意:
有n堆石头,每一次操作只能从一堆里拿一个放到另一堆上,问你最少需要多少次操作,能使每一堆石头都能被x(x>1)整除

解析:
就是枚举所有石头和的质因子。
在讲每一个质因子当作x求操作数,在取min。
在求操作数时,用的是贪心。余数大的堆尽量不动,用余数小的堆去补余数大的堆。

之前我用的是从1-100000枚举质数看是否是sum的质因数,这样一直WA,也找不到原因,把这个改成直接求sum的质因数就A了,求大神指教


#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

const int MAXN = 100000+10;
#define INF 1446744073709551616

typedef long long int ll;

ll a[MAXN],b[MAXN];
ll visit[MAXN],prime[MAXN];
int num,n;

bool cmp(ll a,ll b)
{
    return a>b;
}

ll cal(ll mi)
{
    int i;
    ll tot=0;
    for(i=0;i<n;i++)
    {
        b[i]=a[i]%mi;
        tot+=b[i];
    }
    sort(b,b+n,cmp);
    ll ans=0;
    i=0;
    while(tot&&i<n)  
    {
        ans+=mi-b[i];   //记录要补的总次数,mi-b[i]表示该堆需要补的石子数
        tot-=mi;   //每一次找都一定是从余数最大的找,让除了这一堆以外(跟准确说是余数比他小)的其他堆给他补,每一次一定促成一堆符合条件的
        //记录当前还剩下多少没有被分配
        i++;
    }
    return ans;

}

/*void primetable()
{
    num=0;
    for(int i=2;i<MAXN;i++)
    {
        if(!visit[i])
        {
            prime[num++]=i;
            for(int j=i+i;j<MAXN;j+=i)
            {
                visit[j]=1;
            }
        }
    }
}*/

void sushu(ll sum){   //找出来素因子
    num=0;
    for(ll i=2;i<=sqrt(sum);i++){
        if(sum%i==0)
            prime[num++]=i;
        while(sum%i==0)
            sum/=i;
    }
    prime[num++]=sum;   //找出来的素因子存在这个数组里
}

int main()
{
    int t;
    scanf("%d",&t);
    ll sum;
    //primetable();
    while(t--)
    {
        sum=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
        }
 
        sushu(sum);
        ll ans=INF;
        for(int i=0;i<num/*&&prime[i]<=sum*/;i++)
        {
            if(sum%prime[i]==0)
            {
                ll tmp=cal(prime[i]);
                if(tmp<ans) ans=tmp;
            }
        }
        printf("%lld\n",ans);

        
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值