2016wustacm7_7K

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; background-color: rgb(255, 255, 255);">Description</span>

  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


题意:将原本按ni%A的方式放置的球,放到数目为B的一堆盒子中,方式是ni%B;而将一个球从原来的位置变到后来的位置

需要的成本是|n1%A-ni%B|.求出总成本最小是多少.

 <span style="white-space:pre">#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
long long N,A,B;
long long gcd(long long a,long long b){         //数据很大需要用longlong储存
    return b?gcd(b,a%b):a;
}
long long lcm(long long a,long long b){
    return a*b/gcd(a,b);        //最小公倍数与最大公约数之间的等式关系        也可写成a/gcd(a,b)*b;
}
long long solve(long long t,long long a,long long b){
    long long ai=0,bi=0;
    long long an=0,po=0,ii=0;
    long long mi;
    int flag;
    while(ii<t){
            if(ai+a>=t&&bi+b>=t){
                an+=(t-ii)*po;
                ii=t;
                continue;
            }
            mi=ai+a;
            flag=1;
            if(mi>bi+b){
                mi=bi+b;
                flag=0;
            }
            an+=(mi-ii)*po;
            if(flag){
                ii=mi;
                if(mi==bi+b){
                    po=0;
                    bi+=b;
                }
                else{
                    po=ii-bi;
                }
                ai+=a;
            }
            else{
                ii=mi;
                po=ii-ai;
                bi+=b;
            }
    }
    return an;
}


int main(){
    int T;
    while(scanf("%d",&T)!=EOF){
        while(T--){
            scanf("%lld%lld%lld",&N,&A,&B);
            if(A<B){            //保证A>B;
                long long sw=A;
                A=B;
                B=sw;
            }
            long long lc=lcm(A,B);
            long long ans=0;
            if(lc>=N){
                ans=solve(N,A,B);
            }
            else{
                ans=solve(lc,A,B)*(N/lc)+solve(N%lc,A,B);
            }
            printf("%lld\n",ans);
        }
    }
return 0;
}
/**
 /*if(ai+a>=t&&bi+b>=t){
            an+=po*(t-ii);
            ii=t;
            continue;
        }
        if(ai+a<bi+b){
            an+=(ai+a-ii)*po;
            ii=ai+a;
            po=ii-bi;
            ai+=a;
        }
        else if(ai+a==bi+b){
            an+=(ai+a-ii)*po;
            ii=ai+a;
            po=0;
            ai+=a;
            bi+=b;
        }
        else{
            an+=(bi+b-ii)*po;
            ii=bi+b;
            po=ii-ai;
            bi+=b;
        }*/






/*
LL cal(int x, int a, int b) {       //寻找周期内i%a-i%b值不变的区间内i%a-i%b的值
    int cnt = 0;
    int r1, r2, r;
    r1 = r2 = 0;
    LL t, ans = 0;
    while (cnt < x) {                   //该周期还没结束
        r = min(a - r1, b - r2);
        t = abs(r1 - r2);                  //
        r = min(r, x - cnt);
        r1 = (r1 + r) % a;
        r2 = (r2 + r) % b;
        ans += t * r;
        cnt += r;
    }
    return ans;
}
*/
//(3)**************
/*
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;


long long N,A,B;


long long gcd(long long a,long long b){
    return b?gcd(b,a%b):a;      //求最大公约数
}


long long lcm(long long a,long long b){         //求最小公倍数         i%a-i%b的值出现的周期是这个
    return a/gcd(a,b)*b;
}


/*(i%a-i%b的值)在一个周期的一定区间内是相同不变的*/
/**每次不是只增加一个数,
求出最大mi个球在两个盒子的序号都是递增的,  //先求出在a,b两种盒子中各自的mi,然后取最小值
那么每次只需要加上第一项差值的mi倍,
球的序号加上mi**/
/*
long long slove(long long t,long long a,long long b){
    int an,i,mi;
    i=0;an=0;
    while(i<t) {            //球还没有用完
        mi=(a-i%a)>(b-i%b)?(b-i%b):(a-i%a);         //最多mi个球在两个盒子之间的序号是递增的
        /**
        a-i%a是a盒子中球序号递增的区间
        b-i%b是b盒子中球序号递增的区间
        **/
   /*     if(i+mi>=t)
            mi=t-i;                     //超出周期范围时,mi就只能变作剩下的
        if(i%a-i%b<0){
            an+=(i%b-i%a)*mi;
        }
        else{
            an+=(i%a-i%b)*mi;
        }
        i+=mi;
    }
    return an;
}


int main(){
    int T;
    while(cin>>T){
        while(T--){
            cin>>N>>A>>B;
            long long f=lcm(A,B);
            long long ans;
            if(f>=N){               //如果最小公倍数比球的数目还要多,就按球的数目为界限,来寻找结果
                ans=slove(N,A,B);
            }
            else {
                    ans=slove(f,A,B)*(N/f)+slove(N%f,A,B);          //如果最小公倍数比球数小,那么把球数分成N/f+1份,再来求结果
            }
            printf("%lld\n",ans);
        }
    }
    return 0;
}
*/</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值