hdu4710 Balls Rearrangement(数学公式+取模)
Balls Rearrangement
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 945 Accepted Submission(s): 380
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
题意:有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(数学公式+取模)的更多相关文章
- [hdu4710 Balls Rearrangement]分段统计
题意:求∑|i%a-i%b|,0≤i<n 思路:复杂度分析比较重要,不细想还真不知道这样一段段跳还真的挺快的=.= 令p=lcm(a,b),那么p就是|i%a-i%b|的循环节.考虑计算n的答案 ...
- Gym100947E || codeforces 559c 组合数取模
E - Qwerty78 Trip Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- C语言fmod()函数:对浮点数取模(求余)
头文件:#include <math.h> fmod() 用来对浮点数进行取模(求余),其原型为: double fmod (double x); 设返回值为 ret,那么 x = ...
- 除法取模练习(51nod 1119 & 1013 )
题目:1119 机器人走方格 V2 思路:求C(m+n-2,n-1) % 10^9 +7 (2<=m,n<= 1000000) 在求组合数时,一般都通过双重for循环c[i][ ...
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
- 【HDU 5832】A water problem(大数取模)
1千万长度的数对73和137取模.(两个数有点像,不要写错了) 效率要高的话,每15位取一次模,因为取模后可能有3位,因此用ll就最多15位取一次. 一位一位取模也可以,但是比较慢,取模运算是个耗时的 ...
- 组合数取模Lucas定理及快速幂取模
组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1) , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...
- 【转】C语言快速幂取模算法小结
(转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...
- hdu2302(枚举,大数取模)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2303 题意:给出两个数k, l(4<= k <= 1e100, 2<=l<=1 ...
随机推荐
- LeetCode394 字符串解码
给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数. 你可 ...
- 【Flutter】可滚动组件之CustomScrollView
前言 CustomScrollView是可以使用Sliver来自定义滚动模型(效果)的组件.它可以包含多种滚动模型,举个例子,假设有一个页面,顶部需要一个GridView,底部需要一个ListView ...
- spring中的工厂模式
spring的bean的创建原理就是框架利用反射创建出实例对象 工厂模式:工厂帮我们创建对象:有一个专门帮我们创建对象的类,我们把这个类叫做工厂类. 例如:Plane plane = PlaneFac ...
- python函数1-函数基础
- 没搞清楚网络I/O模型?那怎么入门Netty
微信搜索[阿丸笔记],关注Java/MySQL/中间件各系列原创实战笔记,干货满满. 本文是Netty系列笔记第二篇 Netty是网络应用框架,所以从最本质的角度来看,是对网络I/O模型的封装使用. ...
- (十六)re模块
正则表达式并不是Python的一部分,本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言.正则表达式是用于处理字符串的强大工具,很多编程语言都支持正则表达式的语法. 字符匹配分为普通字 ...
- service自动发现,yaml文件管理内外部端口访问
service服务发现 [root@k8s-master ~]# vim busybox-5d4f595646-dzjv4.yaml apiVersion: v1 kind: Pod metadata ...
- pg_rman的安装与使用
1.下载对应数据库版本及操作系统的pg_rman源码 https://github.com/ossc-db/pg_rman/releases 本例使用的是centos6.9+pg10,因此下载的是pg ...
- 09--Docker 安装tomcat9
1.在hub.docker.com中获取tomcat拉取地址 docker pull tomcat:9.0.41-jdk8-corretto 2.查看Dockerfile 中WORKDIR 为/use ...
- RocketMQ在linx安装及其有关问题解决
Linx安装和使用: rocketmq官网:http://rocketmq.apache.org/ 首先安装JDK(推荐使用JDK1.8),并配置环境变量 下载rocketmq压碎包并解压到指定目录 ...