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
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; }
相关文章推荐
- hdu 6237 A Simple Stone Game (求素因子+贪心)
- HDU-6237 A Simple Stone Game (素数筛 + 贪心)
- HDU-6237:A Simple Stone Game(数论)
- HDU - 6237 A Simple Stone Game (2017CCPC哈尔滨站 思维)
- HDU 6237 A Simple Stone Game
- HDU 6237 A Simple Stone Game(CCPC 2017 哈理工)(素数分解+贪心)(好题)
- [K倍动态减法问题] HDU 2486 & HDU2580 & POJ3922 a simple stone game
- 【hdu 2486】A simple stone game
- HDU_2580 A simple stone game
- HDU 2486 (HDU 2580, POJ 3922) A simple stone game K倍动态减法游戏
- HDU A simple stone game [K倍动态减法游戏]
- HDU 2486 HDU2580 POJ3922 a simple stone game
- hdu 2486/2580 / poj 3922 A simple stone game 博弈论
- hdu 2486 A simple stone game
- hdoj 6237 A Simple Stone Game
- A simple stone game - HDU 2486 K倍递减博弈
- hdu 2486 A simple stone game
- hdu A Simple Stone Game
- HDU 6237/2017CCPC哈尔滨 A Simple Stone Game【质因子】
- hdu 1851 A Simple Game Java 水题系列 博弈