【瞎搞】 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 <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <deque>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long  LL;
const int MAXN = 100+6;
const int MAXM = 140000;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
LL gcd(LL a,LL b)
{
    if(b==0)
        return a;
    else return gcd(b,a%b);
}
inline LL abss(LL a,LL b)
{
    if(a>b)
    return a-b;
    else return b-a;
}
LL ans(LL n,LL a,LL b)
{
    LL len=0,x=0,y=0,p=0,out=0;
    while(len<n)
    {
        LL m=min(a-x,b-y);
        if(len+m>=n)
            m=n-len;
        out+=abss(x,y)*m;
        x=(x+m)%a;
        y=(y+m)%b;
        len+=m;
    }
    return out;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
   //  freopen("out.txt", "w", stdout);
#endif
    LL t,n,a,b;
    cin>>t;
    while(t--)
    {
        cin>>n>>a>>b;
        LL lcm=a*b/gcd(a,b);
       // cout<<lcm<<endl;     //每段lcm里的值都是相同的
        cout<<n/lcm*ans(lcm,a,b)+ans(n%lcm,a,b)<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值