标签:star 竞赛 cep == color def 问题 sim family
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1526 Accepted Submission(s): 346
/************************************************************************* > File Name: H.cpp > Author: LyuCheng > Created Time: 2017-12-01 20:41 > Description: 题意:有n堆石子,每次你可以选择一堆石子拿一个放到另一堆石子里,如果 所有的所有的石子数不互质,那么游戏结束 思路:问题的实质就是找到一个x是的b[1]%x+b[2]%x+...b[n]%x=0;也就是 sum%x=0,那么分解sum的质因子,然后枚举判断 ************************************************************************/ #include <bits/stdc++.h> #define MAXN 123456 #define LL long long #define INF 50000000005 using namespace std; bool prime[MAXN]; LL p[MAXN]; LL tol; int t; int n; LL a[MAXN]; LL g; LL pos; LL fa[MAXN]; LL sum; LL mod; LL res; vector<LL>v; inline void pre(){ for(LL i=2;i<MAXN;i++){ if(prime[i]==false) p[tol++]=i; for(LL j=0;j<tol&&i*p[j]<MAXN;j++){ prime[i*p[j]]=true; if(i%p[j]==0) break; } } } inline void div(LL x){ memset(fa,0,sizeof fa); pos=0; for(LL i=0;i<tol&&p[i]*p[i]<=x;i++){ if(x%p[i]==0){ fa[pos++]=p[i]; while(x%p[i]==0) x/=p[i]; } } if(x>1) fa[pos++]=x; } inline void init(){ g=0; sum=0; v.clear(); res=INF; } int main(){ pre(); scanf("%d",&t); while(t--){ init(); scanf("%d",&n); for(LL i=0;i<n;i++){ scanf("%lld",&a[i]); sum+=a[i]; g=__gcd(a[i],g); } if(n==1){ puts("0"); continue; } div(sum); for(LL i=0;i<pos;i++){ v.clear(); mod=fa[i]; LL cnt=0; for(LL i=0;i<n;i++){ if(a[i]%mod!=0){ v.push_back(a[i]%mod); cnt+=a[i]%mod; } } sort(v.begin(),v.end()); LL tol=cnt/mod; LL s=0; for(LL i=(LL)v.size()-1;i>=0;i--){ tol--; s+=(LL)(mod-v[i]); if(tol<=0) break; } res=min(res,s); } printf("%lld\n",res); } return 0; }
2017中国大学生程序设计竞赛-哈尔滨站 H - A Simple Stone Game
标签:star 竞赛 cep == color def 问题 sim family
原文地址:http://www.cnblogs.com/wuwangchuxin0924/p/7944420.html