# 基本定义

f(a,b,c,n)=ni=0ai+bc$f(a,b,c,n)=\sum_{i=0}^n\lfloor\frac{ai+b}{c}\rfloor$
g(a,b,c,n)=ni=0iai+bc$g(a,b,c,n)=\sum_{i=0}^ni\lfloor\frac{ai+b}{c}\rfloor$
h(a,b,c,n)=ni=0ai+bc2$h(a,b,c,n)=\sum_{i=0}^n\lfloor\frac{ai+b}{c}\rfloor^2$
m=an+bc$m=\lfloor\frac{an+b}{c}\rfloor$

# 推f

f(a,b,c,n)=(a/c)n(n+1)/2+(b/c)(n+1)+f(a%c,b%c,c,n)$f(a,b,c,n)=(a/c)*n*(n+1)/2+(b/c)*(n+1)+f(a\%c,b\%c,c,n)$

f(a,b,c,n)=ni=0mj=1[(ai+b)/c>=j]$f(a,b,c,n)=\sum_{i=0}^n\sum_{j=1}^m[(ai+b)/c>=j]$

f的式子的意义其实是一条倾斜直线与x=0和x=n两条铅垂线组成的直角梯形内整点数量（不包括纵坐标为0的点，包括在线上的点）。

f(a,b,c,n)=ni=0m1j=0[(ai+b)/c>=j+1]$f(a,b,c,n)=\sum_{i=0}^n\sum_{j=0}^{m-1}[(ai+b)/c>=j+1]$
f(a,b,c,n)=ni=0m1j=0[ai>=cj+cb]$f(a,b,c,n)=\sum_{i=0}^n\sum_{j=0}^{m-1}[ai>=cj+c-b]$
f(a,b,c,n)=ni=0m1j=0[ai>cj+cb1]$f(a,b,c,n)=\sum_{i=0}^n\sum_{j=0}^{m-1}[ai>cj+c-b-1]$
f(a,b,c,n)=ni=0m1j=0[i>(cj+cb1)/a]$f(a,b,c,n)=\sum_{i=0}^n\sum_{j=0}^{m-1}[i>(cj+c-b-1)/a]$
f(a,b,c,n)=mj=0(n(cj+cb1)/a)$f(a,b,c,n)=\sum_{j=0}^m(n-(cj+c-b-1)/a)$
f(a,b,c,n)=nmf(c,cb1,a,m1)$f(a,b,c,n)=nm-f(c,c-b-1,a,m-1)$

# 推g

a>=c或b>=c的时候，容易得到
g(a,b,c,n)=(a/c)n(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