2A - Stone
任意一堆移动过后的石子都是整数x的倍数,
那么石子总数显然也应该是x的倍数,
换句话说,x必为石子数总和的一个质因子.
题目要求移动次数尽量小,那么x也应该尽量小.
所以选择石子数总和的最小质因子.
对每堆石子数进行取模运算即可知道每堆石子需要移走或增加多少石子.
然后就可以开始模拟了.
把取模后的石子数进行从大到小的排序并求出取模后的石子总数,
然后求出可以组成x的个数.
用大石子堆到小石子堆去减这些x,在累加起来,即得到最少移动次数.
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; ll arr[];
// 得到第一个质因子
ll getFac(ll x)
{
for (ll i = ; i <= x; i++)
if (x % i == )
return i;
return -;
} bool cmp(ll a, ll b)
{
return a >= b;
} int main()
{
ios::sync_with_stdio(false);
cin.tie(); int n;
cin >> n;
ll sum = ;
for (int i = ; i < n; i++) {
cin >> arr[i];
sum += arr[i];
} // 最小质因子的余数系最小
ll num = getFac(sum);
sum = ;
for (int i = ; i < n; i++) {
arr[i] = arr[i] % num;
sum += arr[i];
} // 模拟
sort(arr, arr + n, cmp);
ll k = sum / num;
ll ans = ;
for (int i = ; i < k; i++) {
ans += num - arr[i];
}
cout << ans << endl;
return ;
}
2A - Stone的更多相关文章
- 红米2A高配刷机记录
2014816 机型:红米2A高配版 设备型号:2014816 CPU:高通 线刷:fastboot平台 http://192.168.7.118/MesReports/Reports/Cutting ...
- POJ1740A New Stone Game[组合游戏]
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5769 Accepted: 3158 ...
- timus 1180. Stone Game 解题报告
1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...
- mac上执行sed的编辑 -i命令报错sed: 1: "test.txt": undefined label ‘est.txt’或sed: 1: "2a\test\": extra characters after \ at the end of a command
问题一 sed编辑命令:[sed -i 's/a/b/g' test.txt] 报错:sed: 1: "test.txt": undefined label 'est.txt' ...
- HDU 4048 Zhuge Liang's Stone Sentinel Maze
Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/327 ...
- POJ 1740 A New Stone Game
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5453 Accepted: 2989 ...
- Light OJ 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- poj 1115 Lifting the Stone 计算多边形的中心
Lifting the Stone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- 【POJ】A New Stone Game(博弈论)
http://poj.org/problem?id=1740 题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...
随机推荐
- 01 java断言assert初步使用:断言开启、断言使用
参考文件:http://blog.sina.com.cn/s/blog_59c9412d0100fd55.html 1 说明 java断言assert是jdk1.4引入的. jvm断言默认是关闭的. ...
- docker下 zookeeper集群
首先创建所需的文件夹 mkdir -p /data/{data1,data2,data3} mkdir -p /data/{datalog1,datalog2,datalog3} docker-com ...
- AlphaPose ubuntu16 python2安装
#https://www.tensorflow.org/install/install_linux#ValidateYourInstallation #https://github.com/MVIG- ...
- Windows下Memcached的安装配置方法
1.将第一个包解压放某个盘下面,比如在c:\memcached. 2.在终端(也即cmd命令界面)下输入 'c:\memcached\memcached.exe -d install' 安装. 3.再 ...
- 507. Perfect Number 因数求和
[抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...
- 面试题:filter过滤器 listener 监听器 案例有点用
1.Filter工作原理(执行流程) 当客户端发出Web资源的请求时,Web服务器根据应用程序配置文件设置的过滤规则进行检查,若客户请求满足过滤规则,则对客户请求/响应进行拦截,对请求头和请求数据进行 ...
- Solidity 没名字的function(){...}作用
官方解释: 这个叫做fallback function,当有人 1. 只发送以太币给合约而不带任何输入数据:2. 调用smart contract时调起了一个不存在的方法.会触发执行这个方法. Wha ...
- IIS身份验证知识摘录
IIS 身份验证 ASP.NET 身份验证分为两个步骤.首先,Internet 信息服务 (IIS) 对用户进行身份验证,并创建一个 Windows 令牌来表示该用户.IIS 通过查看 IIS 元数据 ...
- Mybatis_映射文件_Select
一.Select元素来定义查询操作 Id:唯一标识符.用来引用这条SQL语句,需要和接口的方法名一致 parameterType:参数类型.可以不传,MyBatis会根据TypeHandler自动推断 ...
- 几个SQL小知识(转)
出处:http://www.cnblogs.com/wuguanglei/p/4205976.html 写在前面的话:之前做的一个项目,数据库及系统整体构架设计完成之后,和弟兄们经过一段时间的编码,系 ...