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堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...
随机推荐
- 进程间通信___命名管道(FIFO)
命名管道(FIFO) 基本概念 命名管道和一般的管道基本相同,但也有一些显著的不同: 命名管道是在文件系统中作为一个特殊的设备文件而存在的. 不同祖先的进程之间可以通过管道共享数据. 当共享管道的进程 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial—5.4 绘制数据图
5.4 绘制数据图 参考视频: 5 - 4 - Plotting Data (10 min) 5.4.1 绘制曲线 1.画一个sin曲线 >> t = [:0.01:0.98]; > ...
- libevent源码分析
这两天没事,看了一下Memcached和libevent的源码,做个小总结. 1.入门 1.1.概述Libevent是一个用于开发可扩展性网络服务器的基于事件驱动(event-driven)模型的网络 ...
- 记一篇Python学习的简易版教程
廖雪峰的教学博客https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143178 ...
- 虚拟机Ubuntu16.04安装lrzsz
[系统环境] 宿主机:Win7 64位 虚拟机软件:Vmware workstation 12 虚拟机:Ubuntu 16.0.4 [目的] 配合Secure CRT使用rz,sz,方便在Ubuntu ...
- js 操作table
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs ...
- Hyperledger Fabric开发
打开Hyperledger Fabric在线开发文档:https://hyperledger-fabric.readthedocs.io 建议在Mac或Linux环境下操作,因为文档基本上是按照Mac ...
- Apr编程
一.简介 http://www.xuebuyuan.com/2195578.html 二.教程 http://dev.ariel-networks.com/apr/
- 数据库MySQL 之 库操作
数据库MySQL 之 库操作 浏览目录 系统数据库简介 数据库操作 命名规范 用户权限 修改密码 忘记密码 中文乱码问题 一.系统数据库简介 information_schema :虚拟库,不占用磁盘 ...
- DataTable 设置primarykey 后进行 Merge操作
1.先查看概念 可以看highplayer博客 http://blog.csdn.net/highplayer/article/details/6613817 2. protected void st ...