# BZOJ2987：Earthquake(类欧几里德算法)

## Sol

$n=\lfloor\frac{c}{a}\rfloor$

$\sum_{i=0}^{n}\lfloor\frac{c-ax}{b}\rfloor+1=\sum_{i=0}^{n}\lfloor\frac{-ax+b+c}{b}\rfloor$

$f(a,b,c,n)=\sum_{i=0}^{n}\lfloor\frac{ax+b}{c}\rfloor，c\ne 0$

1. $c\le 0$，那么 $f(a,b,c,n)=f(-a,-b,-c,n)$
2. $a<0$$b<0$，那么

$f(a,b,c,n)=f(a~mod~c + c, b~mod~c + c, c, n) + \frac{n(n + 1)}{2} (\lfloor\frac{a}{c}\rfloor - 1) + (n + 1)(\lfloor\frac{b}{c}\rfloor - 1)$

1. $a>=c$$b>=c$，那么

$f(a,b,c,n)=f(a~mod~c, b~mod~c, c, n) + \frac{n(n + 1)}{2}\lfloor\frac{a}{c}\rfloor + (n + 1)\lfloor\frac{b}{c}\rfloor$

1. 最后 $0\le a<c$$0\le b<c$
$m=\lfloor\frac{an+b}{c}\rfloor$
那么

$\sum_{i=0}^{n}\lfloor\frac{ai+b}{c}\rfloor=\sum_{i=0}^{n}\sum_{j=1}^{m}[\lfloor\frac{ai+b}{c}\rfloor\ge j]=\sum_{i=0}^{n}\sum_{j=0}^{m-1}[\lfloor\frac{ai+b}{c}\rfloor\ge j+1]$

$=\sum_{i=0}^{n}\sum_{j=0}^{m-1}[ai\ge cj+c-b]=\sum_{i=0}^{n}\sum_{j=0}^{m-1}[ai> cj+c-b-1]$

$=\sum_{i=0}^{n}\sum_{j=0}^{m-1}[i> \lfloor\frac{cj+c-b-1}{a}\rfloor]=\sum_{i=0}^{m-1}(n-\lfloor\frac{ci+c-b-1}{a}\rfloor)$

$=nm-\sum_{i=0}^{m-1}\lfloor\frac{ci+c-b-1}{a}\rfloor=nm-f(c,c-b-a,a,m-1)$

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;

inline ll Gcd(ll x, ll y) {
if (!x || !y) return x + y;
return !y ? x : Gcd(y, x % y);
}

inline ll Solve(ll a, ll b, ll c, ll n) {
if (!a) return (n + 1) * (b / c);
if (!n) return b / c;
if (n == 1) return (a + b) / c + b / c;
if (c < 0) return Solve(-a, -b, -c, n);
register ll d = abs(Gcd(Gcd(a, b), c));
a /= d, b /= d, c /= d;
if (a >= c || b >= c) return Solve(a % c, b % c, c, n) + n * (n + 1) / 2 * (a / c) + (n + 1) * (b / c);
if (a < 0 || b < 0) return Solve(a % c + c, b % c + c, c, n) + n * (n + 1) / 2 * (a / c - 1) + (n + 1) * (b / c - 1);
register ll m = (a * n + b) / c;
return n * m - Solve(c, c - b - 1, a, m - 1);
}

ll a, b, c, n;

int main() {
scanf("%lld%lld%lld", &a, &b, &c), n = c / a;
printf("%lld\n", Solve(-a, c + b, b, n));
return 0;
}

posted @ 2018-12-09 16:03  Cyhlnj  阅读(292)  评论(0编辑  收藏  举报