2987: Earthquake
将式子移项得到 y ≤ − A x + C B y\le \frac{-Ax+C}{B} y≤B−Ax+C
答案就是 ∑ x = 0 n [ − A x + C B + 1 ] \sum_{x=0}^{n} [\frac{-Ax+C}{B}+1] ∑x=0n[B−Ax+C+1]
设 f ( n , A , B , C ) = ∑ x = 0 n [ − A x + C B + 1 ] f(n,A,B,C)=\sum_{x=0}^{n} [\frac{-Ax+C}{B}+1] f(n,A,B,C)=∑x=0n[B−Ax+C+1]
当 B ≤ A B \le A B≤A|| B ≤ C B \le C B≤C
f ( n , A , B , C ) = f ( n , A % B , B , C % B ) + [ A / B ] ( n + 1 ) n 2 + [ C / B ] ( n + 1 ) f(n,A,B,C)=f(n,A\%B,B,C\%B)+[A/B]\frac{(n+1)n}{2}+[C/B](n+1) f(n,A,B,C)=f(n,A%B,B,C%B)+[A/B]2(n+1)n+[C/B](n+1)
当 C ≤ 0 C\le 0 C≤0|| A ≤ 0 A \le 0 A≤0
f ( n , A , B , C ) = f ( n , A % B + B , B , C % B + B ) + [ A / B − 1 ] ( n + 1 ) n 2 + [ C / B ] ( n + 1 ) f(n,A,B,C)=f(n,A\%B+B,B,C\%B+B)+[A/B-1]\frac{(n+1)n}{2}+[C/B](n+1) f(n,A,B,C)=f(n,A%B+B,B,C%B+B)+[A/B−1]2(n+1)n+[C/B](n+1)
#include<cstdio>
using namespace std;
typedef long long LL;
LL a,b,c,n;
LL Solve(LL n,LL A,LL B,LL C){
if(!B) return 0;
if(A<0||C<0) return (A/B-1)*n*(n+1)/2+(C/B-1)*(n+1)+Solve(n,A%B+B,B,C%B+B);
if(A>=B||C>=B) return A/B*n*(n+1)/2+C/B*(n+1)+Solve(n,A%B,B,C%B);
int kmax=(A*n+C)/B;
return n*kmax-Solve(kmax-1,B,A,B-C-1);
}
int main(){
scanf("%lld%lld%lld",&a,&b,&c);
printf("%lld\n",Solve(c/a,-a,b,c+b));
return 0;
}