您的位置:首页 > 其它

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

2015-04-29 22:11 537 查看
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

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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).

[align=left]Output[/align]
For each test case, output the total cost.

[align=left]Sample Input[/align]

3
1000000000 1 1
8 2 4
11 5 3

[align=left]Sample Output[/align]

0
8
16

[align=left]Source[/align]
2013 ACM/ICPC Asia Regional Online —— Warmup

分析:

模拟,每次增加 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=0,step=1,i;
for(i=0;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 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: