2017中国大学生程序设计竞赛-哈尔滨站 H - A Simple Stone Game
A Simple Stone Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1526 Accepted Submission(s): 346
The game goes like this: one player starts the game with N piles of stones. There is a_i 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 b_i is the multiple of x where b_i
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 second line contains N positive number, the ith number a_i (1 \leq a_i \leq 100000) indicating the number of stones of the ith pile.
The sum of N of all test cases is not exceed 5 * 10^5.
5
1 2 3 4 5
2
5 7
1
/*************************************************************************
> 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=;i<MAXN;i++){
if(prime[i]==false)
p[tol++]=i;
for(LL j=;j<tol&&i*p[j]<MAXN;j++){
prime[i*p[j]]=true;
if(i%p[j]==)
break;
}
}
} inline void div(LL x){
memset(fa,,sizeof fa);
pos=;
for(LL i=;i<tol&&p[i]*p[i]<=x;i++){
if(x%p[i]==){
fa[pos++]=p[i];
while(x%p[i]==) x/=p[i];
}
}
if(x>) fa[pos++]=x;
} inline void init(){
g=;
sum=;
v.clear();
res=INF;
} int main(){
pre();
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(LL i=;i<n;i++){
scanf("%lld",&a[i]);
sum+=a[i];
g=__gcd(a[i],g);
}
if(n==){
puts("");
continue;
}
div(sum);
for(LL i=;i<pos;i++){
v.clear();
mod=fa[i];
LL cnt=;
for(LL i=;i<n;i++){
if(a[i]%mod!=){
v.push_back(a[i]%mod);
cnt+=a[i]%mod;
}
}
sort(v.begin(),v.end());
LL tol=cnt/mod;
LL s=;
for(LL i=(LL)v.size()-;i>=;i--){
tol--;
s+=(LL)(mod-v[i]);
if(tol<=) break;
}
res=min(res,s);
}
printf("%lld\n",res);
}
return ;
}
2017中国大学生程序设计竞赛-哈尔滨站 H - A Simple Stone Game的更多相关文章
- 2017中国大学生程序设计竞赛-哈尔滨站 Solution
A - Palindrome 题意:给出一个字符串,找出其中有多少个子串满足one-half-palindromic 的定义 思路:其实就是找一个i, j 使得 以i为中轴的回文串长度和以j为中轴的 ...
- HDU6237-A Simple Stone Game-找素因子(欧拉函数)-2017中国大学生程序设计竞赛-哈尔滨站-重现赛
A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDU6235-Permutation-水题-2017中国大学生程序设计竞赛-哈尔滨站-重现赛
Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tot ...
- HDU 6235.Permutation (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tot ...
- 2017中国大学生程序设计竞赛-哈尔滨站 A - Palindrome
Palindrome Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tota ...
- 【2017中国大学生程序设计竞赛-哈尔滨站】B - K-th Number
原题: 题意: 给你一个长度为N的正整数组A,对于这个数组的所有子区间,若长度小于k则不管它,若长度大于等于k则取第k大放入数组B 问你B中第M大的数是谁 一眼序列分治,然而没思路 数据结构?能想到从 ...
- HDU 6273.Master of GCD-差分数组 (2017中国大学生程序设计竞赛-杭州站-重现赛(感谢浙江理工))
Super-palindrome 题面地址:http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf 这道题是差分数组的题目,线 ...
- HDU 6154 - CaoHaha's staff | 2017 中国大学生程序设计竞赛 - 网络选拔赛
/* HDU 6154 - CaoHaha's staff [ 构造,贪心 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 整点图,每条线只能连每个方格的边或者对角线 问面积大于n的 ...
随机推荐
- IAR环境下编译CC2640入门开发
1. 安装SDK包,之后导入AIR里面,编译报错 看样子似乎是xdc工具的路径配置不对,进入路径配置对话窗 开始配置 配置完之后,重新编译 Fatal Error[Pe1696]: cannot op ...
- Linux 下获取通讯IP
#!/bin/sh # filename: get_net.sh default_route=$(ip route show) default_interface=$() address=$(ip a ...
- 了解Python控制流语句——continue 语句
continue 语句用以告诉 Python 跳过当前循环块中的剩余语句,并继续该循环的下一次迭代. 案例(保存为 continue.py): while True: s = input('Enter ...
- 157. Unique Characters 【LintCode by java】
Description Implement an algorithm to determine if a string has all unique characters. Example Given ...
- JavaScript 正则
元字符 预定义类 边界 ^在中括号中时,匹配非hello的 str = 'hello world' str.match(/[^hello]/g) //[" ", "w&q ...
- PAT-甲级解题目录
PAT甲级题目:点这里 pat解题列表 题号 标题 题目类型 10001 1001 A+B Format (20 分) 字符串处理 1003 1003 Emergency (25 分) 最短路径 ...
- Java进阶——— 线程池的原理分析
前言 在了解线程池之前,其实首先出现的疑问是:为什么要使用线程池,其次是了解什么是线程池,最后是如何使用线程池,带着疑问去学习. 为什么要使用 前面多线程文章中,需要使用线程就开启一个新线程,简单方便 ...
- 论文阅读之Joint cell segmentation and tracking using cell proposals
论文提出了一种联合细胞分割和跟踪方法,利用细胞segmentation proposals创建有向无环图,然后在该图中迭代地找到最短路径,为单个细胞提供分割,跟踪和事件. 3. PROPOSAL GE ...
- python leveldb 文档
标签(空格分隔): python leveldb import leveldb db = leveldb.LevelDB('./db') db.Put('hello', 'world') print ...
- Redis 错误摘记篇
yum安装的redis提示如下报错,大概意思就是配置文件和redis-server进程文件版本不一致.. [root@vm-10-104-28-24 yum.repos.d]# redis-serve ...