Balls Rearrangement

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

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.

思路:我们可以发现循环节为a,b的最小公倍数tmp,所以我们只要求从1到tmp的花费即可,易知a和b的大小关系对题目答案没有影响,不妨设a>b,设从1到tmp的花费依次为 w1,w2,w3,....wtmp,我们可以将这tmp个值分成每b个一组,一共a/gcd组(gcd为a和b的最大公约数),我们设num[i]为第i组的和,现在的关键问题是num[i]怎么求,对于每一组num[i],这时第一个数在a盒子中的第po个,那么它的花费就是po-1(因为这个数一定是在b盒子的第一个),那么对于后面的第2个,第三个。。。。。。的花费也将是po-1,直到走到第po+b-1个,或者到第a个盒子(其实就是a和po+b-1去个小),若走到了第po+b-1个盒子,则这一组已经算完,为b*(po-1)花费,且po将被更新为po+b,否则,还应该计算剩下来的花费,这个和前一半类似,只不过这是a盒子从1开始,b盒子从a-po+2开始而已,这也是一串相同的花费,可以O(1)求出,然后更新po。将所有组求完后,剩下的工作就简单了。这里不再罗嗦。

以下是代码。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define ll long long
using namespace std;
ll dp[100010],sum[100010];
int Po[100010];
long long GCD(long long a,long long b)
{
if(b==0)
return a;
return GCD(b,a%b);
}
ll getans(ll x)
{
if(x>0)
return x;
return -x;
}
int main()
{
//freopen("dd.txt","r",stdin);
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
ll n,a,b;
cin>>n>>a>>b;
if(a<b)
swap(a,b);
if(a==b)
printf("0\n");
else
{
ll tmp=GCD(a,b);
ll tt=tmp;
tmp=a*b/tmp;
dp[1]=0;
sum[1]=0;
ll po=b+1;
Po[1]=1;
Po[2]=b+1;
for(int i=2;i<=a/tt;i++)
{
if(po+b-1<=a)
{
dp[i]=(po-1)*b;
po=po+b;
if(po>a)
po-=a;
}
else
{
dp[i]=(po-1)*(a-po+1);
int tt=a-po+2;
dp[i]+=(tt-1)*(b-tt+1);
po=b-tt+2;
if(po>a)
po-=a;
}
Po[i+1]=po;
sum[i]=sum[i-1]+dp[i];
} ll ans=0;
ans=sum[a/tt]*(n/tmp);
n%=tmp;
// cout<<n<<endl;
ll x=n/b;
ans+=sum[x];
n%=b;
if(n)
{
ll PO=Po[x+1];
for(int i=1;i<=n;i++)
{
ans+=getans(PO-i);
PO++;
if(PO>a)
PO-=a;
}
}
cout<<ans<<endl;
} }
return 0;
}

2013 多校联合 2 A Balls Rearrangement (hdu 4611)的更多相关文章

  1. 2013 多校联合 F Magic Ball Game (hdu 4605)

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...

  2. 2013多校联合2 I Warm up 2(hdu 4619)

    Warm up 2 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

  3. 2013 多校联合2 D Vases and Flowers (hdu 4614)

    Vases and Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others ...

  4. 2013多校联合3 G The Unsolvable Problem(hdu 4627)

    2013-07-30 20:35 388人阅读 评论(0) 收藏 举报 http://acm.hdu.edu.cn/showproblem.php?pid=4627 The Unsolvable Pr ...

  5. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  6. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  7. 2017 ACM暑期多校联合训练 - Team 9 1008 HDU 6168 Numbers (模拟)

    题目链接 Problem Description zk has n numbers a1,a2,...,an. For each (i,j) satisfying 1≤i<j≤n, zk gen ...

  8. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  9. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

随机推荐

  1. 使用Canvas基于手势可以使树秋千

    用Canvas制作能够依据手势摆动的树 依据工作的须要.制作一个摆动的树做为页面的背景.为了添加页面的交互性,我又为背景中的树添加了鼠标(触控)事件,使他可以依据鼠标(触控)做出对应的动作,当手指做上 ...

  2. SOA面向服务架构

    SOA面向服务架构 风尘浪子 只要肯努力,梦想总有一天会实现 随笔分类 - SOA面向服务架构 结合领域驱动设计的SOA分布式软件架构 摘要: 领域驱动设计DDD的总体结构,Repository层使用 ...

  3. 纯CSS3打造七巧板

    原文:纯CSS3打造七巧板 最近项目上要制作一个七巧板,脑子里瞬间闪现,什么...七巧板不是小时候玩的吗... 七巧板的由来 先来个科普吧,是我在查资料过程中看到的,感觉很有意思. 宋朝有个叫黄伯思的 ...

  4. 使用Python改写的身份证信息查询小程序

    花了几天时间过了一遍python基础.真心感觉python让世界充满了爱…先简单的使用一下python好了,拿以前写的<C语言身份证信息查询系统(修改版)>开刀~ 很多东西,不需要考虑C语 ...

  5. 异常:必须先将 ContentLength 字节写入请求流,然后再调用 [Begin]

    异常描述 异常:必须先将 ContentLength 字节写入请求流,然后再调用 [Begin] 解决方案 //解决异常:必须先将 ContentLength 字节写入请求流,然后再调用 [Begin ...

  6. 【转】仿QQ5.0侧滑菜单ResideMenu

    本文由孙国威 原创.如需转载,请注明出处! 原文:http://blog.csdn.net/manoel/article/details/39013095 为了后续对这个项目进行优化,比如透明度动画. ...

  7. .NET MVC4 实训记录之五(访问自定义资源文件)

    .Net平台下工作好几年了,资源文件么,大多数使用的是.resx文件.它是个好东西,很容易上手,工作效率高,性能稳定.使用.resx文件,会在编译期动态生成已文件名命名的静态类,因此它的访问速度当然是 ...

  8. FileWriter字符输出流和FileReader字符输出流

    //FileWriter public class FileWriterDemo { //字符流:适用于文本文件,以字符为单位进行操作,经常和缓冲流一起使用 /**  * 字符流操作步骤:  * 1. ...

  9. Varnish缓存服务详解及应用实现

    1.varnish的基本介绍    Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多.在1975年时,储存媒介只有 ...

  10. 路由模块router实现step1

    hashchange事件 参考:http://www.cnblogs.com/rubylouvre/archive/2012/10/24/2730599.html 需要解决的问题: 1.IE6/7及兼 ...