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. Lucene7.2.1系列(二)luke使用及索引文档的基本操作

    系列文章: Lucene系列(一)快速入门 Lucene系列(二)luke使用及索引文档的基本操作 Lucene系列(三)查询及高亮 luke入门 简介: github地址:https://githu ...

  2. pip安装使用详解【转】

    转自:pip安装使用详解 – 运维生存时间http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa/ pip类似RedHat里面的yum ...

  3. 全局应用程序类(Global.asax)

     注:该部分参考的园区的“积少成多”的 <ASP.NET MVC中的Global.asax文件> . 1.Global.asax文件介绍 global.asax这个文件包含全局应用程序事件 ...

  4. 防范SQL注入

    使用占位符的方式写查询语句hibernate

  5. poj 2420(模拟退火)

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6066   Accepted: 285 ...

  6. javscript练习(三)

    编写一个函数,计算两个数字的和差积商 function calculator(num1,num2,sign){          switch(sign){                   cas ...

  7. python进行des加密解密,而且可以与JAVA进行互相加密解密

    import binasciifrom pyDes import des, CBC, PAD_PKCS5import uuidimport time # pip install -i https:// ...

  8. 【Mac】【已解决】连接Android机器提示“此电脑不能读取您插入的磁盘”

    出现的报错提示页面截图如下: 解决方案: 下载“Android File Transfer.dmg”安装在Mac. 打开USB调试,连接手机即可读取手机磁盘.   下载链接:https://www.t ...

  9. (转载)使用curl 和 libjson 完成联网和数据解析

    转载地址:http://my.oschina.net/cocosgame/blog/71181 libjson 编译和使用 - 3. libjson的C接口 API http://blog.csdn. ...

  10. 【严蔚敏】【数据结构题集(C语言版)】1.16 自大至小依次输出读入的三个整数X,Y,Z

    #include <stdio.h> #include<stdlib.h> int main() { int x,y,z,temp; scanf("%d%d%d&qu ...