HDU-6237:A Simple Stone Game(数论)
来源:互联网 发布:尼尔森数据分析报告 编辑:程序博客网 时间:2024/05/18 01:19
A Simple Stone Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 116 Accepted Submission(s): 25
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 withN 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.
The game goes like this: one player starts the game with
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 containsN positive number, the i th number ai(1≤ai≤100000) indicating the number of stones of the i th pile.
The sum ofN of all test cases is not exceed 5∗105 .
The second line contains
The sum of
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
251 2 3 4 525 7
Sample Output
21
思路:可以考虑枚举石头数量总和的质因子x,更新答案。对于一个因子x,分别求出a[i]%x的值,那么最少移动次数就相当于把Σ(a[i]%x)剩下的石头移动到Σ(a[i]%x)/x个石堆中的次数,那么当然是选前(a[i]%x)/x大的石堆,然后把其它剩下的石头移到这个前(a[i]%x)/x大的石堆中,即ans=Σ(a[i]%x)-a[i]%x中前k大的a[i]%x。
#include<bits/stdc++.h>using namespace std;const int MAX=1e5+10;long long a[MAX];int all=0,n;int pr[MAX+100]; //素数表bool isp[MAX+10];void init(){ all = 0; memset(isp,0,sizeof isp); isp[1]=1; for(int i=2;i<=MAX;i++) { if(!isp[i])pr[all++] = i; for(int j=0;j<all;j++) { long long t = 1LL*pr[j]*i ; if(t<=MAX) { isp[t] = true; if(i%pr[j]==0)break; } else break; } } return ;}struct cmp{ bool operator() (const int a,const int b) const{return a<b;}};long long slove(int prim){ long long sum=0; priority_queue<int,vector<int>,cmp>p; for(int i=1;i<=n;i++) { sum+=a[i]%prim; if(a[i]%prim)p.push(a[i]%prim); } int t=sum/prim; while(t--) { sum-=p.top(); p.pop(); } return sum;}int main(){ init(); int T;cin>>T; while(T--) { scanf("%d",&n); long long sum=0; for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum+=a[i]; sort(a+1,a+n+1); long long ans=sum-a[n]; for(int i=0;i<all;i++) { if(sum%pr[i]==0)ans=min(ans,slove(pr[i])); } printf("%lld\n",ans); } return 0;}
阅读全文
1 0
- HDU-6237:A Simple Stone Game(数论)
- HDU 6237 A Simple Stone Game
- hdu A Simple Stone Game
- hdu 6237 A Simple Stone Game (求素因子+贪心)
- HDU-6237 A Simple Stone Game (素数筛 + 贪心)
- hdu 2486 A simple stone game
- 【hdu 2486】A simple stone game
- hdoj 6237 A Simple Stone Game
- A simple stone game (poj3922,博弈)
- hdu2486 A simple stone game
- HDU_2580 A simple stone game
- hdu2580 a simple stone game
- HDU A simple stone game [K倍动态减法游戏]
- HDU 2486 HDU2580 POJ3922 a simple stone game
- A simple stone game - HDU 2486 K倍递减博弈
- HDU 6237 A Simple Stone Game(CCPC 2017 哈理工)(素数分解+贪心)(好题)
- HDU 6237/2017CCPC哈尔滨 A Simple Stone Game【质因子】
- POJ 3922 A simple stone game
- 6.4
- 童贞的过往——IT
- 数据库查询语句易错例题整理
- Mybatis学习笔记(十五)【查询缓存之一级缓存】
- 学习shell脚本语言之注意点
- HDU-6237:A Simple Stone Game(数论)
- 现实版钢铁侠
- String,StringBuffer,StringBuilder三者区别
- 算是一个刚刚踏入IT界的新人的自我介绍
- 漫步最优化四十三——拟牛顿法
- 深入理解 Spring 事务原理
- 6.11
- 一位资深程序员大牛给予Java初学者的学习路线建议
- 高性能 MySQL 笔记