hdu 4611 Balls Rearrangement 数学

Balls Rearrangement

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

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

Author

SYSU

Source

2013 Multi-University Training Contest 2

题意:求sigma(i%a-i%b);

思路:显然lcm为循环节;

     当同时模加1 的时候差值相等所以只有在对A或B取模为0的时候会改变;

   即A、B的倍数为断点;

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=3e5+10,M=1e6+10,inf=1e9,mod=1e9+7;
ll a[N],b[N],c[N];
ll gcd(ll x,ll y)
{
    return y==0?x:gcd(y,x%y);
}
int main()
{
    ll x,y,z,i,t;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d%I64d%I64d",&x,&y,&z);
        ll gc=gcd(y,z);
        ll lcm=y*z/gc;
        int ji=1;
        for(i=1;i<=lcm/y;i++)
        a[ji++]=i*y;
        for(i=1;i<lcm/z;i++)
        a[ji++]=i*z;
        sort(a,a+ji);
        for(i=0;i<ji-1;i++)
        {
            b[i]=a[i+1]-a[i];
            c[i]=abs(a[i]%y-a[i]%z);
        }
        ll sum=0;
        for(i=0;i<ji-1;i++)
        sum+=b[i]*c[i];
        ll ans=0;
        ans+=x/lcm*sum;
        x%=lcm;
        for(i=0;i<ji-1;i++)
        {
            if(x<=0)
            break;
            if(x<b[i])
            ans+=x*c[i];
            else
            ans+=b[i]*c[i];
            x-=b[i];
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-10-28 19:34:57

hdu 4611 Balls Rearrangement 数学的相关文章

hdu 4710 Balls Rearrangement (数学思维)

题意:就是  把编号从0-n的小球对应放进i%a编号的盒子里,然后又买了新盒子, 现在总共有b个盒子,Bob想把球装进i%b编号的盒子里.求重置的最小花费. 每次移动的花费为y - x ,即移动前后盒子编号的差值的绝对值. 算法: 题目就是要求                  先判断  n与  lcm(a,b)的大小,每一个周期存在循环,这样把区间缩短避免重复计算. 如果n>lcm(a,b)则   ans = (n/lcm)*solve(lcm)+solve(n%lcm) 否则   ans =

HDU 5570 balls 期望 数学

balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5570 Description There are n balls with m colors. The possibility of that the color of the i-th ball is color j is ai,jai,1+ai,2+...+ai,m. If the number of b

【瞎搞】 HDU 4710 Balls Rearrangement

题目地址:点击打开链接 题意:有N 个小球,编号 0--N-1 两个序列      第 i 个 球 Xi = (i mod A ) 第i个 Yi=(i mod B ) 求   sigma(|Xi-Yi |) 可以知道总有一段 Xi 与Yi 的差是相等的 模拟下可以加快速度. #include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include <iostre

hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 735    Accepted Submission(s): 305 Problem Description Bob has N balls and A b

HDU 4611

Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 2862    Accepted Submission(s): 1071 Problem Description Bob has N balls and A boxes. He numbers the balls from 0 to N-1, and

HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spherical deep-sea submersible which was unpowered and lowered into the ocean on a cable, and was used to conduct a series of dives under the sea. The Bathys

HDU 4937 Lucky Number (数学,进制转换)

题目 参考自博客:http://blog.csdn.net/a601025382s/article/details/38517783 //string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last); //把[first0,last0)之间的部分替换成[first,last)之间的字符串 /* 题意: 我们将3,4,5,6认为是幸运数字.给定一个十进制数n. 现在可以讲起任意转

HDU 1018 Big Number 数学题解

Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of

hdu 4823 Energy Conversion(数学)

题目链接:hdu 4823 Energy Conversion 题目大意:中文题,不解释. 解题思路:首先判断一下m是否已经大于n了,如果大于那么就是0,假设中间变换的各个值为ai,那么bi=ai+c,bi数组为等比数组(可推),所以就有了cnt=log((n+c)a)log(double(k)),结果为浮点数,需要向上取整. #include <cstdio> #include <cstring> #include <cmath> int main () { int