hdu4710
Balls Rearrangement
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 344 Accepted Submission(s): 165
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.
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).
1000000000 1 1
8 2 4
11 5 3
8
16
/*分析:对于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的更多相关文章
- [hdu4710 Balls Rearrangement]分段统计
题意:求∑|i%a-i%b|,0≤i<n 思路:复杂度分析比较重要,不细想还真不知道这样一段段跳还真的挺快的=.= 令p=lcm(a,b),那么p就是|i%a-i%b|的循环节.考虑计算n的答案 ...
- hdu4710 Balls Rearrangement(数学公式+取模)
Balls Rearrangement Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
随机推荐
- 在GridView控件里面绑定DropDownList控件
参考链接: http://www.aspsnippets.com/Articles/Populate-DropDownList-with-Selected-Value-in-EditItemTempl ...
- STL之queue(单向队列)
单向队列中的数据是先进先出(First In First Out,FIFO).单向队列一共6个常用函数(front().back().push().pop().empty().size()) #inc ...
- HTML5 DTD
HTML5/HTML 4.01/XHTML 元素和有效的 DTD 下面的表格列出了所有的 HTML5/HTML 4.01/XHTML 元素,以及它们会出现在什么文档类型 (DTD) 中: 标签 HTM ...
- ThinkPHP第四天(U函数,URL类型参数配置,伪静态后缀名配置,数据传递与获取$_GET等)
1.U('地址','参数','伪静态','是否跳转','是否显示域名'); 在模板中使用U方法而不是固定写死URL地址的好处在于,一旦你的环境变化或者参数设置改变,你不需要更改模板中的任何代码. 在模 ...
- 面向对象程序设计-C++ Finial exam review NOTES【第十六次上课笔记】
写在前面: 我记得也不全,如果有记录的更全的同学可以留言,我会添加哒 :) 常量 内敛函数 为什么需要内敛函数 内敛函数适用于什么场合 内敛函数本身,最大优点是,避免了真正函数调用的开销 因为普通函数 ...
- 转:JavaScript定时机制、以及浏览器渲染机制 浅谈
昨晚,朋友拿了一道题问我: a.onclick = function(){ setTimeout(function() { //do something ... },0); }; //~~~ 我只知道 ...
- VS QT 配置OpenGL
在visual studio 下编译OpenGL代码出现以下错误,原因是vs没有自带opengl库,需要自己引入 无法解析的外部符号 __imp__glClear@4 无法解析的外部符号 __imp_ ...
- hibernate的配置 1
hibernate 是一种ORM框架,是ORM框架中一个典范 ORM叫做对象关系映射 是面向对象语言和关系型数据库之间的映射关系 所以只有在面向对象语言或者关系型数据库没用的时候ORM才会消失 ORM ...
- 数据切分——Mysql分区表的管理与维护
关于Mysql分区表的介绍可以参考: http://blog.csdn.net/jhq0113/article/details/44592865 关于Mysql分区表的创建可以参考: http://b ...
- uva - The Lottery(容斥,好题)
10325 - The Lottery The Sports Association of Bangladesh is in great problem with their latest lotte ...