[hdu4710 Balls Rearrangement]分段统计

题意:求∑|i%a-i%b|,0≤i<n

思路:复杂度分析比较重要,不细想还真不知道这样一段段跳还真的挺快的=.=

  • 令p=lcm(a,b),那么p就是|i%a-i%b|的循环节。考虑计算n的答案,令答案为f(n),则最后的结果可以表示为n/p*f(p)+f(n%p)
  • 考虑计算f(n),设两个指针i、j,表示当前累加答案的起始位置分别对a和b取模的结果,则每次i与j中间的一个可以变成0,跳过的一段区间中答案是相同的。
  • 复杂度分析,令a<b,则每次跳过的区间长度近似等于a,则总的复杂度为O(n/a)=O(lcm(a,b)/a)=O(b),也就是说复杂度大致为O(max(a,b))

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define X                   first
#define Y                   second
#define pb                  push_back
#define mp                  make_pair
#define all(a)              (a).begin(), (a).end()
#define fillchar(a, x)      memset(a, x, sizeof(a))
#define copy(a, b)          memcpy(a, b, sizeof(a))

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}

const double PI = acos(-1.0);
const int INF = 1e9 + 7;
const double EPS = 1e-12;

/* -------------------------------------------------------------------------------- */

ll a, b, n;

ll gcd(ll a, ll b) {
    return b? gcd(b, a % b) : a;
}

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

ll solve(ll n) {
    ll p = 0, q = 0, c = 0, ans = 0;
    while (c < n) {
        ll buf = min(a - p, b - q);
        umin(buf, n - c);
        c += buf;
        ans += buf * abs(p - q);
        p = (p + buf) % a;
        q = (q + buf) % b;
    }
    return ans;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    int T;
    cin >> T;
    while (T --) {
        cin >> n >> a >> b;
        ll p = lcm(a, b);
        cout << (p <= n? n / p * solve(p) : 0) + solve(n % p) << endl;
    }
    return 0;
}
posted @ 2015-08-20 01:33  jklongint  阅读(182)  评论(0编辑  收藏  举报