Balls Rearrangement

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 945    Accepted Submission(s): 380


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

题意:有n个球,编号为0~n-1,有a个盒子,编号为0~a-1,每一个球放在第x%a(0<=x<=n-1)个盒子里,现在有b个盒子,每一个球要重新放到x%b个盒子内,如果编号相同则不用移动,如果编号不同,那么每一次移动的价值为abs(x%a-x%b),问总价值是多少。

思路:首先容易发现,循环节最大为lcm(a,b),即答案是n/p*jisuan(a,b,p)+jisuan(a,b,n%p),但是我们会发现,如果a,b是接近100000的两个素数,那么我们光是从0~lcm(a,b)做一遍会超时,所以要用别的方法。模拟几个样例后会发现,从x%a=0或者x%b=0到下一个x%a=0或者x%b=0这一段区间内,所有数从a盒子搬到b盒子产生的价值是一样的,所以我们可以"跳着"暴力,然后就不会超时了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define lson th<<1
#define rson th<<1|1
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0) int gcd(int a,int b){
return b ? gcd(b,a%b) : a;
} ll lcm(int a,int b){
return (ll)a*(ll)b/gcd(a,b);
}
ll jisuan(ll a,ll b,ll p)
{ ll t,x=0,y=0,c=0;
ll ans=0;
while(c<p)
{
t=min(a-x,b-y);
if(c+t>=p){
t=p-c;
}
ans+=(ll)t*abs(x-y);
c+=t;
x=(x+t)%a;
y=(y+t)%b;
}
return ans;
} int main()
{
int m,i,j,T;
ll n,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld",&n,&a,&b);
ll p=lcm(a,b);
printf("%lld\n",(ll)n/p*(ll)jisuan(a,b,p)+jisuan(a,b,n%p) );
}
return 0;
}

hdu4710 Balls Rearrangement(数学公式+取模)的更多相关文章

  1. [hdu4710 Balls Rearrangement]分段统计

    题意:求∑|i%a-i%b|,0≤i<n 思路:复杂度分析比较重要,不细想还真不知道这样一段段跳还真的挺快的=.= 令p=lcm(a,b),那么p就是|i%a-i%b|的循环节.考虑计算n的答案 ...

  2. Gym100947E || codeforces 559c 组合数取模

    E - Qwerty78 Trip Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  3. C语言fmod()函数:对浮点数取模(求余)

    头文件:#include <math.h> fmod() 用来对浮点数进行取模(求余),其原型为:    double fmod (double x); 设返回值为 ret,那么 x = ...

  4. 除法取模练习(51nod 1119 & 1013 )

    题目:1119 机器人走方格 V2 思路:求C(m+n-2,n-1) % 10^9 +7       (2<=m,n<= 1000000) 在求组合数时,一般都通过双重for循环c[i][ ...

  5. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  6. 【HDU 5832】A water problem(大数取模)

    1千万长度的数对73和137取模.(两个数有点像,不要写错了) 效率要高的话,每15位取一次模,因为取模后可能有3位,因此用ll就最多15位取一次. 一位一位取模也可以,但是比较慢,取模运算是个耗时的 ...

  7. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  8. 【转】C语言快速幂取模算法小结

    (转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...

  9. hdu2302(枚举,大数取模)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2303 题意:给出两个数k, l(4<= k <= 1e100, 2<=l<=1 ...

随机推荐

  1. 在MongoDB中执行查询与创建索引

    实验目的: (1)掌握MongoDB中数据查询的方法: (2)掌握MongoDB中索引及其创建: 实验内容: 一. MongoDB中数据查询的方法: (1)find函数的使用: (2)条件操作符: a ...

  2. python中列表的insert和append的效率对比

    python中insert和append方法都可以向列表中插入数据只不过append默认插入列表的末尾,insert可以指定位置插入元素. 我们来测试一下他俩插入数据的效率: 测试同时对一个列表进行插 ...

  3. Can't locate Time/HiRes.pm in @INC (@INC contains

    Can't locate Time/HiRes.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/ ...

  4. iostat的输出

    第一行显示的时子系统启动以来的平均值,接下来的报告显示了增量的平均值,每个设备一行 Device:         rrqm/s   wrqm/s     r/s     w/s   rsec/s   ...

  5. 鸿蒙的fetch请求加载聚合数据的前期准备工作-手动配置网络权限

    目录: 1.双击打开"config.json"文件 2.找到配置网络访问权限位置1 3.配置内容1 4.默认访问内容是空的 5.添加配置内容2 6.复制需要配置的网络二级URL 7 ...

  6. SparkStreaming和Kafka基于Direct Approach如何管理offset实现exactly once

    在之前的文章<解析SparkStreaming和Kafka集成的两种方式>中已详细介绍SparkStreaming和Kafka集成主要有Receiver based Approach和Di ...

  7. PW6513高压40V的LDO芯片,SOT89封装

    一般说明 PW6513系列是一款高精度,高输入电压,低静态电流,高速,低具有高纹波抑制的线性稳压器.输入电压高达40V,负载电流为在电压=5V和VIN=7V时高达300mA.该设备采用BCD工艺制造. ...

  8. 网络编程-I/O复用

    I/O模型 Unix下可用的I/O模型有五种: 阻塞式I/O 非阻塞式I/O I/O复用(select和poll.epoll) 信号驱动式I/O(SIGIO) 异步I/O(POSIX的aio_系列函数 ...

  9. 糊糊的学习笔记--Fiddle抓包

    Fiddle简述 Fiddler是一个http调试代理,它能 够记录所有的你电脑和互联网之间的http通讯,Fiddler 可以也可以让你检查所有的http通讯,设置断点,以及Fiddle 所有的&q ...

  10. navicat premium 11.0.17 破解版

    下载地址: 链接:https://pan.baidu.com/s/1zBoKRAaQZb2p2weelJpKMQ       提取码:b8dd 一款功能强大的数据库管理工具Navicat Premiu ...