您的位置:首页 > 其它

hdu 6237 A Simple Stone Game

2018-01-31 20:47 357 查看
[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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int a[maxn];
ll sum,ans,tp1,tp2,tp3;
queue<ll> que;
vector<ll> nm;
void del(ll n)
{
for(ll i=2;i*i<=n;i++)
if((!(n%i))&&(n>=i))
{
while(!(n%i))
n/=i;
que.push(i);
}
if(n!=1)
que.push(n);
}
int main()
{
int t,n,i;
scanf("%d",&t);
while(t--&&scanf("%d",&n)!=EOF)
{
for(sum=0,i=0;i<n;i++)
{
scanf("%d",a+i);
sum+=a[i];
}
del(sum);
ans=1e10+5;
while(que.size())
{
tp1=que.front();
que.pop();
nm.clear();
tp2=tp3=0;
for(i=0;i<n;i++)
if(a[i]%tp1)
{
nm.push_back(a[i]%tp1);
tp2+=a[i]%tp1;
}
sort(nm.begin(),nm.end());
for(vector<ll>::iterator it=(nm.end()-1);it>=nm.begin();it--)
{
tp3+=tp1-*it;
tp2-=tp1;
if(tp2<=0)
break;
}
ans=min(ans,tp3);
}
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: