hdu 4611 Balls Rearrangement (找循环节)
来源:互联网 发布:知敬畏守规矩 申论 编辑:程序博客网 时间:2024/06/08 04:59
Balls Rearrangement
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1700 Accepted Submission(s): 643
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
思路:
将0~N-1分别写成两个矩阵的形式其实就有思路了,每次充满两矩阵中的其中一列,因为初始间隔一定,每走一步或每走多步的相对间隔仍然没变,所以可以一次统计出来(即每次用i+=step代替i++)。这样还有一种情况不能处理,就是两个矩形的宽都很小(即n很大,a、b很小),所以想到找循环节,循环节很容易找即lcm(a,b)。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;typedef long long ll;ll n,m,a,b,ans,lcm;ll gcd(ll x,ll y){ ll r=x%y; if(r==0) return y; return gcd(y,r);}void solve(){ ll i,j,r,mod,sum,t; ll p1,p2,step,mi; r=n/lcm; mod=n%lcm; p1=p2=0; sum=t=0; for(i=0;i<lcm;i+=step) { step=min(a-p1,b-p2); // 充满矩形的一列需要走的步数 if(i<mod&&i+step+1>=mod) { t=sum+(mod-i)*fabs(p1*1.0-p2); } sum+=step*fabs(p1*1.0-p2); p1=(p1+step)%a; // 更新位置指针 p2=(p2+step)%b; } ans=r*sum+t;}int main(){ ll i,j,t,c; scanf("%I64d",&t); while(t--) { scanf("%I64d%I64d%I64d",&n,&a,&b); c=gcd(a,b); lcm=a/c*b; solve(); printf("%I64d\n",ans); } return 0;}
- hdu 4611 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 4611 Balls Rearrangement
- hdu-4611-Balls Rearrangement
- hdu 4611 Balls Rearrangement
- 4611 hdu Balls Rearrangement
- HDU 4611 Balls Rearrangement
- HDU 4611 Balls Rearrangement
- hdu 4611Balls Rearrangement
- hdu 4611 Balls Rearrangement(规律)
- HDU 4611 Balls Rearrangement(规律)
- 提高你的Java代码质量吧:不同的列表应该选择不同的遍历方法
- 核心标签库<c:set>标签、<c:choose>标签、<c:forEach>标签&<c:if>标签的使用例子
- Android dip是什么
- 【每日一摩斯】-LGWR Is Generating Trace file with "Warning: Log Write Time 540ms, Size 5444kb" In 10.2.0.4
- Source Insight快捷键及使用技巧
- hdu 4611 Balls Rearrangement (找循环节)
- iOS开发之二维码的应用
- uva 10020 Minimal coverage(贪心-最小覆盖问题)
- 图表插件-15款经典图表插件软件推荐 创建最漂亮的图表
- Codeforces Round #147 (Div. 2) / 237B Young Table (搜索)
- 周总结
- gsoap实现webservice如何发送结构体数组
- 数组元素的比较
- 8种排序算法--折半插入算法