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 ...
随机推荐
- 【剑指 Offer】10-I.斐波那契数列
题目描述 写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项.斐波那契数列的定义如下: F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - ...
- 剑指offer 面试题10.1:青蛙跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 编程思想 对于本题,前提只有 一次 1阶或者2阶的跳法.a.如果两种跳 ...
- HDU6375双端队列
要点分析: 1.本题可以使用C++STL中的deque双端队列来方便解决(底层是一个双向的链表) 2.值得注意的是N的上限为150000,所以直接开这么大的空间会超内存,可以配合map一起使用 关于双 ...
- 如何实现一个简易版的 Spring - 如何实现 Constructor 注入
前言 本文是「如何实现一个简易版的 Spring」系列的第二篇,在 第一篇 介绍了如何实现一个基于 XML 的简单 Setter 注入,这篇来看看要如何去实现一个简单的 Constructor 注入功 ...
- 【Python】简单的脚本,轻松批量修改文件名称
使用python脚本,批量修改文件夹名称 先创建一些没用的案例文件 import os #创建新文件夹 dir = os.makedirs('D:\\SomeThing\\testfile') #将文 ...
- LeetCode501.二叉搜索树中的众数
题目,本题未做出,还有很多要学习 class Solution { public: vector<int>ans; int base,count,maxCount; void update ...
- SAP demo包 示例程序
在SAP的这个开发类中SABAPDEMOS,存放了N多的demo程序 有空的时候,可以看看.
- [USACO13DEC]牛奶调度Milk Scheduling
原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=4096 容易想到的一个测略就是,优先考虑结束时间小的牛.所以我们对所有牛按照结束时间排序.然 ...
- C#高级编程第11版 - 第五章 索引
[1]5.1 泛型概述 1.通过泛型,你可以创建独立于特定类型(contained types)以外的方法和类,而不用为不同类型编写多份同样功能的代码,你只需要创建一个方法或者类. 2.泛型类使用泛型 ...
- Azure DevOps Pipelines执行RobotFramework自动化代码
一.Azure DevOps介绍 1.什么是 Azure DevOps? Azure DevOps其实是VSTS(Visual Studio Team Service)更名后的名字.而VSTS是TFS ...