http://acm.hdu.edu.cn/showproblem.php?pid=4710

Balls Rearrangement

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 735    Accepted Submission(s): 305

Problem Description
Bob has N balls and A boxes. He numbers the balls from 0 to N-1, and numbers the boxes from 0 to A-1. To find the balls easily, he puts the ball numbered x into the box numbered a if x = a mod A. Some day Bob buys B new boxes, and he wants to rearrange the balls from the old boxes to the new boxes. The new boxes are numbered from 0 to B-1. After the rearrangement, the ball numbered x should be in the box number b if x = b mod B. 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.
 
Input
The first line of the input is an integer T, the number of test cases.(0<T<=50) 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).
 
Output
For each test case, output the total cost.
 
Sample Input
3
1000000000 1 1
8 2 4
11 5 3
 
Sample Output
0
8
16
 
Source

分析:

模拟,每次增加 step ,一次可以放一块。

AC代码:

 #include<iostream>
#include<stdio.h>
#include<math.h>
#define min(a,b) a>b?b:a
using namespace std;
int main()
{
int T,n,a,b;
cin>>T;
while(T--)
{
cin>>n>>a>>b;
if(a==b)
printf("0\n");
else
{
__int64 ans=,step=,i;
for(i=;i<n;i=i+step)
{
int stepa=a-i%a;
int stepb=b-i%b;
step=min(stepa,stepb);
__int64 dis=abs(i%a-i%b);
if(i+step>=n)
dis=dis*(n-i);
else
dis=dis*step;
ans=ans+dis;
}
printf("%I64d\n",ans);
}
}
return ;
}

hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup的更多相关文章

  1. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  2. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

  3. hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...

  4. hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...

  5. hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  6. hduoj 4706 Children&#39;s Day 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4706 Children's Day Time Limit: 2000/1000 MS (Java/Others) ...

  7. 2013 ACM/ICPC Asia Regional Online —— Warmup

    1003 Rotation Lock Puzzle 找出每一圈中的最大值即可 代码如下: #include<iostream> #include<stdio.h> #inclu ...

  8. HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)

    Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...

  9. HDU 4749 Parade Show 2013 ACM/ICPC Asia Regional Nanjing Online

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题目大意:给一个原序列N,再给出一个序列M,问从N中一共可以找出多少个长度为m的序列,序列中的数 ...

随机推荐

  1. spring源码学习之路---IOC容器初始化要义之bean定义载入(五)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 最近工作很忙,时间不多,研究 ...

  2. FPGA的典型应用领域

    本文关键字:fpga应用,fpga应用领域, fpga培训,FPGA应用开发入门与典型实例 一.数据采集和接口逻辑领域 1.FPGA在数据采集领域的应用 由于自然界的信号大部分是模拟信号,因此一般的信 ...

  3. Secrets

    https://www.youtube.com/watch?v=qHm9MG9xw1o I need another storySomething to get off my chestMy life ...

  4. Flex Flash Player回声消除的最佳方法

    Adobe Flash Player 已经成为音频和视频播放的非常流行的工具.实际上,目前大多数因特网视频均使用 Flash Player观看. Flash Player 通过将许多技术进行组合可以提 ...

  5. 李洪强iOS经典面试题129

    1. 怎么解决缓存池满的问题(cell) ios中不存在缓存池满的情况,因为通常我们ios中开发,对象都是在需要的时候才会创建,有种常用的说话叫做懒加载,还有在UITableView中一般只会创建刚开 ...

  6. java设计模式。。。转载

    maowang I am a slow walker,but I never walk backwards! 博客园 首页 新随笔 联系 订阅 管理 随笔 - 125  文章 - 0  评论 - 12 ...

  7. Android-----搭建开发环境AND模拟器配置AND启动项目

    开发工具我这里用的是eclipse 你也可以用Google最新推出的Android Studio开发工具(不需要配置) 下载地址:https://developer.android.com/sdk/i ...

  8. CodeForces 219D 树形DP

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  9. vbox下Oracle Enterprise liunx5.4虚拟机安装10G RAC实验(一)

    1.配置第一个虚拟机 1.1 安装后的登录界面 1.2 第1台机器(单数据配置方面) 1.2.1 验证安装包 1.2.2 修改内核参数 1.2.3添加安全限制 1.2.4关闭防火墙 1.2.5添加用户 ...

  10. hadoop-1.2.1安装配置

    1.准备三台节点 hnd1  hnd2  hnd3 下载 hadoop 下载地址:http://apache.fayea.com/hadoop/common/ API文档:http://hadoop. ...