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 4710 Balls Rearrangement

    // [7/31/2014 Sjm] /* 看到此题直接先打表找规律(数学很糟糕,不会用公式推,只好打表找规律) 发现循环周期 T = A和B的最小公倍数 于是依靠区间 [min(a, b), T] ...

  2. HDU——1013(字符串+数学)Digital Roots

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013 题意:给你一个整数列表,让位数上的数相加得到一个新的数,若为个位数则输出,若不为个位数重复此操作 ...

  3. 2013 ACM/ICPC Asia Regional Online —— Warmup1 1005 Balls Rearrangement

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4706 以前做过的题目: i从0到n-1时,如果一个一个加会很慢,注意到如果mod a的序列 和mod ...

  4. HDU 5985 Lucky Coins 数学

    Lucky Coins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5985 Description Bob has collected a lot ...

  5. hdu 5312 Sequence(数学推导——三角形数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others)  ...

  6. HDU 3636-Dragon Balls

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  7. HDU 6217 BBP Formula (数学)

    题目链接: HDU 7217 题意: 题目给你可以计算 \(π\) 的公式: \(\pi = \sum_{k=0}^{\infty}[\frac{1}{16^k}(\frac{4}{8k+1})-(\ ...

  8. HDU - 6641 TDL(数学)

    Problem Description For a positive integer n, let's denote function f(n,m) as the m-th smallest inte ...

  9. CF850F Rainbow Balls(数学、期望)

    解析 二倍相邻项,就要想裂项 纯数学题 一道黑色的小凯的疑惑 设总球数为 sss 我们先钦定一种颜色留到最后,那么其他颜色可以等价考虑 设 fif_{i}fi​ 为钦定的颜色的球有 iii 个,涂完需 ...

最新文章

  1. how is table select_all_icon being loaded
  2. getsize java_Java ZipEntry getSize()用法及代码示例
  3. lua运行外部程序_LTUI v2.2 发布, 一个基于lua的跨平台字符终端UI界面库
  4. 记录ie下报XMLHttpRequest: 网络错误 0x80070005, 拒绝访问。
  5. H.264解码器ffmpeg完整优化代码(包括PC和Windows Mobile版本)
  6. 欧氏空间内积定义_MP5:内积、外积、面积、Hermite内积、辛内积
  7. linux上的定时器上的jiffies,Linux kernel -- 定时器/jiffies
  8. iOS 加载本地Gif图片
  9. ReportMachine报表控件唯一官方论坛 - http://rmachine.5d6d.com/
  10. linux 单网卡 双网段,用单网卡连接两个网段
  11. PPT批量导入文字(Python实现)
  12. JavaScript设计模式----装饰者模式
  13. (休息几天)读米什金之货币银行学——金融市场工具
  14. AtCoder Beginner Contest 275 A-D题解
  15. echarts 简单词云制作,自定义图案词云echarts-wordcloud.js
  16. backdoor-factory工具使用
  17. Android Webview调相机拍照选择图片并显示到网页
  18. 华大460 GPIO 例程赏析_20220911
  19. 不一样的“中国速度”,数据可视化交通运输大屏,带你见证中国高铁
  20. 火车票抢票API 根据乘客的车次与座席要求快速订票出票 1

热门文章

  1. hive sql 学习笔记
  2. EZ 2018 03 23 NOIP2018 模拟赛(五)
  3. 在iframe框架中全屏不好使的原因
  4. HDU6156 Palindrome Function
  5. java堆内存与栈内存区别
  6. [3]MVC3中使用Telerik(转)
  7. Oracle 多行记录合并/连接/聚合字符串的几种方法
  8. BASH启动脚本及其启动顺序
  9. C#.NET 比较好用的tcp通信模板(服务器端篇)
  10. 如何移动SQL SERVER的系统数据库