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 ...
随机推荐
- 进程外Session和进程内Session存储
- requirejs--源码分析
/*---------------------------------------start-------------------------------*/ req({}); // num == 1 ...
- MSBI常见问题总结
SSIS 1.对连接管理器“DC”的AcquireConnection方法调用失败,错误代码0xC0209303.可能在此之前已经发出错误消息,提供了有关AcquireConnection方法调用失败 ...
- Android 样式
先在Value文件夹下建一个Common.xml的文件. <style name="CodeFont" parent="@android:style/TextApp ...
- Oracle EBS-SQL (OM-6):打开订单.sql
/*打开头*/ update oe_order_headers_all t set t.flow_status_code = 'BOOKED', t.open_flag = 'Y' where t.o ...
- 关于SubclassWindow()和SubclassDlgItem
msdn上的解析 CWnd::SubclassWindowBOOL SubclassWindow( HWND hWnd ); Return Value Nonzero if the function ...
- poj 1088 滑雪_记忆化搜索
题意:略 直接用记忆化搜索就行了 #include<cstdio> #include<iostream> using namespace std; int n,m; int m ...
- #include <amp.h>
parallel_for_each(av.extent, [=](concurrency::index<1>idx)restrict(amp) {av[idx] += 1; }); //[ ...
- android 边学边记 2015.10.16
1.Menu.FIRST在reference中描述为:First value for group and item identifier integers.我们可以理解为ID设置的最小数值. 2.se ...
- 关于httpHandlers、handlers和httpModules、modules的那些配置中的各种问题
在web.config中配置httpHandlers.handlers和httpModules.modules根据服务器环境不同设置各有不同 在IIS6及IIS7.0以上的经典模式中配置httpMod ...