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

最新文章

  1. 将一个字符串中的大写字母转换成小写字母,小写字母转换成大写字母(java)
  2. C和指针 第八章 数组
  3. Bzoj1189 [HNOI2007]紧急疏散evacuate
  4. Linux更改服务器Hostname
  5. mongodb初步使用
  6. 控制不能离开Finally子句主体
  7. MapReduce实战:查找相同字母组成的单词
  8. Unity NGUI 网络斗地主 -界面制作
  9. UIStepper UISlider UISwitch UITextField 基本控件
  10. jQuery语音播放插件
  11. (转)crontab安装(command not found)
  12. shell脚本练习题
  13. DateTime格式
  14. 常用算法和Demo(Java实现)(持续更新)
  15. js引用值传递改变问题(使用深拷贝)
  16. 【转】探讨:ASP.NET技术的学习顺序问题
  17. C#数组,ArrayList,List
  18. mvc未登录跳转到登录界面
  19. 深度学习原理与框架-神经网络-线性回归与神经网络的效果对比 1.np.c_[将数据进行合并] 2.np.linspace(将数据拆成n等分) 3.np.meshgrid(将一维数据表示为二维的维度) 4.plt.contourf(画出等高线图,画算法边界)
  20. vue按需引入echarts

热门文章

  1. Ember.js - About
  2. SQL中on条件与where条件的区别(转载)
  3. 【FAQ】SpingMVC实现集合參数(Could not instantiate bean class [java.util.List])
  4. Java ArrayList add(int index, E element) example
  5. 细节!重点!易错点!--面试java基础篇(二)
  6. Writing a Windows Shell Extension(marco cantu的博客)
  7. 14.10.3 InnoDB Checkpoints InnoDB 检查点:
  8. 《学习opencv》笔记——矩阵和图像操作——cvSetIdentity,cvSolve,cvSplit,cvSub,cvSubS and cvSubRS
  9. GDI 总结三: CImage类使用
  10. android 细节之 旋转动画