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的 ...
随机推荐
- day 4 __all__ 包 __init__.py
1.__all__的作用 如果一个文件中有__all__变量,那么也就意味着这个变量中的元素,不会被from xxx import *时导入 __all__ = ["test1", ...
- .net core 基于multipart/form-data的文件上传,这里以图片上传为例
首先传递的数据格式大概如下: 然后就可以在后端获取数据了:直接上代码了哈: [HttpPost] ///分别获取 data数据和调用图片上传方法 public async Task< ...
- SpringBoot-05:SpringBoot初运行以及tomcat端口号的修改
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 上篇博客讲了,如何创建SpringBoot工程,所以,我本篇博客讲述,如何跑起来自己的第一个案例 1.准备一个 ...
- springBoot RabbitMq 转换json序列化
package com.alirm.redis_cache.config.RabbitMQ; import org.springframework.amqp.rabbit.core.RabbitTem ...
- 「题目代码」P1060~P1065(Java)
P1060 谭浩强C语言(第三版)习题7.5 注意行末空格. import java.util.*; import java.io.*; import java.math.*; import java ...
- 「功能笔记」性能分析工具gprof使用笔记
根据网上信息整理所成. 功能与优劣 gprof实际上只是一个用于读取profile结果文件的工具.gprof采用混合方法来收集程序的统计信息,它使用检测方法,在编译过程中在函数入口处插入计数器用于收集 ...
- RSA加密通信小结(一)
一.背景描述 帮朋友完成相关方案的改进. 二.计划与方案 1.加密方式采用RSA 1024加密. 2.发送与接收都采用RSA加密,采用两套不同的密钥. 3.统一的加解码函数.(此处除了对于传输数据进行 ...
- linux部署MantisBT(二)部署php
二.部署php 1.下载php安装包 http://php.net/downloads.php 2.安装php yum install libxml2 yum install libxml2-deve ...
- Java学习 · 初识 IO流
IO流 1. 原理与概念 a) 流 i. 流动,流向 ii. 从一端移动到另一端 源头到目的地 iii. 抽象.动态概念,是一连 ...
- Idea Live Templates
常用live templates 模板 注释 : * * @param $params$ * @return $return$ * $date$ $time$ chiyuanzhen743 */ lo ...