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 ;
}

最新文章

  1. 2. K线学习知识二
  2. JS原型继承和类式继承
  3. AngularJS Best Practices: ng-include vs directive
  4. PAT自测_打印沙漏、素数对猜想、数组元素循环右移、数字加倍重排、机器洗牌
  5. spring注解 @Transactional
  6. FreeBSD从零开始---安装后配置(三)
  7. UI4_UIImageView
  8. HDU1875——畅通工程再续(最小生成树:Kruskal算法)
  9. phpexcel导入excel文件报the filename xxx is not recognised as an OLE file错误。
  10. TPen的7种Style和16种Mode
  11. SpringMVC 学习-拦截器 HandlerInterceptor 类
  12. iOS 容器控制器 (Container View Controller)
  13. Eclipse添加struts2
  14. 什么是B-Tree
  15. MySQL连接数实时查看
  16. Excel下拉框多列显示,如何只显示一列
  17. Ocr答题辅助神器 OcrAnswerer4.x,通过百度OCR识别手机文字,支持屏幕窗口截图和ADB安卓截图,支持四十个直播App,可保存题库
  18. Maven 项目 无缘无故报错:版本冲突,其他机器上正常-提交的时候报冲突怎么也解决不掉
  19. 网页浏览 infinite scroll效果知识
  20. sqlserver分组统计合并

热门文章

  1. java enum(枚举)使用详解 + 总结
  2. 360safe安全卫士防网站攻击源码
  3. WinForm 窗体属性
  4. JAVA中保留指定小数位方法
  5. 数据结构 C++ 单链表 一元多项式的相加
  6. MySQL数据恢复和复制对InnoDB锁机制的影响
  7. velocityjs 动画库 比jquery默认的animate强
  8. 20145334赵文豪 《Java程序设计》第1周学习总结
  9. Linux下一些有用的指令
  10. BizTalk开发系列(三十五) TCP/IP 适配器