Balls Rearrangement

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=4611

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

Hint

题意

有n个数,给你a和b

T1[i]=i%a,T2[i]=i%b

求sigma(T1[i]-T2[i])

题解:

简单思考一下,他们之间的差一开始都是0的,当遇到某个数是a的倍数的时候,他们之间每个数的差就会增加a,遇到某个数是b的倍数的时候,差就会减少b

根据这个去跑一个lcm周期的答案就好了

然后再暴力算最后一个n%lcm的答案就好了

代码

#include<bits/stdc++.h>
using namespace std; long long gcd(long long a,long long b)
{
if(b==0)return a;
return gcd(b,a%b);
}
long long lcm(long long a,long long b)
{
return a*b/gcd(a,b);
}
vector<long long>p;
void init()
{
p.clear();
}
void solve()
{
init();
long long n,a,b;
cin>>n>>a>>b;
long long c = lcm(a,b);
for(long long i=1;i*a<=c;p.push_back(i*a),i++);
for(long long i=1;i*b<=c;p.push_back(i*b),i++);
p.push_back(0);sort(p.begin(),p.end());p.erase(unique(p.begin(),p.end()),p.end());
long long ans = 0,now = 0;
for(int i=1;i<p.size()-1;i++)
{
if(p[i]%a==0)now-=a;
if(p[i]%b==0)now+=b;
ans+=abs(now)*(p[i+1]-p[i]);
}
long long k = n/c;
long long Ans = 0;
Ans = Ans + k*ans;
k = n%c;
int kkk = 0;
for(int i=0;i<p.size()-1&&p[i+1]<k;i++)
{
kkk = i+1;
if(p[i]%a==0)now-=a;
if(p[i]%b==0)now+=b;
Ans+=abs(now)*(p[i+1]-p[i]);
}
if(p[kkk]%a==0)now-=a;
if(p[kkk]%b==0)now+=b;
Ans+=abs(now)*(k-p[kkk]);
cout<<Ans<<endl;
}
int main()
{
int t;scanf("%d",&t);
while(t--)solve();
return 0;
}

HDU 4611 Balls Rearrangement 数学的更多相关文章

  1. HDU 4611 Balls Rearrangement(2013多校2 1001题)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  2. HDU 4611 Balls Rearrangement (数学-思维逻辑题)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...

  3. HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)

    以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题 此题题意:求sum(|i%A-i%B|)(0<i<N-1) A.B的循环节不同时,会有重 ...

  4. hdu 4611 Balls Rearrangement

    http://acm.hdu.edu.cn/showproblem.php?pid=4611 从A中向B中移动和从B中向A中移动的效果是一样的,我们假设从B中向A中移动 而且A>B 我们先求出所 ...

  5. hdu 4710 Balls Rearrangement (数学思维)

    意甲冠军:那是,  从数0-n小球进入相应的i%a箱号.然后买一个新的盒子. 今天的总合伙人b一个盒子,Bob试图把球i%b箱号. 求复位的最小成本. 每次移动的花费为y - x ,即移动前后盒子编号 ...

  6. HDU 5570 balls 期望 数学

    balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5570 De ...

  7. hdu 4710 Balls Rearrangement()

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 [code]: #include <iostream> #include <cstdio ...

  8. hdu 4710 Balls Rearrangement

    题意就不说了,刚开始做我竟然傻傻地去模拟,智商捉急啊~~超时是肯定的 求出 a ,b 的最小公倍数,因为n够长的话,就会出现循环,所以就不要再做不必要的计算了.如果最小公倍数大于n的话,就直接计算n吧 ...

  9. hdu 4710 Balls Rearrangement 数论

    这个公倍数以后是循环的很容易找出来,然后循环以内的计算是打表找的规律,规律比较难表述,自己看代码吧.. #include <iostream> #include <cstdio> ...

随机推荐

  1. 70.如何在xilinx SDK中显示行号

    Window→preferences→editor→test editor 对ecilpse的通用方法 打开Eclipse软件,在菜单中选择窗口——首选项,打开新的窗口. 在新的窗口中依次选择常规—— ...

  2. Centos更新配置文件命令

    source 命令是 bash shell 的内置命令,从 C Shell 而来.source 命令的另一种写法是点符号,用法和 source 相同,从Bourne Shell而来.source 命令 ...

  3. SpringBoot微服务

    在企业级软件的架构模型上,我们主要讨论下SOA与微服务架构. SOA的全称是Service-Oriented Architecture,可译为“面向服务的架构”,它是一个组件模型,将应用程序的不同功能 ...

  4. Nginx - 限制并发、限制访问速率、限制流量

    1. 前言 本文针对 Nginx 的三个模块进行配置,并证实各自的功能特点: (1)limit_conn_zone 模块  - 限制同一 IP 地址并发连接数: (2)limit_request 模块 ...

  5. Java打包问题之一:打包出现java.io.IOException: invalid header field

    前言 java的打包工具jar有时候会出一些莫名其妙的问题,比如不合法的头部字段等等.这些问题之前也没注意,因为一直是用eclipse打包.后来在公司的时候,要求统一编写shell脚本来进行打包. 其 ...

  6. Android 下使用 JSON 实现 HTTP 请求,外加几个示例!

    不得不说,JSON 格式的确是非常美妙的,速度快而且简化了很多操作在 Android 下,Android SDK 已经为我们封装好了整个与 JSON 有关的操作,使用非常方便 以下就是一个标准的 JS ...

  7. [翻译]HLS实践

    最近公司项目没事做,课余实践研究一下技术,算是积累,也可以用到项目里,从零开始记录 HLS:Http Live Streaming 官方文档 https://developer.apple.com/s ...

  8. Three.js基础探寻七——Lamber材质与Phong材质

    材质(Material)是独立于物体顶点信息之外的与渲染效果相关的属性.通过设置材质可以改变物体的颜色.纹理贴图.光照模式等. 本篇将介绍基本材质以及两种基于光照模型的材质(Lamber与Phong) ...

  9. 在kubernetes运行一个容器案例

    1. 检查kubernetes 组件是否正常运行. [root@c720120 ~]# kubectl get cs NAME                 STATUS    MESSAGE    ...

  10. 在Docker中自定义Jenkins镜像

    一. 构建Jenkins slave. 1. 构建镜像需要三个步骤: (1) 创建Dockerfile (2) 构建镜像 (3)在master上改变agent的配置 2. 以下是创建一个服务Pytho ...