HDU 4611 Balls Rearrangement
来源:互联网 发布:各国人看中国知乎 编辑:程序博客网 时间:2024/05/22 12:43
Balls Rearrangement
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1213 Accepted Submission(s): 471
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
题意:有3个数字 N, a, b; 求 abs(n % a - n % b) 的和 n是从0~ N - 1
思路: 模拟
样例 11 5 3
不能一个个的算, 要跳着算。 同是递增的差值一样。
是对称的。 有点想算 (1 + 2 + 3 + …… + k) * 2 >= n; 即 (1 + k) * k >= n; k = sqrt(n);
复杂度粗略算得为 O(sqrt(n)); 不会超时。
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int V = 200000 + 5;const int MaxN = 100000 + 5;const int mod = 1000000000 + 7;int T;__int64 n, a, b;__int64 LCM(__int64 a, __int64 b) { __int64 aa = max(a, b); __int64 bb = min(a, b); do { __int64 r = aa % bb; aa = bb; bb = r; }while(bb); return a * b / aa;}__int64 f(__int64 n, __int64 a, __int64 b) { __int64 a_mod, b_mod, start, ans; ans = start = a_mod = b_mod = 0; while(start < n) { __int64 len = min(a - a_mod, b - b_mod); if(len > n - start) len = n - start; ans += len * abs(b_mod - a_mod); a_mod = (a_mod + len) % a; b_mod = (b_mod + len) % b; start += len; } return ans;}int main() { scanf("%d", &T); while(T--) { __int64 ans = 0; scanf("%I64d%I64d%I64d", &n, &a, &b); __int64 lcm = LCM(a, b); if(lcm >= n) //如果最小公倍数超过 n 直接算n次; ans = f(n, a, b); else //否则算公倍数以内的 * 倍数 再加上多出的 ans = f(lcm, a, b) * (n / lcm) + f(n % lcm, a, b); printf("%I64d\n", ans); }}
- 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 4611Balls Rearrangement(思维)
- hdu - 4710/4611 - Balls Rearrangement
- [hdu 4611]Balls Rearrangement 数论
- HDU 4611 Balls Rearrangement 解题报告
- hdu 4611/4710 Balls Rearrangement 数论
- hdu 4611 Balls Rearrangement(数学:推理+模拟)
- hdu 4611 Balls Rearrangement(规律)
- 利用预渲染加速iOS设备的图像显示
- 自定义 UIPopoverController
- Android 客户端与服务器交互方式
- hdu4414(DFS 找十字架数量)
- 注重学习方法
- HDU 4611 Balls Rearrangement
- c语言万年历(源代码)
- 黑马程序员_IO流2_(字符缓冲区,字节缓冲区,字节字符转换流)
- hdu1412(水题 用一下链表做){A} + {B}.
- c语言计算器(源代码)
- NYOJ 42-一笔画问题:欧拉路径
- 圈子圈套()
- hdu1896(模拟+优先级队列)
- 在表头插入生成单链表