Balls Rearrangement

Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1682 Accepted Submission(s): 634

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
 
Recommend
zhuyuanchen520
唉,比赛的时候,一直想通过推出公式来,纠结在起过一个周期时,找到规律,其实,我们只要把有统一差值的一起算,不是统一差值的下次再算就已经很快了啊!
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
__int64 gcd(__int64 a,__int64 b)
{
if(a==0)return b;
return gcd(b%a,a);
}
__int64 solve(__int64 n,__int64 a,__int64 b)
{
__int64 sum=0,k1=0,k2=0,i=0,temp;
while(i<n)
{
temp=min(a-k1,b-k2);
i+=temp,sum+=temp*fabs(k1-k2);
if(i>n)
sum-=(i-n)*fabs(k1-k2);
k1=(k1+temp)%a,k2=(k2+temp)%b;
}
return sum;
}
int main()
{
int tcase;
__int64 n,m,a,b;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%I64d%I64d%I64d",&n,&a,&b);
m=a*b/gcd(a,b);
if(n<m)
{
printf("%I64d\n",solve(n,a,b));
}
else
printf("%I64d\n",n/m*solve(m,a,b)+solve(n%m,a,b));
}
return 0;
}

hdu4611 Balls Rearrangement的更多相关文章

  1. HDU-4611 Balls Rearrangement 循环节,模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4611 先求出循环节,然后比较A和B的大小模拟过去... //STATUS:C++_AC_15MS_43 ...

  2. hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...

  3. 2013 多校联合 2 A Balls Rearrangement (hdu 4611)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  4. HDU 4611 Balls Rearrangement 数学

    Balls Rearrangement 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4611 Description Bob has N balls ...

  5. HDU 4611 Balls Rearrangement(2013多校2 1001题)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  6. hdu4710 Balls Rearrangement(数学公式+取模)

    Balls Rearrangement Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. Balls Rearrangement(HDU)

    Problem Description Bob has N balls and A boxes. He numbers the balls from 0 to N-1, and numbers the ...

  8. hdu 4611 Balls Rearrangement

    http://acm.hdu.edu.cn/showproblem.php?pid=4611 从A中向B中移动和从B中向A中移动的效果是一样的,我们假设从B中向A中移动 而且A>B 我们先求出所 ...

  9. 2013 Multi-University Training Contest 2 Balls Rearrangement

    先算出lcm(a,b),如果lcm>=n,则直接暴力解决:否则分段,求出0-lcm内的+0-n%lcm内的值. 再就是连续相同的一起计算!! #include<iostream> # ...

随机推荐

  1. linux 解决Ubuntu编译内核uImage出现问题“mkimage” command not found - U-Boot images will not be built问题

    解决Ubuntu编译内核uImage出现问题“mkimage” command not found - U-Boot images will not be built问题 http://www.lin ...

  2. QQ登录-第三方SDK的接入总结

    由于项目的需要,使用了[QQ登录]SDK 的相关功能!   1.集成[QQ登录]SDK   [QQ登录]SDK下载地址: http://wiki.open.qq.com/wiki/website/SD ...

  3. Hbiernate关联排序问题

    使用场景: 假设有两张表请求信息.账户表,它们之间是一对多的关系.对应的java类分别为Sfcx_RequestInfo和Sfcx_Zhxx.Sfcx_RequestInfo有一个Set属性 sfcx ...

  4. Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)

    解题报告 二分图第一题. 题目描写叙述: 为了參加即将召开的会议,A国派出M位代表,B国派出N位代表,(N,M<=1000) 会议召开前,选出K队代表,每对代表必须一个是A国的,一个是B国的; ...

  5. Visual Studio2013创建、公布监控Windows Azure网站

    原文 Visual Studio2013创建.公布监控Windows Azure网站 随着Visual Studio 2013的发布,现在我们可以在Visual Studio内部实现Windows A ...

  6. 菜单组件——axure线框图部件库介绍

    软件类的教程,我写不出长篇大论,这里面的都是基础的操作,希望初学者,根据一个功能演示,可以自己测试其他功能菜单的效果! Axure自带的菜单组件,我几乎没有用到过,做菜单导航,我第一时间想到的还是矩形 ...

  7. JSP的学习(3)——语法知识二之page指令

    本篇接上一篇<JSP的学习(2)——语法知识一>,继续来学习JSP的语法.本文主要从JSP指令中的page指令,对其各个属性进行详细的学习: JSP指令: JSP指令是为JSP引擎而设计的 ...

  8. greatis很不错,出售源代码

    http://www.greatis.com/delphicb/ 特别是: http://www.greatis.com/delphicb/imgedit/

  9. ubuntu环境ceph配置入门(一)

    环境:ubuntu server 14.04 64bit,安装ceph版本号0.79 正常情况下应有多个主机,这里为了高速入门以一台主机为例,多台主机配置方式类似. 1. 配置静态IP及主机名 静态I ...

  10. 积累的VC编程小技巧之工具条和状态条

    1.工具条和状态条中控件的添加: 方法⑴.只能在ToolBar里创建控件:首先,在ToolBar中创建一个Button,其ID为ID_TOOL_COMBO(我们要将创建的控件放在该Button的位置上 ...