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 ...
随机推荐
- HOJ 1108
题目链接:HOJ-1108 题意为给定N和M,找出最小的K,使得K个N组成的数能被M整除.比如对于n=2,m=11,则k=2. 思路是抽屉原理,K个N组成的数modM的值最多只有M个. 具体看代码: ...
- selenium grid结构图
调用 Selenium-Grid 的基本结构图如下: 上面是使用 selenium-grid 的一种普通方式,仅仅使用了其支持的分布式执行的功能,即当你同时需 要测试用例比较多时,可以平行的执行这些用 ...
- Python实现好友全头像的拼接
微信好友全头像 话不多说,直接上代码 import itchat import math import PIL.Image as Image import os itchat.auto_login() ...
- Django 1.10文档中文版Part2
目录 2.5 第一个Django app,Part 3:视图和模板 2.5.1 概览 2.5.2 编写更多的视图 2.5.3 编写能实际干点活的视图 2.5.4 404错误 2.5.5 使用模板系统 ...
- acm专题---拓扑排序+优先队列
struct node{ int id; int cnt; node(int _id,int _cnt):id(_id),cnt(_cnt){} bool operator<(node a) c ...
- 构建基于TCP的应用层通信模型
各层的关系如下图,表述的是两个应用或CS间通信的过程: 通常使用TCP构建应用时,需要考虑传输层的通信协议,以便应用层能够正确识别消息请求.比如,一个请求的内容很长(如传文件),那肯定要分多次发送 ...
- IIS 部署nodejs
1.Node.js 2.IIS的URL Rewrite模块 3.iisnode
- Redis 在Centos7下配置开机自启动
设置Redis开机启动需要如下几个步骤: 编写配置脚本 [ vim /etc/init.d/redis ] #!/bin/sh # # Simple Redis init.d script conce ...
- Java8所有的包介绍(由英文文档翻译而来)
转载: Java8所有的包介绍(由英文文档翻译而来)
- Shiro切入Spring的方式
在springMVC中要使用shiro,一般都遵循下面的配置: applicationContext-shiro.xml <bean id="shiroFilter" cla ...