标签: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
标签:style blog http io ar os sp on 2014
原文地址:http://blog.csdn.net/kewowlo/article/details/41522501