HDU 4611 Balls Rearrangement(2013多校2 1001题)
Balls Rearrangement
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 25 Accepted Submission(s): 8
This work may be very boring, so he wants to know the cost before the rearrangement. If he moves a ball from the old box numbered a to the new box numbered b, the cost he considered would be |a-b|. The total cost is the sum of the cost to move every ball, and it is what Bob is interested in now.
Then T test case followed. The only line of each test case are three integers N, A and B.(1<=N<=1000000000, 1<=A,B<=100000).
相当于求 abs(i%A - i%B)对i从0~N-1求和
题目给了N,A,B;
数据比较大。
首先可以确定的是A,B的LCM是一个循环。
然后一段的话,用模拟,相同段直接跳过求解,
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
using namespace std;
long long gcd(long long a,long long b)
{
if(b==)return a;
else return gcd(b,a%b);
}
long long lcm(long long a,long long b)
{
return a/gcd(a,b)*b;
}
long long calc(int n,int a,int b)
{
long long ans = ;
int i = ;
int ta=,tb=;
int p = ;
while(i < n)
{
if(ta+a >= n && tb+b >= n)
{
ans += (long long)(n-i)*p;
i = n;
continue;
}
if(ta+a < tb+b)
{
ans += (long long)p*(ta+a-i);
i = ta+a;
p = i - tb;
ta+=a;
}
else if(ta+a==tb+b)
{
ans+= (long long)p*(ta+a-i);
i = ta+a;
ta+=a;
tb+=b;
p = ;
}
else
{
ans += (long long)p*(tb+b-i);
i = tb+b;
tb+= b;
p = i-ta;
}
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int n,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&a,&b);
if(a==b)
{
printf("0\n");
continue;
}
if(a < b)swap(a,b);
long long LCM = lcm(a,b);
if(LCM >= n)
{
printf("%I64d\n",calc(n,a,b));
continue;
}
long long tmp = calc(LCM,a,b);
long long ans = tmp * (n/LCM)+calc(n%LCM,a,b);
printf("%I64d\n",ans);
}
return ;
}
HDU 4611 Balls Rearrangement(2013多校2 1001题)的更多相关文章
- HDU 4696 Answers (2013多校10,1001题 )
Answers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- HDU 4666 Hyperspace (2013多校7 1001题 最远曼哈顿距离)
Hyperspace Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- HDU 4643 GSM (2013多校5 1001题 计算几何)
GSM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...
- HDU 4686 Arc of Dream (2013多校9 1001 题,矩阵)
Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- HDU 4611 Balls Rearrangement 数学
Balls Rearrangement 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4611 Description Bob has N balls ...
- HDU 4705 Y (2013多校10,1010题,简单树形DP)
Y Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...
- HDU 4704 Sum (2013多校10,1009题)
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submi ...
- HDU 4699 Editor (2013多校10,1004题)
Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- HDU 4678 Mine (2013多校8 1003题 博弈)
Mine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
随机推荐
- Centos更新配置文件命令
source 命令是 bash shell 的内置命令,从 C Shell 而来.source 命令的另一种写法是点符号,用法和 source 相同,从Bourne Shell而来.source 命令 ...
- 如何优雅地使用vim编辑器
如何优雅地使用vim编辑器大赞..原文链接:http://jackiekuo.com/code/2014/05/07/use-vim-the-pragmatic-way/ 接上篇如何优雅地使用Vim? ...
- SuSE Linux Supervisor的安装与使用案例
建议使用 root 管理员账户操作 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务器一开机就启动服务器 ...
- 亲手安装RabbitMq 3.7.2 并安装Trace插件
===============================================================================================1.安装E ...
- 洛谷P1803凌乱的yyy 题解
题目传送门 这道题也是贪心思想.先排序一遍(按照结束的时间排)在进行扫描,满足条件的直接保存,增加ans,最后输出即可. #include<bits/stdc++.h> using nam ...
- 如何在获取celery中的任务执行情况
开始以为在flower中获取,原来flower也是从celery中获取的. 如果直接用celery命令,一直会提示拒绝连接. 网上说了,用django命令就可以的. 于是试了下,OK了. 这样,至少可 ...
- MINIBASE源代码阅读笔记之HFPage
HFPage heap file的page的类 成员 slot_t:用来表示页里的slot,包括offset和length slot[]:倒着生长的slot array slotCnt:有多少已用sl ...
- linux的文件隐藏属性 chattr lsattr
<鸟哥的Linux私房菜 基础学习篇(第三版)> 7.4.2 读书笔记 显然,这里要说的并不是rwx那9个权限,而是要聊chattr和lsattr这两个命令.这两个命令在只能在Ext2/E ...
- [实战]MVC5+EF6+MySql企业网盘实战(5)——ajax方式注册
写在前面 今天贴合到实际的客户需求仔细的想了下,其实在userInfo这个类里面很多字段都不是必须的.有很多的事业单位根本就不能上网,填写的邮箱也是exchange的,个人的详细信息都在ad里面可以取 ...
- [前端神器]handlebars+requirejs基本使用方法
最近在某网站看到了handlebars.js,出于好奇就百度了下这是神马玩意,结果让我很是欢喜,于是就开始自学下,handlebars就几个方法,蛮简单,言归正传! 以下是基本教学逻辑演示,会附完整代 ...