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. WM_PAINT消息详解,使用InvalidateRect或InvalidateRgn函数刻意产生WM_PAINT消息(WIN7里有变化,“调整视觉效果”,将“启用桌面组合”去掉)

    什么时候会触发WM_PAINT消息消息呢? 以下内容来自大名鼎鼎的<Windows程序设计(第五版)> 大多数Windows程序在WinMain中进入消息循环之前的初始化期间都要呼叫函数U ...

  2. SIX GOD

    SIX GOD是什么意思呢.?_百度知道 SIX GOD

  3. PAT 1002 Hello World for U (20)

    Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...

  4. C++智能指针的实现

    说起智能指针,不少人都不陌生.比方auto_ptr.shared_ptr.unique_ptr.weak_ptr. 依据shared_ptr的功能,自己仿造也实现了个. 对于shared_ptr这样的 ...

  5. C语言中的enum(枚举)使用方法

    近期在写数据结构的广义表时候用到了这个概念,在学习C语言的时候没有太注意们这里学一下. 我在网上结合了非常多资料,这里自己总结一下. 首先说.JAVA和C++中都有枚举类型. 假设一个变量你须要几种可 ...

  6. HDU 4424 Conquer a New Region 最大生成树

    给你一颗树 每条边有一个权值 选择一个点为中心 定义S值为中心到其它n-1个点的路径上的最小边权 求全部点S值的和 从大到小排序 每次合并2棵树 设为A集合 B集合 设A集合的最大S值的和为sumA ...

  7. 【Demo 0009】Android 组件(BroadcastReceiver)

    本章学习要点:        1.  了解Broadcast的作用;        2.  掌握自定义广播和系统广播的接收:        3.  掌握广播的发送:

  8. 机房收费系统总结之4——VB.NET 轻松解决判断文本框、组合框为空问题

    纵观机房收费系统,判断文本框.组合框为空问题无非两种情况.第一种:判断窗体中所有文本框.组合框是否为空.第二种:判断一部分文本框.组合框是否为空.下面看看是如何实现这两种情况的. 第一种:判断窗体中所 ...

  9. C# - 使用 List<> 泛型给GridView控件数据

    创建实体模型: namespace Test.Models { public class Student { public string ID { get; set; } public string ...

  10. Servlet的学习之Session(2)

    在上一篇中我们学习了Session对象默认在一个会话过程中,由服务器创建,能保存在这个会话过程中用户访问多个web资源时产生的需要保存的数据,并在访问服务器中其他web资源时可以将这些数据从Sessi ...