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. android实现两个页面跳转

    1.实现两个界面之间转换 在安卓当中,最常见的就是按下按钮之后跳转到第二个界面. 关键代码很简单: 这是以bn2按钮为例,当前Activity为MainActivity,跳转到Registration ...

  2. Maven_根据不同个环境打包, 获取不同的配置文件等等

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  3. PHP slim restfull框架nginx 配置

    http://docs.slimframework.com/ 下载地址这个东西很不错,照到官方的例子做 <?php require 'vendor/autoload.php'; $app = n ...

  4. php继承后构造函数的特性

    在5.x版本的php中: 如果父类有构造函数,它的子类也有构造函数,那么在运行子类时就“不会执行父类的构造函数”. 要想执行父类的构造函数,需要在子类的构造函数中加上: parent::__const ...

  5. jQuery -> 获取元素的各种过滤器

    转自http://blog.csdn.net/feelang/article/details/26613023

  6. js 随机数 洗牌算法

    function shuffle(arr){ var len = arr.length; for(var i = 0;i<len -1;i++) { var idx = Math.floor(M ...

  7. mysql分区及实例演示

    一.为什么要分区? 需求:大数据.解决方案:分而治之,更细一点即为.将大表和大索引分为一个更小的操作单元 在mysql中,分区允许将表.索引和索引编排表细分为更小的单元.分区后,每个分区有自己单独的名 ...

  8. C# 路径

    一.根目录 .// 或者直接给出文件名称,是找根目录的路径 如:path = "test.xml" 与 path = ".//test.xml"是一个意思 二. ...

  9. 【iCore3 双核心板】例程五:SYSTICK定时器实验——定时点亮LED

    实验指导书及代码包下载: http://pan.baidu.com/s/1eQsKcEY iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  10. AjaxPost、冒泡示例

    //Ajax提交表单 $("#AssetsForm").submit(function () { $.ajax({ type: "post", url: &qu ...