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 ...
随机推荐
- 字节流复制mp3文件(带缓冲区)
//自定义的缓冲区 import java.io.*; class MyBufferedInputStream{ private byte[] buf = new byte[1024]; pr ...
- eclipse(MyEclipse)插件之aptana安装
1.在MyEclipse安装目录下创建文件夹, aptana2.在aptana文件夹下创建文件夹eclipse3.将aptana_update_024747.zip中的文件解压缩到aptana\ecl ...
- BULK SQL
DECLARE TYPE TY_EMP IS TABLE OF EMP%ROWTYPE; --如果是IS TABLE OF行类型(ROWTYPE.RECORD等)就是二维 V_Emp TY_EMP; ...
- poj 2661 Factstone Benchmark
/** 大意: 求m!用2进制表示有多少位 m! = 2^n 两边同时取对数 log2(m!) = n 即 log2(1) + log2(2)+log2(3)+log2(4)...+log2(m) = ...
- NSString 用法大全。
一.NSString 创建字符串. NSString *astring = @"This is a String!"; 创建空字符串,给予赋值. NSString *astri ...
- 在Github上搭建你的博客
title: blog on github date: 2014-03-24 20:29:47 tags: [blog,github,hexo] --- **用Github写博文** 参考http:/ ...
- Swap file ".Podfile.swp" already exists!
解决Swap file ".ceshi.c.swp" already exists!问题 关于swp文件:使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一 ...
- 【Oracle】SQL*Loader-522: lfiopn failed for file
Linux下使用sqlldr进行批量操作,此操作会自动生成和删除临时文件. 因此,当前操作的用户必须具备对存放文件的文件夹有增删改的权限. 使用root登录,修改改文件夹权限为777即可. chmod ...
- iOS 判断有无网络连接
众所周知,我们在开发APP时,涉及网络连接的时候,都会想着提前判断一下当前的网络连接状态,如果没有网络,就不再请求url,省去不必要的步骤,所以,这个如何判断?其实很简单. 前提:工程添加:Syste ...
- Git简介及安装和简单配置
首先需要清楚的是Git和GitHub的区别. Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了 ...