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. C#_事件

    C#_事件 概述 今天用来演示事件的例子是模拟实现一个文件下载类,在这个类中我将定义一个DownLoad事件,这个事件用来在文件下载的过程中,向订阅这个事件的用户发出消息,而这个消息将用DownLoa ...

  2. 使用css3写一朵云

  3. string的不可变性

    1.不可变性 代码如下: static void Main(string[] args){string str1 = "a";string str2 = str1;str2 = & ...

  4. Mac 实用工具

    命令行常用工具: Iterm2 也是一个终端命令行工具,支持多工作区,使用清爽 http://www.iterm2.com/documentation.html 给你的命令行 代码上色 Solariz ...

  5. PHP学习之-1.5 字符串

    字符串 一个字符串是用双引号扩起来的一个词或者一个句子,比如 "Hello Word" ,你可以使用PHP语言输入这个字符串,像这样 <?php echo "Hel ...

  6. GNOME界面简单使用

    GNOME界面 CentOS下的文件夹打开方式,默认是打开一个文件夹就重新的打开一个窗口,并不是在原有的文件夹中显示要打开文件夹的内容. 怎么修改: 打开任意一个文件夹. Edit --> pr ...

  7. TControl.GetDeviceContext会给图形控件建立新的坐标原点和建立新的剪裁区域

    这是取得DC句柄的其中一种方法(会重定义原点和建立新的剪裁区): function TControl.GetDeviceContext(var WindowHandle: HWnd): HDC; be ...

  8. 【c语言】统计一个数二进制中的1的个数

    // 统计一个数二进制中的1的个数 #include <stdio.h> int count(int a) { int count = 0; while (a) { count++; a ...

  9. Git 使用规范流程(转)

    团队开发中,遵循一个合理.清晰的Git使用流程,是非常重要的. 否则,每个人都提交一堆杂乱无章的commit,项目很快就会变得难以协调和维护. 下面是ThoughtBot 的Git使用规范流程.我从中 ...

  10. 【PHP SDK for OpenStack/Rackspace APIs】身份验证

    在你使用php-opencloud之前必须先取得云服务提供商的身份验证.这是整个过程中最简单也是最让人沮丧的部分. 说它最简单是因为你只需要两部分信息: 云服务提供商的身份验证端点 用来身份验证的证书 ...