Balls Rearrangement

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 344    Accepted Submission(s): 165

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
/*分析:对于i%a - i%b,每次加上从i开始和这个值(i%a - i%b)相等的一段,
这样i就不是每次+1,而是每次加上一段,如果碰到n大于a,b的最小公倍数,
则只需要计算a,b最小公倍数长度的总和,然后sum*=n/per + p;//p表示前i个数,p=n%per; 本题反思:刚开始自己就是这样想,但是想到a,b的最小公倍数可能很大,而且n也很大,
如果刚好碰到n<per但是n很大;//per表示a,b最小公倍数,或者碰到n>per但是per很大
即使一段段的算也可能超时,所以一直不敢下手,一直在找寻更简单的推论。。结果一直没找到
下次碰到这种情况应该先试试,不能找不出别的更简单的方法就连自己想到的方法都不试试 现在认真分析发现时间复杂度好像是:O((a/b * min(per,n)/a));//假设a>=b
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#include<math.h>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=10;
__int64 p; __int64 Gcd(__int64 a,__int64 b){
if(b == 0)return a;
return Gcd(b,a%b);
} __int64 calculate(__int64 n,__int64 a,__int64 b,__int64 num){
p=0;
__int64 la=a,lb=b,sum=0,l;
for(__int64 i=0;i<n;){
l=min(la,min(lb,n-i));
if(i+l>num && i<num)p=sum+abs((int)(i%a - i%b))*(num-i);
sum+=abs((int)(i%a - i%b))*l;
i+=l;
la=(la-l+a-1)%a+1;
lb=(lb-l+b-1)%b+1;
}
return sum;
} int main(){
__int64 n,a,b,t;
scanf("%I64d",&t);
while(t--){
scanf("%I64d%I64d%I64d",&n,&a,&b);
__int64 gcd=Gcd(a,b),per=a*b/gcd,k=min(per,n);//求出最小公倍数
__int64 sum=calculate(k,a,b,n%k);
if(n>per)sum=(n/per)*sum+p;//p表示前n%k个i%a-i%b的和
printf("%I64d\n",sum);
}
return 0;
}

hdu4710的更多相关文章

  1. [hdu4710 Balls Rearrangement]分段统计

    题意:求∑|i%a-i%b|,0≤i<n 思路:复杂度分析比较重要,不细想还真不知道这样一段段跳还真的挺快的=.= 令p=lcm(a,b),那么p就是|i%a-i%b|的循环节.考虑计算n的答案 ...

  2. hdu4710 Balls Rearrangement(数学公式+取模)

    Balls Rearrangement Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. js div 内容显示分页

    由于工作需要 div固定大小 而内容不定 所以 如果内容过多自然就显示不出来了 所以 需要分页一类的功能下面是代码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  2. codeforces 478B Random Teams

    codeforces   478B  Random Teams  解题报告 题目链接:cm.hust.edu.cn/vjudge/contest/view.action?cid=88890#probl ...

  3. 分蛋糕(C - 二分查找)

    分蛋糕 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/C Description My birthd ...

  4. django开发简易博客(四)

    上一节,我们讲述了怎么使用静态文件,并使用bootstrap对页面进行了美化,这一节我们将增强我们blog的功能,添加发表博客,删除博客的功能. 一.表单的使用 要实现添加blog的功能,就得使用表单 ...

  5. PHP学习笔记13-操作Cookie

    PHP会话管理图: 创建index.php: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/7/1 * ...

  6. python2.7_1.2_打印设备名和IPv4地址

    代码如下: # -*- coding: utf-8 -*- import socket def print_machine_info(): host_name = socket.gethostname ...

  7. Qt 学习 之 二进制文件读写

    在上一章中,我们介绍了有关QFile和QFileInfo两个类的使用.我们提到,QIODevice提供了read().readLine()等基本的操作.同时,Qt 还提供了更高一级的操作:用于二进制的 ...

  8. cocos2dx进阶学习之CCSpriteBatchNode

    继承关系 CCSpriteBatchNode -> CCNode, CCTextureProtocol 成员变量 inline CCTextureAtlas* getTextureAtlas(v ...

  9. CMake学习小结

    假定有vegagis工程,工程的目录结构如下: #--vegagis#  |--src 源文件目录#     |--gui 界面工程,输出类型:dll,依赖于QT的QtCore.QtGui.QtXml ...

  10. PHP 自学之路-----XML编程(Xpath技术,simpleXml技术)基础入门

    XPAth技术 XPath的设计的核心思想,可以通过xpath迅速简介的定位到你希望查找的节点.主要目的是描述节点相对其他节点的位置,可以取得所有符合条件的节点,成为[位置路径]. Xapth主要用来 ...