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. Leetcode 100 Same Tree python

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  2. jquery跨域访问解决方案(转)

    客户端“跨域访问”一直是一个头疼的问题,好在有jQuery帮忙,从jQuery-1.2以后跨域问题便迎刃而解.由于自己在项目中遇到跨域问题,借此机会对跨域问题来刨根问底,查阅了相关资料和自己的实践,算 ...

  3. SDK Manager 报错:Connection timed out: connect

    安装Eclipse的安卓开发环境的时候,安装sdk时报错,出现: 解决办法: 1.选择左上角的Tools 2.选择Options,勾选下面红色框的东西 3. 4.重新重启一下sdk manager即可

  4. MEMS加速度计工作原理

    一般加速度计有两块芯片组成,一块是MEMS传感器,另一块是客户化的信号处理芯片. 加速度计也称惯性传感器,因为它的工作原理就是靠MEMS中可移动部分的惯性.由于中间电容板的质量很大,而且它是一种悬臂构 ...

  5. 14.3.5.1 Interaction of Table Locking and Transactions 表锁和事务的相互作用

    14.3.5.1 Interaction of Table Locking and Transactions 表锁和事务的相互作用 LOCK TABLES 和UNLOCK TABLES 交互实用事务如 ...

  6. Chapter 1. Hello, Perl/Tk

    Chapter 1. Hello, Perl/Tk 内容: Perl/Tk Concepts Some Perl/Tk History Getting Started with Perl/Tk Hel ...

  7. UML_行为图

    活动图是UML用于对系统的动态行为建模的另一种常用工具,它描述活动的顺序,展现从一个活动到另一个活动的控制流.活动图在本质上是一种流程图.活动图着重表现从一个活动到另一个活动的控制流,是内部处理驱动的 ...

  8. 使用Oracle安装账户登录数据库

    如果Linux或Unix平台,安装了Oracle,而且有root账号,则使用“su Oracle安装账户”的方式,sqlplus登录到数据库,这种方式是最可靠的.正如@dbsnake所说,”主要是Or ...

  9. hdu 4775 Infinite Go(暴力)

    pid=4775" target="_blank" style="">题目链接:hdu 4775 Infinite Go 题目大意:两个人下围棋 ...

  10. android gallery的使用

    1: xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xml ...