標籤:style blog http io ar os sp on 2014
題目地址:點擊開啟連結
題意:有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;}
【瞎搞】 HDU 4710 Balls Rearrangement