hdu 6237(思考能力)
杭電広範な新入生を歓迎して2017新入生試合に参加します(赤いRegisterringをクリックします)
A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 419 Accepted Submission(s): 87
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(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.
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题意:玩一个游戏,有n堆石头,每堆石头有a[i]个,每次你能将一堆石头的一个放到其他堆,当存在一个x使得所有的a[i]%x==0,游戏结束,输出游戏进行的最小次数数。思路:首先考虑用欧拉函数分解质因数,然后枚举这些因子得到可能的x值,然后用a[i]%x得到的不为0的数字记录下来,最后就是在这里移动了,显然应该从大到小进行。统计答案时候就用当前的质因子减去模x后剩余的,最后取小就可以了。代码:#include
using namespace std; typedef long long LL; const int maxn=1e5+5; set<LL>s; vector<LL>v; LL a[maxn]; void init(LL n) { for(LL i=2;i*i<=n;i++) { if(n&&n%i==0) { while(n%i==0) { n/=i; } s.insert(i); } } if(n>1) s.insert(n); } int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); s.clear(); LL sum=0; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); sum+=a[i]; } init(sum); LL ans=1e10+10,sum1=0; for(autoit=s.begin();it!=s.end();it++)// { LL num1=*it; sum1=0; LL cnt=0; v.clear(); for(int i=1;i<=n;i++) { LL num2=a[i]%num1;//% if(num2==0) continue; v.push_back(num2); sum1+=num2; } sort(v.begin(),v.end()); for(int i=(int)v.size()-1;i>=0;i--) { cnt+=num1-v[i]; sum1-=num1; if(sum1<=0) break; } ans=min(ans,cnt); } printf("%lld
",ans); } return 0; }