Balls Rearrangement(hdu4611,模拟+数学)
来源:互联网 发布:在线提醒软件 编辑:程序博客网 时间:2024/06/05 08:46
http://acm.hdu.edu.cn/showproblem.php?pid=4611
Balls Rearrangement
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 365 Accepted Submission(s): 129
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
2013 Multi-University Training Contest 2
Recommend
zhuyuanchen520
Statistic | Submit | Discuss | Note
解析:
题意:求sum{abs(i%A - i%B)}(i=0...n-1)
思路请看大神
http://blog.csdn.net/julyana_lin/article/details/7893283
相当于求 abs(i%A - i%B)对i从0~N-1求和
Accepted 4611 140MS 308K 1159 B G++ */自己仿写的:#include<stdio.h>#include<string.h>#include<math.h>#include <iostream>using namespace std;long long gcd(long long a,long long b){return b==0? a:gcd(b,a%b);}long long lcm(long long a,long long b){return a*b/gcd(a,b);}long long solve(long long n,long long a,long long b){long long i,pa,pb,m,ans;ans=i=pa=pb=m=0;while(i<n){if(pa+a>=n&&pb+b>=n){ans+=m*(n-i);break;}else if(pa+a<pb+b){ans+=m*(pa+a-i);pa+=a;i=pa;m=i-pb;}else if(pa+a>pb+b){ans+=m*(pb+b-i);pb+=b;i=pb;m=i-pa;}else{ans+=m*(pa+a-i);pb+=b;pa+=a;i=pb;m=0;}}return ans;}int main(){int T;long long n,a,b,ans;scanf("%d",&T);while(T--){scanf("%I64d%I64d%I64d",&n,&a,&b);long long m=a<b? a:b;if(a==b||n<=m){printf("0\n");continue;}long long temp;if(a<b){temp=a;a=b;b=temp;}long long lc=lcm(a,b);if(lc<=n){ans=solve(n,a,b);printf("%I64d\n",ans);continue;}ans=solve(lc,a,b)*(n/lc)+solve(n%lc,a,b);printf("%I64d\n",ans);} return 0;}
- Balls Rearrangement(hdu4611,模拟+数学)
- hdu4611 Balls Rearrangement,模拟
- hdu4611 Balls Rearrangement(推公式)
- hdu4611 Balls Rearrangement
- hdu4611 Balls Rearrangement
- hdu4611 Balls Rearrangement
- hdu4611 Balls Rearrangement
- hdu 4611 Balls Rearrangement(数学:推理+模拟)
- HDU 4710 Balls Rearrangement (纯数学)
- HDU 4611 Balls Rearrangement (数学-思维逻辑题)
- hdu 4710 Balls Rearrangement (数学思维)
- 数学 之 hdu 4710 Balls Rearrangement
- hdu(4710) Balls Rearrangement
- Balls Rearrangement
- Balls Rearrangement
- HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)
- HDU 4611 Balls Rearrangement 数学 循环节优化
- hdu 4611 Balls Rearrangement(规律)
- Sad Love Story(hdu4631,模拟+set容器)
- 创建一个和已知表相同的表
- 大数据相乘
- 求数组中和最大的子数组与始末下标
- 理解WebKit和Chromium:Chromium资源磁盘缓存
- Balls Rearrangement(hdu4611,模拟+数学)
- 将数组中指定的前N位移动到数组的最后面
- RMAN-03009 & ORA-17628
- 使用gfortran将数据写成Grads格式的代码示例
- 已知一个整数N,求另外一个整数M,使得M本身 + M各个位上的数 = N
- Linux vim 设置
- 用小白鼠鉴别有毒药水问题
- "sqlite3 not found"
- 自写strstr函数