Problem 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
 
Source
 #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)的更多相关文章

  1. HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)

    以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题 此题题意:求sum(|i%A-i%B|)(0<i<N-1) A.B的循环节不同时,会有重 ...

  2. HDU 4611 Balls Rearrangement(2013多校2 1001题)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  3. (hdu)5391 Zball in Tina Town

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5391 Problem Description Tina Town is a friendl ...

  4. (hdu)1285 确定比赛名次

    Problem Description 有N个比赛队(<=N<=),编号依次为1,,,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接 ...

  5. hdu 4710 Balls Rearrangement()

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 [code]: #include <iostream> #include <cstdio ...

  6. HDU 4611 Balls Rearrangement (数学-思维逻辑题)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...

  7. [HDOJ5933]ArcSoft's Office Rearrangement(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5933 题意:长度为nn的数组: a_1, a_2, \cdotsa​1​​,a​2​​,⋯, 每次操作 ...

  8. (hdu)1042 N! 大数相乘

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1042 Problem Description Given an integer N( ≤ ...

  9. (hdu)5234 Happy birthday 二维dp+01背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5234 Problem Description Today is Gorwin’s birt ...

随机推荐

  1. 进程外Session和进程内Session存储

  2. requirejs--源码分析

    /*---------------------------------------start-------------------------------*/ req({}); // num == 1 ...

  3. MSBI常见问题总结

    SSIS 1.对连接管理器“DC”的AcquireConnection方法调用失败,错误代码0xC0209303.可能在此之前已经发出错误消息,提供了有关AcquireConnection方法调用失败 ...

  4. Android 样式

    先在Value文件夹下建一个Common.xml的文件. <style name="CodeFont" parent="@android:style/TextApp ...

  5. 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 ...

  6. 关于SubclassWindow()和SubclassDlgItem

    msdn上的解析 CWnd::SubclassWindowBOOL SubclassWindow( HWND hWnd ); Return Value Nonzero if the function ...

  7. poj 1088 滑雪_记忆化搜索

    题意:略 直接用记忆化搜索就行了 #include<cstdio> #include<iostream> using namespace std; int n,m; int m ...

  8. #include <amp.h>

    parallel_for_each(av.extent, [=](concurrency::index<1>idx)restrict(amp) {av[idx] += 1; }); //[ ...

  9. android 边学边记 2015.10.16

    1.Menu.FIRST在reference中描述为:First value for group and item identifier integers.我们可以理解为ID设置的最小数值. 2.se ...

  10. 关于httpHandlers、handlers和httpModules、modules的那些配置中的各种问题

    在web.config中配置httpHandlers.handlers和httpModules.modules根据服务器环境不同设置各有不同 在IIS6及IIS7.0以上的经典模式中配置httpMod ...