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.
 

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
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);    }}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 1岁宝宝不喝奶粉怎么办 母乳不够宝宝不喝奶粉怎么办 宝宝吃奶粉上火了怎么办 我小孩不喝奶粉怎么办 2岁宝宝不喝奶粉怎么办 婴儿吃奶粉上火了怎么办 100天的宝宝咳嗽怎么办 40天的小孩咳嗽怎么办 40天的婴儿咳嗽怎么办 50天的婴儿咳嗽怎么办 宝宝20天感冒了怎么办 1个月宝宝咳嗽怎么办 40天的宝宝干咳怎么办 百天的宝宝咳嗽怎么办 50天的孩子咳嗽怎么办 百天宝宝咳嗽有痰怎么办 1岁半宝宝拉肚子怎么办 百天的宝宝拉肚子怎么办 激战2帧数三十多怎么办 太受欢迎了怎么办txt微 太受欢迎了怎么办网盘 太受欢迎了怎么办微盘 我太受欢迎了该怎么办h 我太受欢迎了该怎么办1 卡培他滨副作用怎么办 究极风暴4卡怎么办 脸下垂法令纹深怎么办 20岁有法令纹怎么办 3个月婴儿铅超标怎么办 一岁宝宝铅超标怎么办 小樱死了 佐助怎么办 平板玩游戏闪退怎么办 手机玩游戏闪退怎么办 天天爱消除闪退怎么办 半夜牙疼的厉害怎么办 心眼特别小的人怎么办 自己心眼太小了怎么办 我心眼小爱生气怎么办 公章颜色不红了怎么办 香港逗留超过7天怎么办 酷骑倒闭了押金怎么办