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堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...
随机推荐
- ZOJ3954 Seven-Segment Display
题意: emmmm见原题吧 分析: 这也是当时省赛选拔的题,场上以为是大模拟,然后没敢写...补题发现是道水题··· 因为每一列的顺序不一定,但是行是一定的.所以只要把每一列组成一个数字,然后弄两个集 ...
- XML数据格式简介
---------------siwuxie095 XML 简介 XML,即 可扩展标记语言(Extensible Markcup La ...
- 190. Reverse Bits 二进制相反数
[抄题]: Reverse bits of a given 32 bits unsigned integer. Example: Input: 43261596 Output: 964176192 E ...
- Linux查看操作系统版本的几种方式
Linux查看操作系统版本的几种方式: 1.uname -a 2.lsb_release -a 3.cat /etc/issue 4.cat /proc/version 5.cat /etc/redh ...
- 在Python中操作谷歌浏览器
在Python中使用谷歌浏览器,注意以下几点: 1.下载安装的谷歌浏览器Chrome和驱动chromedriver.exe要版本一致. 2.驱动chromedriver.exe要放在Chrome浏览器 ...
- A. Xor-tree
题目意思: 给一颗n个节点的树,每个节点有一个值要么是0要么是1,改变某个节点的值时,它的儿子不变,它儿子的儿子翻转,它儿子的儿子的儿子不变,如此类推.给定各个节点的目标值,求最少的翻转次数,使得达到 ...
- SQL将表中某一类型的一列拼接成一行
SELECT TypeName ,(SELECT ','+ UserName FROM [ContainerMembers] t WHERE TypeName= aa.TypeName FOR XML ...
- Mac10.9下的libtiff编译
libtiff介绍 libtiff下载 libtiff编译 libtiff介绍? 参考:http://en.wikipedia.org/wiki/Tiff libtiff下载 直接到官网下载:http ...
- javascrip总结12:逻辑运算符与等号运算符
1 逻辑运算符 逻辑运算的结果只有true 或者 false. 1.1 与&&: 两个表达式为true的时候,结果为true. 1.2 或|| 只要有一个表达式为true,结果为tru ...
- css3的那些高级选择器一
css大家都不陌生了,从1996年12月css1正式推出,经历了1998年5月css2,再到2004年2月css2.1,最后一直到2010年推出的css3.css的推出给web带来巨大 的改变,使我们 ...