Balls Rearrangement(HDU)
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).
#include <algorithm>
#include <cstdio>
using namespace std;
#define LL long long LL gcd(LL a,LL b)
{
return !b ? a:gcd(b,a%b);
}
LL lcd(LL a,LL b)
{
return a/gcd(a,b)*b;
} LL fun(LL x,LL a,LL b)
{
LL tmp,i,s;
i=,s=;
while(i<x)
{
tmp=min(a-i%a,b-i%b);
if(tmp+i >= x) tmp=x-i;
s+=abs(i%a-i%b)*tmp;
i+=tmp;
}
return s;
} int main()
{
LL T,n,a,b,t,ans;
scanf("%I64d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d",&n,&a,&b);
t=lcd(a,b);
if(n <= t) ans=fun(n,a,b);
else ans=n/t*fun(t,a,b)+fun(n%t,a,b);
printf("%I64d\n",ans);
}
return ;
}
数论
Balls Rearrangement(HDU)的更多相关文章
- HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)
以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题 此题题意:求sum(|i%A-i%B|)(0<i<N-1) A.B的循环节不同时,会有重 ...
- HDU 4611 Balls Rearrangement(2013多校2 1001题)
Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- (hdu)5391 Zball in Tina Town
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5391 Problem Description Tina Town is a friendl ...
- (hdu)1285 确定比赛名次
Problem Description 有N个比赛队(<=N<=),编号依次为1,,,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接 ...
- hdu 4710 Balls Rearrangement()
http://acm.hdu.edu.cn/showproblem.php?pid=4710 [code]: #include <iostream> #include <cstdio ...
- HDU 4611 Balls Rearrangement (数学-思维逻辑题)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...
- [HDOJ5933]ArcSoft's Office Rearrangement(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5933 题意:长度为nn的数组: a_1, a_2, \cdotsa1,a2,⋯, 每次操作 ...
- (hdu)1042 N! 大数相乘
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1042 Problem Description Given an integer N( ≤ ...
- (hdu)5234 Happy birthday 二维dp+01背包
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5234 Problem Description Today is Gorwin’s birt ...
随机推荐
- python 解释器内建函数001
python解释器内建函数列表如下: 001.abs() 求绝对值 #!/usr/bin/python if __name__=="__main__": print(abs(-10 ...
- asp.net 获取当月的第一天和最后一天示例
DateTime now = DateTime.Now; DateTime d1 = ); DateTime d2 = d1.AddMonths().AddDays(-); d1是本月的第一天,d2本 ...
- ZigBee技术简介
Zigbee的由来 在蓝牙技术的使用过程中,人们发现蓝牙技术尽管有许多优点,但仍存在许多缺陷.对工业,家庭自动化控制和遥测遥控领域而言,蓝牙技术显得太复杂,功耗大,距离近,组网规模太小等,……而工业自 ...
- KeybMap 键盘映射工具更新至 V1.5(修订)
KeybMap 更新至 V1.5,主要是增加了对一些多媒体键定义修改功能,也可以将任意一键定义为打开指定的程序. 3月9日略做修订. http://www.mympc.org/down/1/2005- ...
- QT多重继承的时候,要把QObject放在最前面,否则报错——C++认为人性本恶,默认都是私有的,这点和Delphi的世界观不一样
在买来的控件(没有源码)的基础上,想加入QObject的一些特性,不得不多继承: class MyProgress : public CProgress, public QObject 但总是报错: ...
- Inno Setup:获取isl中的多国语言字串
原文 http://zwkufo.blog.163.com/blog/static/25882512010101041626803/?suggestedreading&wumii 用InnoS ...
- C++14介绍
C++14标准是 ISO/IEC 14882:2014 Information technology -- Programming languages -- C++ 的简称[1] .在标准正式通过之 ...
- Counting square
Problem Description There is a matrix of size R rows by C columns. Each element in the matrix is eit ...
- C链表之创建简单静态链表
C代码: #include<stdio.h> #include<stdlib.h> #include<malloc.h> //创建简单静态链表 typedef st ...
- STL入门
STL入门 STL的组成 六大组件 容器container 算法algorithm 迭代器iterator 仿函数function object 适配器adaptors 空间配制器allocator ...