# 【瞎搞】 HDU 4710 Balls Rearrangement

第i個 Yi=（i mod B ）

`#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;}`

【瞎搞】 HDU 4710 Balls Rearrangement

## Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD