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

这个题在多校出现过,借个kuangbin大神的代码

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
using namespace std;
long long gcd(long long a,long long b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}
long long lcm(long long a,long long b)
{
    return a/gcd(a,b)*b;
}
long long calc(int n,int a,int b)
{
    long long ans = 0;
    int i = 0;
    int ta=0,tb=0;
    int p = 0;
    while(i < n)
    {
        if(ta+a >= n && tb+b >= n)
        {
            ans += (long long)(n-i)*p;
            i = n;
            continue;
        }
        if(ta+a < tb+b)
        {
            ans += (long long)p*(ta+a-i);
            i = ta+a;
            p = i - tb;
            ta+=a;
        }
        else if(ta+a==tb+b)
        {
            ans+= (long long)p*(ta+a-i);
            i = ta+a;
            ta+=a;
            tb+=b;
            p = 0;
        }
        else
        {
            ans += (long long)p*(tb+b-i);
            i = tb+b;
            tb+= b;
            p = i-ta;
        }
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    int n,a,b;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&a,&b);
        if(a==b)
        {
            printf("0\n");
            continue;
        }
        if(a < b)swap(a,b);
        long long LCM = lcm(a,b);
        if(LCM >= n)
        {
            printf("%I64d\n",calc(n,a,b));
            continue;
        }
        long long tmp = calc(LCM,a,b);
        long long ans = tmp * (n/LCM)+calc(n%LCM,a,b);
        printf("%I64d\n",ans);
    }
    return 0;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值