Balls Rearrangement

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1661    Accepted Submission(s): 627

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
 

             
  题目大意:刚开始比赛的时候,一看见这题,以为是水题。是自己想简单了。题目是这样:给你n个球编号0~n-1,先放进a个箱子里,i mod a =p,第i个球放在箱子p里面,就是同模的放在一起。然后再把这些球移到b个箱子里面,同样的法则。sum值等于每个球从i号箱子移到j号箱子的数目总和。一个球的sum=abs(i-j).

          解题思路:开始把他想简单了,这些球都是有唯一的标识的。后来想清楚了之后,发现循环节是mi(n,lcm(a,b))但是直接用循环节统计会TLE。如果a取9999,b取9997,n=10^9那么循环节直接是10^9。肯定会TLE。然后又开始找规律,突然发现可以用两个指针不断地移动。不用每次都是++,自己可以在纸上画一下。
0 1 2 3 4     0 1 2
5 6 7 8 9 3 4 5
10 6 7 8
9 10

如果到6的时候,左边一行可以到9,右边一行可以到8.我们选取小的。会发现6~8之间abs(i-j)是一样的。同理0可以直接跳到3那里。主要就是用到这个思想。详见代码。当时因为没有用__int64Wa了好半天!!!

          题目地址:Balls Rearrangement

AC代码:
0 1 2 3 4     0 1 2
5 6 7 8 9 3 4 5
10 6 7 8
9 10 #include<iostream>
#include<cstdio>
using namespace std; __int64 f(__int64 x)
{
if(x<0)
x=-x;
return x;
}
__int64 gcd(__int64 m,__int64 n)
{
__int64 t;
while(n)
{
t=m%n; m=n; n=t;
}
return m;
} __int64 min1(__int64 x1,__int64 x2)
{
if(x1<x2) return x1;
else return x2;
} int main()
{
__int64 mi,p,a,b,i,j,t1,t2,cnt,sum,tes;
__int64 lcm;
scanf("%I64d",&tes);
while(tes--)
{
scanf("%I64d%I64d%I64d",&p,&a,&b);
if(a==b)
{
puts("0");
continue;
}
lcm=a/gcd(a,b)*b; //a,b的最小公倍数
mi=p;
if(mi>lcm)
mi=lcm; //mi是循环节
t1=p/mi,t2=p%mi;
cnt=0,i=0,j=0;
sum=0; __int64 mi2;
while(cnt<mi) //一个循环内的
{
if(i>=a) i-=a;
if(j>=b) j-=b;
mi2=min1(a-i,b-j);
if(mi2>mi-cnt) //这点需要注意,不能超过步数
mi2=mi-cnt;
sum+=f(i-j)*mi2;
i+=mi2; j+=mi2; cnt+=mi2;
}
sum=sum*t1;
cnt=0,i=0,j=0;
while(cnt<t2) //循环节以外的
{
if(i>=a) i-=a;
if(j>=b) j-=b;
mi2=min1(a-i,b-j);
if(mi2>t2-cnt)
mi2=t2-cnt;
sum+=f(i-j)*mi2;
i+=mi2; j+=mi2; cnt+=mi2;
}
printf("%I64d\n",sum);
}
return 0;
} /*
23
1000000000 1 1
8 2 4
11 5 3
1000000000 9999 9997
*/

HDU 4611Balls Rearrangement(思维)的更多相关文章

  1. HDU 5776 sum (思维题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 题目让你求是否有区间的和是m的倍数. 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续 ...

  2. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  3. Binary Tree HDU - 5573 (思维)

    题目链接: B - Binary Tree  HDU - 5573 题目大意: 给定一颗二叉树,根结点权值为1,左孩子权值是父节点的两倍,右孩子是两倍+1: 给定 n 和 k,让你找一条从根结点走到第 ...

  4. HDU 5178 pairs —— 思维 + 二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5178 pairs Time Limit: 2000/1000 MS (Java/Others)     ...

  5. ACM-ICPC 2016 大连赛区现场赛 K. Guess the number && HDU 5981(思维+DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5981 题意:A在[L, R]之间随机选取一个数X,之后B来猜这个数,如果猜的数比X小,那么A就告诉B猜 ...

  6. ACM-ICPC 2017 沈阳赛区现场赛 M. Wandering Robots && HDU 6229(思维+期望)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6229 参考题解:https://blog.csdn.net/lifelikes/article/det ...

  7. CCPC 2017 哈尔滨 D. X-Men && HDU 6233(思维+期望)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6233 题意:一个树上有m个人,每个人在某个节点上,每个时刻每个人可以和一个与他距离大于 1 的点进行交 ...

  8. Cube HDU - 1220(思维)

    Cowl is good at solving math problems. One day a friend asked him such a question: You are given a c ...

  9. HDU - 4811 - Ball (思维)

    题意: 给出一定数量的三种颜色的球,计算如何摆放得到值最大(有一定顺序) 有三种摆放方法 1.如果放的是第一个(桌子上原来没有),数值不变 2.如果在末尾追加一个,那么增加前面不同颜色的个数的值 3. ...

随机推荐

  1. [深入JUnit] 为什么别测试private函数

    [深入JUnit] 为什么别测试private函数 摘自http://www.tuicool.com/articles/iumaayJ 时间 2016-03-28 10:58:03 SegmentFa ...

  2. 今天弄了整整一天DCloud

    开发一个新闻移动客户端 起先用appcan.apiCloud,最后选择了DCloud: MUI文档看了n久,js代码一上来不很适应编码风格,一堆括号弄得头大: 数据库由爬虫程序将新闻页面数据爬出来: ...

  3. alert 在手机浏览器会显示网址,怎么能去掉这个网址

    之前就看到有人发过这帖子,现在自己也遇到这问题了. 目前想到的一个解决方案,是用jquery的模拟的alert插件进行代替,可是找的几个插件都不能实现alert的阻塞功能.怎么破 ,具体解决方案如下: ...

  4. Oracle 中按条件过滤重复记录

    在数据处理中,经常会遇到类似这样的情况:数据库中存在多条记录,其中某些字段值相同,其他字段值不同.实际的业务需要针对这样的情况,只保留一条数据,其他数据删除.如何做到呢?在sql中有top关键字相对容 ...

  5. IOS的一个带动画的多项选择的控件(一)

    先上效果图: 这个程序分2个层次,一个是顶部的带UITextField的bar,一个是下拉选择的view,下拉选择的view带有4个自己定义的UIView 我们先定义一个UIViewControlle ...

  6. RMAN常用备份恢复命令汇总

    RMAN命令 1.独立命令  RMAN>shutdown immediate  RMAN>startup  RMAN>backup format 'd:\backup\%d_%s.b ...

  7. 学习iOS必须知道的[转载]

    part1 : http://www.cocoachina.com/ios/20150608/12052.html part2 : http://www.cocoachina.com/ios/2015 ...

  8. (转) [老老实实学WCF] 第三篇 在IIS中寄存服务

    第三篇 在IIS中寄宿服务 通过前两篇的学习,我们了解了如何搭建一个最简单的WCF通信模型,包括定义和实现服务协定.配置服务.寄宿服务.通过添加服务引用的方式配置客户端并访问服务.我们对WCF的编程生 ...

  9. jmeter初识

    Jmeter是一款开源的性能测试工具,基于协议的方式生成脚本进行负载,模仿几十或上百的用户访问程序,相比Loadrunner,Jmeter只有几十个M,Jmeter需要jdk环境的配置,计数器和报告没 ...

  10. DTO学习系列之AutoMapper(六)----EntityFramework和AutoMapper的婚后生活

    写在前面 我到底是什么? 越界的可怕 做好自己 后记 文章标题主要关键字:mapping DTOs to Entities,注意并不是“Entities to DTOs”,表示实体对象到DTO的转换, ...