# 【LG5171Earthquake】

## 题解

### 前置知识：类欧几里得

$\xi(i)=\lfloor\frac {ai+b}c\rfloor$

\begin{aligned} \xi(i)&=\left\lfloor\frac{ai}c+\frac bc\right\rfloor \\&=\left\lfloor\frac{(a\bmod c)i+(b\bmod c)} c\right\rfloor+i\Big\lfloor\frac ac\Big\rfloor+\Big\lfloor\frac bc\Big\rfloor \end{aligned}

\begin{aligned} \varphi(a,b,c,n)&=\sum_{i=0}^n\sum_{d=1}^{\lfloor\frac {an+b}c\rfloor}\left[\lfloor\frac {ai+b}c\rfloor\geq d\right]\\&=\sum_{i=0}^n\sum_{d=0}^{\lfloor\frac {an+b}c\rfloor-1}\left[a^{-1}c\lfloor\frac {ai+b}c\rfloor\geq a^{-1}c(d+1)>a^{-1}(cd+c-1)\right]\\&=\sum_{i=0}^n\sum_{d=0}^{\lfloor\frac {an+b}c\rfloor-1}\left[i>\frac {cd+c-b-1}{a}\right] \end{aligned}

\begin{aligned} \varphi(a,b,c,n)&=\sum_{d=0}^{\lfloor\frac {an+b}c\rfloor-1}(n-\left\lfloor\frac {cd+c-b-1}{a}\right\rfloor)\\ &=n\left\lfloor\frac {an+b}c\right\rfloor-\sum_{d=0}^{\lfloor\frac {an+b}c\rfloor-1}\left\lfloor\frac {cd+c-b-1}{a}\right\rfloor\\ &=n\left\lfloor\frac {an+b}c\right\rfloor-\varphi(c,c-b-1,a,\left\lfloor\frac {an+b}c\right\rfloor-1)\\ &=n\left\lfloor\frac {an+b}c\right\rfloor-\varphi(c,c-b-1,a,\xi(n)-1) \end{aligned}

long long f(long long a, long long b, long long c, long long n) {
if (!a) return b / c * (n + 1);
else if (a >= c || b >= c) return f(a % c, b % c, c, n) + n * (n + 1) / 2 * (a / c) + (n + 1) * (b / c);
else {
long long m = (a * n + b) / c;
return n * m - f(c, c - b - 1, a, m - 1);
}
}


### 关于此题

$ax+by-c\leq 0$

$n=\lfloor\frac ca\rfloor$，那么两端的点就为$(0,\frac cb),(n,\frac {c-an}b)$

## 代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
long long a, b, c;
long long f(long long a, long long b, long long c, long long n) {
if (!a) return b / c * (n + 1);
else if (a >= c || b >= c) return f(a % c, b % c, c, n) + n * (n + 1) / 2 * (a / c) + (n + 1) * (b / c);
else {
long long m = (a * n + b) / c;
return n * m - f(c, c - b - 1, a, m - 1);
}
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
cin >> a >> b >> c;
printf("%lld\n", f(a, c % a, b, c / a) + c / a + 1);
return 0;
}

posted @ 2019-11-06 12:00  heyujun  阅读(68)  评论(2编辑  收藏