您的位置:首页 > 其它

hdu 6237 A Simple Stone Game (求素因子+贪心)

2017-11-14 21:06 423 查看

A Simple Stone Game

[b]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 797    Accepted Submission(s): 162
[/b]

[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

2
5
1 2 3 4 5
2
5 7

 

[align=left]Sample Output[/align]

2
1

 

[align=left]Source[/align]
2017中国大学生程序设计竞赛-哈尔滨站-重现赛(感谢哈理工)

 

题意:
有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;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: