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. HD1083 二分图,匈牙利算法

    #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<stdio.h> #include<stdlib. ...

  2. KeyEvent

    http://blog.csdn.net/elfylin/article/details/8008763 一. 接口KeyEvent.Callback和View.OnKeyListener 二. 流程 ...

  3. Inno Setup 精灵显示插件 InnoFairy (V2.0 版本)

    原文 http://restools.hanzify.org/article.asp?id=111 一个如影随形的小精灵会令到你的安装程序更加人性化. 就是这样一个功能的Inno Setup插件, 希 ...

  4. 一张图片入门Python

    一张图片入门Python 之前已有别人整理了,一张图入门Python,快速了解各种基本的语法. 英文版: 图 5.1. Quick Python Script Explanation 中文版: 图 5 ...

  5. linux grep练习

    1.显示/proc/meminfo文件中以不区分大小的s开头的行: 2.显示/etc/passwd中以nologin结尾的行; 3.显示/etc/inittab中以#开头,且后面跟一个或多个空白字符, ...

  6. 【剑指offer】连续子数组的最大和

    个開始,到第3个为止).你会不会被他忽悠住? 输入: 输入有多组数据,每组測试数据包括两行. 第一行为一个整数n(0<=n<=100000),当n=0时,输入结束.接下去的一行包括n个整数 ...

  7. 用HTML5、地理定位API和Web服务来开发移动应用

    HTML 5 是一项让人振奋的技术,这有着充分的理由.这将会是一次技术突破,因为它可以将桌面应用程序功能带入浏览器中.除了传统浏览器外,对于移动浏览器,其潜力甚至更大.不仅如此,最流行的移动浏览器甚至 ...

  8. Bostonkey Simple calc

    Simple Calc 明显的memcpy栈溢出,是一个静态链接的程序所以没给libc.发现里面有: 参数a1应该为_libc_stack_end的地址了._stack_prot通过rop修改为0x7 ...

  9. 【十分不错】【离线+树状数组】【TOJ4105】【Lines Counting】

    On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you ...

  10. .NET 创建并写CSV文件

    /// <summary> /// 创建并写日志 /// </summary> /// <param name="SuccessA100">&l ...