HDU 4611 Balls Rearrangement 数学
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 数学的更多相关文章
- HDU 4611 Balls Rearrangement(2013多校2 1001题)
Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 4611 Balls Rearrangement (数学-思维逻辑题)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...
- HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)
以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题 此题题意:求sum(|i%A-i%B|)(0<i<N-1) A.B的循环节不同时,会有重 ...
- hdu 4611 Balls Rearrangement
http://acm.hdu.edu.cn/showproblem.php?pid=4611 从A中向B中移动和从B中向A中移动的效果是一样的,我们假设从B中向A中移动 而且A>B 我们先求出所 ...
- hdu 4710 Balls Rearrangement (数学思维)
意甲冠军:那是, 从数0-n小球进入相应的i%a箱号.然后买一个新的盒子. 今天的总合伙人b一个盒子,Bob试图把球i%b箱号. 求复位的最小成本. 每次移动的花费为y - x ,即移动前后盒子编号 ...
- HDU 5570 balls 期望 数学
balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5570 De ...
- hdu 4710 Balls Rearrangement()
http://acm.hdu.edu.cn/showproblem.php?pid=4710 [code]: #include <iostream> #include <cstdio ...
- hdu 4710 Balls Rearrangement
题意就不说了,刚开始做我竟然傻傻地去模拟,智商捉急啊~~超时是肯定的 求出 a ,b 的最小公倍数,因为n够长的话,就会出现循环,所以就不要再做不必要的计算了.如果最小公倍数大于n的话,就直接计算n吧 ...
- hdu 4710 Balls Rearrangement 数论
这个公倍数以后是循环的很容易找出来,然后循环以内的计算是打表找的规律,规律比较难表述,自己看代码吧.. #include <iostream> #include <cstdio> ...
随机推荐
- 遍历目录大小——php经典实例
遍历目录大小——php经典实例 <?php function dirSize($dir){ //定义大小初始值 $sum=; //打开 $dd=opendir($dir); //遍历 while ...
- 删除none的images
脚本 #!/bin/bash docker ps -a | grep "Exited" | awk '{print $1 }'|xargs docker stop docker p ...
- 01布尔模型&倒排索引
原文链接: http://www.cnblogs.com/jacklu/p/8379726.html 博士一年级选了这门课 SEEM 5680 Text Mining Models and Appli ...
- 在Linux(CentOS)中安装.netcore SDK
官方链接 :https://dotnet.microsoft.com/download/linux-package-manager/centos/sdk-current 可以直接根据官方链接,选择Li ...
- Windows下Oracle数据库自动备份批处理脚本
expdb命令版本 @echo off REM ########################################################### REM # Windows Se ...
- 苹果的浏览器safari无法识别 2016-1-1这样的日期,会返回Invalid Date
1.很多时候我们遇到的日期是2016-1-1这样的,中间是带横线的,但是有时候我们需要转化为标准的时间,即使用new Date(time)这样的方法,这时在safari浏览器里面Invalid Dat ...
- django入门--django-blog-zinnia搭建个人博客
1.安装python 选择合适python2.7及以上版本安装https://www.python.org/downloads/ 2.建立虚拟环境 这不是必须的,但是建议使用,为每个项目单独引入依赖, ...
- hashCode()与equals()区别
这两个方法均是超类Object自带的成员方法.Object类是所有Java类的祖先.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法.在不明确给出超类的情况下,Java会自 ...
- AWS 使用总结
A.升配置的流程: 1.新开一台配置较高的机器; 2.将新机器和老机器的磁盘都取消关联,注意需要记录下老机器的磁盘分区设备名,如:/dev/sda1: 3.将老机器的磁盘挂载到新机器上,磁盘分区设备名 ...
- 创建 dblink
目的:oracle中跨数据库查询 两台数据库服务器db_A(本地)和db_B(远程192.168.1.100),db_A下用户user_a 需要访问到db_B下user_b的数据解决:查询 ...