HDU 4611 Balls Rearrangement (数学-思维逻辑题)

该博客主要解析HDU 4611题目,涉及数学和思维逻辑问题。作者提供了AC(Accepted)代码来解决如何重新排列球的问题。
摘要由CSDN通过智能技术生成

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611


题意:给你一个N、A、B,要你求



AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <ctime>
#pragma comment(linker, "/STACK:16777216")
using namespace std;

typedef __int64 LL;
const int N=10005;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);

LL gcd(LL a,LL b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}

LL lcm(LL a,LL b)
{
    return a/gcd(a,b)*b;
}

LL cost(LL n,LL a,LL b)
{
    LL ans=0,x=0,y=0,tmp=0,now=0;
    while(now<n)
    {
        tmp=min(a-x,b-y);//找到最近达到0的有几个数,也就是当前有几个相同的数字
        if(tmp+now>n)
            tmp=n-now;
        ans+=tmp*(abs(x-y));
        x=(x+tmp)%a;
        y=(y+tmp)%b;
        now+=tmp;
    }
    return ans;
}

int main()
{
    int t;
    cin>>t;
    LL n,a,b;
    while(t--)
    {
        cin>>n>>a>>b;
        LL l=lcm(a,b);
        if(l<=n)
            cout<<cost(l,a,b)*(n/l)+cost(n%l,a,b)<<endl;
        else
            cout<<cost(n,a,b)<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值