hdu4611 Balls Rearrangement
来源:互联网 发布:中骏集团怎么样知乎 编辑:程序博客网 时间:2024/06/11 08:37
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.
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).
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
31000000000 1 18 2 411 5 3
Sample Output
0816
Source
2013 Multi-University Training Contest 2
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
- hdu4611 Balls Rearrangement
- hdu4611 Balls Rearrangement
- hdu4611 Balls Rearrangement
- hdu4611 Balls Rearrangement,模拟
- Balls Rearrangement(hdu4611,模拟+数学)
- hdu4611 Balls Rearrangement(推公式)
- Balls Rearrangement
- Balls Rearrangement
- hdu 4611 Balls Rearrangement
- hdu 4611 Balls Rearrangement
- hdu-4611-Balls Rearrangement
- HDU 4611 Balls Rearrangement
- HDU 4611 Balls Rearrangement
- hdu-4611-Balls Rearrangement
- hdu 4611 Balls Rearrangement
- hdu 4710 Balls Rearrangement
- 4611 hdu Balls Rearrangement
- 学习笔记
- php接口和异常处理
- redis学习笔记五之pipeline
- 鲍尔默在微软最大的遗憾?
- 大师的blog
- hdu4611 Balls Rearrangement
- Java 二叉树排序
- 迟来的毕业答辩总结
- redis学习笔记六之发布订阅
- Linux内核空间-用户空间通信之debugfs
- redis学习笔记七之持久化
- 标准的MySQL数据库SQL语句语法
- Fragment例子
- 多线程并发实践_笔记_第六章