博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu4710
阅读量:7043 次
发布时间:2019-06-28

本文共 2090 字,大约阅读时间需要 6 分钟。

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但是per很大 即使一段段的算也可能超时,所以一直不敢下手,一直在找寻更简单的推论。。结果一直没找到下次碰到这种情况应该先试试,不能找不出别的更简单的方法就连自己想到的方法都不试试现在认真分析发现时间复杂度好像是:O((a/b * min(per,n)/a));//假设a>=b */#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 99999999using 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
num && i
per)sum=(n/per)*sum+p;//p表示前n%k个i%a-i%b的和 printf("%I64d\n",sum); } return 0;}

转载地址:http://nwhal.baihongyu.com/

你可能感兴趣的文章
领域驱动设计(DDD)的实践经验分享之持久化透明
查看>>
[Erlang 0007] Erlang ETS Table 二三事
查看>>
12.14. Switch Config Example
查看>>
Spring MVC视图解析器:配置多个视图解析器的优先级
查看>>
groovy-运算符
查看>>
VMware 镜像文件下载 CentOS + Debian + Ubuntu + Fedora
查看>>
ECHO.js 纯javascript轻量级延迟加载
查看>>
第 3 章 Networking
查看>>
24.4. REGEXP
查看>>
通过指定函数/方法形参类型提高PHP代码可靠性
查看>>
机器理解大数据秘密:聚类算法深度剖析
查看>>
[Erlang 0123] Erlang EPMD
查看>>
超级简单:使用Visual Studio自动产生存储过程
查看>>
[Tex学习笔记]矩阵输入中的省略号
查看>>
[数学杂志]AML
查看>>
[唐诗]187春思-李白
查看>>
.NET设计模式实例之单例模式( Singleton Pattern)
查看>>
JAVA学习中Swing概述中的JFrame学习
查看>>
帝国cms无法注册登录快速解决方法 附路径
查看>>
谈谈一些有趣的CSS题目(十一)-- reset.css 知多少?
查看>>