# 反演

# 二项式反演

如果有 :
f(x)=i=0n(1)i(ni)g(i) f(x) = \sum _{i = 0} ^{n} (-1) ^{i} * \binom{n}{i} * g(i)

那么有 :
g(x)=i=0n(1)i(ni)f(i) g(x) = \sum _{i = 0} ^{n} (-1) ^{i} * \binom{n}{i} * f(i)

# 单位根反演

[kn]=1ki=0k1(ωkn)i [k|n] = \frac{1}{k} \sum _{i = 0} ^{k - 1} \left( \omega _{k} ^{n} \right) ^{i}

# 类欧几里得算法

f(a,b,c,n)=i=0nai+bcg(a,b,c,n)=i=0nai+bc2h(a,b,c,n)=i=0niai+bc\begin{aligned} f(a,b,c,n) & = \sum _{i = 0} ^{n} \left\lfloor \frac{ai + b}{c} \right\rfloor \\ g(a,b,c,n) & = \sum _{i = 0} ^{n} \left\lfloor \frac{ai + b}{c} \right\rfloor ^{2} \\ h(a,b,c,n) & = \sum _{i = 0} ^{n} i \ast \left\lfloor \frac{ai + b}{c} \right\rfloor \\ \end{aligned}

# f(a,b,c,n)=i=0nai+bcf(a,b,c,n) = \sum _{i = 0} ^{n} \lfloor \frac{ai + b}{c} \rfloor

(1). 对于 aca \ge cbcb \ge c

f(a,b,c,n)=i=0nai+bc=i=0n(a/cc+a%c)i+b/cc+b%cc=i=0n(a/ci+b/c+(a%c)i+b%cc)=n(n+1)2a/cc+(n+1)b/cc+i=0n(a%c)i+b%cc=n(n+1)2a/cc+(n+1)b/cc+f(a%c,b%c,c,n)\begin{aligned} f(a,b,c,n) & = \sum _{i = 0} ^{n} \left\lfloor \frac{ai + b}{c} \right\rfloor \\ & = \sum _{i = 0} ^{n} \left\lfloor \frac{ ( \lfloor a / c \rfloor c + a \% c ) \, i + \lfloor b / c \rfloor c + b \% c } {c} \right\rfloor \\ & = \sum _{i = 0} ^{n} \left( \lfloor a / c \rfloor i + \lfloor b / c \rfloor + \left\lfloor \frac{ (a \% c) \, i + b \% c } {c} \right\rfloor \right) \\ & = \frac{n \, (n + 1)}{2} * \lfloor a / c \rfloor c + (n + 1) * \lfloor b / c \rfloor c + \sum _{i = 0} ^{n} \left\lfloor \frac{ (a \% c) \, i + b \% c } {c} \right\rfloor \\ & = \frac{n \, (n + 1)}{2} * \lfloor a / c \rfloor c + (n + 1) * \lfloor b / c \rfloor c + f(a \% c,\, b \% c,\, c,\, n) \\ \end{aligned}

(2). 对于 a<ca < cb<cb < c

m=an+bcm=\lfloor \frac{an+b}{c} \rfloor , 则有 :

f(a,b,c,n)=i=0nai+bc=i=0nj=0m1[j<ai+bc]=i=0nj=0m1[j+1ai+bc]=i=0nj=0m1[jc+cbai]=i=0nj=0m1[jc+cb1a<i]=j=0m1i=0n[jc+cb1a<i]=j=0m1(njc+cb1a)=nmf(c,cb1,a,m1)\begin{aligned} \\ f(a,b,c,n) & = \sum _{i = 0} ^{n} \left\lfloor \frac{ai + b}{c} \right\rfloor \\ & = \sum _{i = 0} ^{n} \sum _{j = 0} ^ {m-1} \left[ j < \left\lfloor \frac{ai + b}{c} \right\rfloor \right] \\ & = \sum _{i = 0} ^{n} \sum _{j = 0} ^ {m-1} \left[ j + 1 \leq \frac{ai + b}{c} \right] \\ & = \sum _{i = 0} ^{n} \sum _{j = 0} ^ {m-1} \left[ jc + c - b \leq ai \right] \\ & = \sum _{i = 0} ^{n} \sum _{j = 0} ^ {m-1} \left[ \frac{jc + c - b - 1}{a} < i \right] \\ & = \sum _{j = 0} ^ {m-1} \sum _{i = 0} ^{n} \left[ \frac{jc + c - b - 1}{a} < i \right] \\ & = \sum _{j = 0} ^ {m-1} \left( n - \left\lfloor \frac{jc + c - b - 1}{a} \right\rfloor \right) \\ & = nm - f(c,\, c-b-1,\, a,\, m - 1) \\ \end{aligned}

综上,可得以下式子 :

f(a,b,c,n)={n(n+1)2ac+(n+1)bc+f(a%c,b%c,c,n),ac  or  bcnmf(c,cb1,a,m1),a<c  and  a<c\begin{aligned} f(a,b,c,n) = \begin{cases} \frac{n \, (n + 1)}{2} * \left\lfloor \frac{a}{c} \right\rfloor + (n + 1) * \left\lfloor \frac{b}{c} \right\rfloor + f(a \% c,\, b \% c,\, c,\, n) & , a \ge c \;or\; b \ge c \\ nm - f(c,\, c-b-1,\, a,\, m - 1) & , a < c \;and\; a < c \end{cases} \end{aligned}

# g(a,b,c,n)=i=0nai+bc2g(a,b,c,n) = \sum _{i = 0} ^n \lfloor \frac{ai + b}{c} \rfloor ^2

(1). 对于 aca \ge cbcb \ge c

g(a,b,c,n)=i=0nai+bc2=i=0n(a/c+a%c)i+(b/c+b%c)c2=i=0n(a/ci+b/c+(a%c)i+b%cc)2=i=0n((a/ci+b/c)2+(a%c)i+(b%c)c2+2i(a/c+b/c)(a%c)i+b%cc)=i=0n(a/ci+b/c)2+i=0n(a%c)i+(b%c)c2+i=0n2(a/ci+b/c)(a%c)i+b%cc=i=0na/c2i2+i=0nb/c2+i=0n2ia/cb/c+i=0n(a%c)i+(b%c)c2+i=0n2a/ci(a%c)i+b%cc+i=0n2b/c(a%c)i+b%cc=a/c2i=0ni2+i=0nb/c2+2a/cb/ci=0ni+i=0n(a%c)i+(b%c)c2+2a/ci=0ni(a%c)i+b%cc+2b/ci=0n(a%c)i+b%cc=n(n+1)(n2+1)a/c26+(n+1)b/c2+n(n+1)a/cb/c+g(a%c,b%c,c,n)+2a/ch(a%c,b%c,c,n)+2b/cf(a%c,b%c,c,n)\begin{aligned} g(a,b,c,n) & = \sum _{i = 0} ^{n} \left\lfloor \frac{ai + b}{c} \right\rfloor ^{2} \\ & = \sum _{i = 0} ^{n} \left\lfloor \frac{ ( \lfloor a / c \rfloor + a \% c ) i + ( \lfloor b / c \rfloor + b \% c ) }{c} \right\rfloor ^ {2} \\ & = \sum _{i = 0} ^{n} \left( \lfloor a / c \rfloor i + \lfloor b / c \rfloor + \left\lfloor \frac{ (a \% c) i + b \% c }{c} \right\rfloor \right) ^ {2} \\ & = \sum _{i = 0} ^{n} \left( ( \lfloor a / c \rfloor i + \lfloor b / c \rfloor ) ^{2} + \left\lfloor \frac{ (a \% c) i + (b \% c) }{c} \right\rfloor ^ {2} + 2 i ( \lfloor a / c \rfloor + \lfloor b / c \rfloor ) \left\lfloor \frac{ (a \% c) i + b \% c }{c} \right\rfloor \right) \\ & = \sum _{i = 0} ^{n} ( \lfloor a / c \rfloor i + \lfloor b / c \rfloor ) ^{2} + \sum _{i = 0} ^{n} \left\lfloor \frac{ (a \% c) i + (b \% c) }{c} \right\rfloor ^ {2} + \sum _{i = 0} ^{n} 2 ( \lfloor a / c \rfloor i + \lfloor b / c \rfloor ) \left\lfloor \frac{ (a \% c) i + b \% c }{c} \right\rfloor \\ & = \sum _{i = 0} ^{n} \lfloor a / c \rfloor ^{2} i ^{2} + \sum _{i = 0} ^{n} \lfloor b / c \rfloor ^{2} + \sum _{i = 0} ^{n} 2 i \lfloor a / c \rfloor \lfloor b / c \rfloor + \sum _{i = 0} ^{n} \left\lfloor \frac{ (a \% c) i + (b \% c) }{c} \right\rfloor ^ {2} + \sum _{i = 0} ^{n} 2 \lfloor a / c \rfloor i \left\lfloor \frac{ (a \% c) i + b \% c }{c} \right\rfloor + \sum _{i = 0} ^{n} 2 \lfloor b / c \rfloor \left\lfloor \frac{ (a \% c) i + b \% c }{c} \right\rfloor \\ & = \lfloor a / c \rfloor ^{2} \sum _{i = 0} ^{n} i ^{2} + \sum _{i = 0} ^{n} \lfloor b / c \rfloor ^{2} + 2 \lfloor a / c \rfloor \lfloor b / c \rfloor \sum _{i = 0} ^{n} i + \sum _{i = 0} ^{n} \left\lfloor \frac{ (a \% c) i + (b \% c) }{c} \right\rfloor ^ {2} + 2 \lfloor a / c \rfloor \sum _{i = 0} ^{n} i \left\lfloor \frac{ (a \% c) i + b \% c }{c} \right\rfloor + 2 \lfloor b / c \rfloor \sum _{i = 0} ^{n} \left\lfloor \frac{ (a \% c) i + b \% c }{c} \right\rfloor \\ & = \frac{n (n + 1) (n \ast 2 + 1) \lfloor a / c \rfloor ^{2}} {6} + (n + 1) \lfloor b / c \rfloor ^{2} + n (n+1) \lfloor a / c \rfloor \lfloor b / c \rfloor + g(a \% c,\, b \% c,\, c,\, n) + 2 \lfloor a / c \rfloor h(a \% c,\, b \% c,\, c,\, n) + 2 \lfloor b / c \rfloor f(a \% c,\, b \% c,\, c,\, n) \end{aligned}

(2). 对于 a<ca < cb<cb < c

已知 x2=(2i=1xi)x=(2i=0xi)xx ^{2} = \left( 2 \sum _{i = 1} ^{x} i \right) -x = \left( 2 \sum _{i = 0} ^{x} i \right) -x

m=an+bcm = \lfloor \frac{an + b}{c} \rfloor, 则有 :

g(a,b,c,n)=i=0nai+bc2=2i=0nj=0ai+bcji=0nai+bc=2i=0nj=1mj[  j<ai+bc  ]f(a,b,c,n)=2i=0nj=1mj[  jc+cb1a<i  ]f(a,b,c,n)=2i=0nj=0m1(j+1)[  jc+cb1a<i  ]f(a,b,c,n)=2j=0m1i=0n(j+1)[  jc+cb1a<i  ]f(a,b,c,n)=2j=0m1(j+1)(njc+cb1a)f(a,b,c,n)=2nj=1mj2j=0m1(j+1)jc+cb1af(a,b,c,n)=nm(m+1)2h(c,cb1,a,m1)2f(a,b,c,m1)f(a,b,c,n)\begin{aligned} g(a,b,c,n) & = \sum _{i = 0} ^{n} \left\lfloor \frac{ai + b}{c} \right\rfloor ^{2} \\ & = 2 \sum _{i = 0} ^{n} \sum _{j = 0} ^{ \lfloor \frac{ai + b}{c} \rfloor} j - \sum _{i = 0} ^{n} \left\lfloor \frac{ai + b}{c} \right\rfloor \\ & = 2 \sum _{i = 0} ^{n} \sum _{j = 1} ^{m} j \left[ \; j < \left\lfloor \frac{ai + b}{c} \right\rfloor \; \right] - f(a, b, c, n) \\ & = 2 \sum _{i = 0} ^{n} \sum _{j = 1} ^{m} j \left[ \; \frac{jc + c - b - 1}{a} < i \; \right] - f(a, b, c, n) \\ & = 2 \sum _{i = 0} ^{n} \sum _{j = 0} ^{m-1} (j+1) \left[ \; \frac{jc + c - b - 1}{a} < i \; \right] - f(a, b, c, n) \\ & = 2 \sum _{j = 0} ^{m-1} \sum _{i = 0} ^{n} (j+1) \left[ \; \frac{jc + c - b - 1}{a} < i \; \right] - f(a, b, c, n) \\ & = 2 \sum _{j = 0} ^{m-1} (j+1) \left( n - \left\lfloor \frac{jc + c - b - 1}{a} \right\rfloor \right) - f(a, b, c, n) \\ & = 2 n \sum _{j = 1} ^{m} j - 2 \sum _{j = 0} ^{m-1} (j+1) \left\lfloor \frac{jc + c - b - 1}{a} \right\rfloor - f(a, b, c, n) \\ & = n m (m + 1) - 2 h(c,\, c - b - 1,\, a,\, m - 1) - 2 f(a,\, b,\, c,\, m-1) - f(a, b, c, n) \end{aligned}

综上,可得以下式子 :

g(a,b,c,n)={n(n+1)(n2+1)a/c26+(n+1)b/c2+n(n+1)a/cb/c+g(a%c,b%c,c,n)+2a/ch(a%c,b%c,c,n)+2b/cf(a%c,b%c,c,n)nm(m+1)2h(c,cb1,a,m1)2f(a,b,c,m1)f(a,b,c,n),a<c  and  a<c\begin{aligned} g(a,b,c,n) = \begin{cases} \frac{n (n + 1) (n \ast 2 + 1) \lfloor a / c \rfloor ^{2}} {6} + (n + 1) \lfloor b / c \rfloor ^{2} + n (n+1) \lfloor a / c \rfloor \lfloor b / c \rfloor + g(a \% c,\, b \% c,\, c,\, n) + 2 \lfloor a / c \rfloor h(a \% c,\, b \% c,\, c,\, n) + 2 \lfloor b / c \rfloor f(a \% c,\, b \% c,\, c,\, n) \\ n m (m + 1) - 2 h(c,\, c - b - 1,\, a,\, m - 1) - 2 f(a,\, b,\, c,\, m-1) - f(a, b, c, n) & , a < c \;and\; a < c \end{cases} \end{aligned}

# h(a,b,c,n)=i=0niai+bch(a,b,c,n) = \sum _{i=0}^{n} i \left\lfloor \frac{ai+b}{c} \right\rfloor

(1). 对于 aca \ge cbcb \ge c

h(a,b,c,n)=i=0niai+bc=i=0ni(a/cc+a%c)i+b/cc+b%cc=i=0ni(a/ci+b/c+(a%c)i+b%cc)=i=0ni2a/c+i=0nib/c+i=0ni(a%c)i+b%cc=n(n+1)(n2+1)a/c6+n(n+1)b/c2+h(a%c,b%c,c,n)\begin{aligned} h(a,b,c,n) & = \sum _{i = 0} ^{n} i \ast \left\lfloor \frac{ai + b}{c} \right\rfloor \\ & = \sum _{i = 0} ^{n} i \ast \left\lfloor \frac{ ( \lfloor a / c \rfloor c + a \% c ) \, i + \lfloor b / c \rfloor c + b \% c } {c} \right\rfloor \\ & = \sum _{i = 0} ^{n} i \ast \left( \lfloor a / c \rfloor i + \lfloor b / c \rfloor + \left\lfloor \frac{ (a \% c) \, i + b \% c } {c} \right\rfloor \right) \\ & = \sum _{i = 0} ^{n} i ^{2} \lfloor a / c \rfloor + \sum _{i = 0} ^{n} i \lfloor b / c \rfloor + \sum _{i = 0} ^{n} i \ast \left\lfloor \frac{ (a \% c) \, i + b \% c } {c} \right\rfloor \\ & = \frac{n (n + 1) ( n * 2 +1) \lfloor a / c \rfloor}{6} + \frac{n (n + 1) \lfloor b / c \rfloor}{2} + h(a \% c,\, b \% c,\, c, n) \end{aligned}

(2). 对于 a<ca < cb<cb < c

G=jc+cb1aG = \left\lfloor \frac{jc + c - b - 1 }{a} \right\rfloor, 则有 :

h(a,b,c,n)=i=0niai+bc=i=0nij=0m1[j<ai+bc]=i=0nj=0m1i[jc+cb1a<i]=j=0m1i=0ni[G<i]=j=0m1i=G+1ni=12j=0m1(n+G+1)(nG)=12j=0m1(n2G2+nG)=12j=0m1n212j=0m1G2+12j=0m1n12j=0m1G=12(n2m+nmf(c,cb1,a,m1)g(c,cb1,a,m1))=12nm(n+1)12f(c,cb1,a,m1)12g(c,cb1,a,m1)\begin{aligned} h(a,b,c,n) & = \sum _{i = 0} ^{n} i \ast \left\lfloor \frac{ai + b}{c} \right\rfloor \\ & = \sum _{i = 0} ^{n} i \sum _{j = 0} ^{m - 1} \left[ j < \left\lfloor \frac{ai + b}{c} \right\rfloor \right] \\ & = \sum _{i = 0} ^{n} \sum _{j = 0} ^{m - 1} i \ast \left[ \frac{jc + c - b - 1}{a} < i \right] \\ & = \sum _{j = 0} ^{m - 1} \sum _{i = 0} ^{n} i \ast \left[ G < i \right] \\ & = \sum _{j = 0} ^{m - 1} \sum _{i = G + 1} ^{n} i \\ & = \frac{1}{2} \sum _{j = 0} ^{m - 1} (n + G + 1)(n - G) \\ & = \frac{1}{2} \sum _{j = 0} ^{m - 1} (n ^ {2} - G ^ {2} + n - G) \\ & = \frac{1}{2} \sum _{j = 0} ^{m - 1} n ^ {2} - \frac{1}{2} \sum _{j = 0} ^{m - 1} G ^ {2} + \frac{1}{2} \sum _{j = 0} ^{m - 1} n - \frac{1}{2} \sum _{j = 0} ^{m - 1} G \\ & = \frac{1}{2} \left( n ^ {2} m + n m - f(c,\, c - b - 1,\, a, m - 1) - g(c,\, c - b - 1,\, a, m - 1) \right) \\ & = \frac{1}{2} n m (n + 1) - \frac{1}{2} f(c,\, c - b - 1,\, a, m - 1) - \frac{1}{2} g(c,\, c - b - 1,\, a, m - 1) \\ \end{aligned}

综上,可得以下式子 :

h(a,b,c,n)={n(n+1)(n2+1)a/c6+n(n+1)b/c2+h(a%c,b%c,c,n),ac  or  bc12nm(n+1)12f(c,cb1,a,m1)12g(c,cb1,a,m1),a<c  and  a<c\begin{aligned} h(a,b,c,n) = \begin{cases} \frac{n (n + 1) ( n * 2 +1) \lfloor a / c \rfloor}{6} + \frac{n (n + 1) \lfloor b / c \rfloor}{2} + h(a \% c,\, b \% c,\, c, n) & , a \ge c \;or\; b \ge c \\ \frac{1}{2} n m (n + 1) - \frac{1}{2} f(c,\, c - b - 1,\, a, m - 1) - \frac{1}{2} g(c,\, c - b - 1,\, a, m - 1) & , a < c \;and\; a < c \end{cases} \end{aligned}

# 总结:

f(a,b,c,n)={n(n+1)2ac+(n+1)bc+f(a%c,b%c,c,n),ac  or  bcnmf(c,cb1,a,m1),a<c  and  a<cg(a,b,c,n)={n(n+1)(n2+1)a/c26+(n+1)b/c2+n(n+1)a/cb/c+g(a%c,b%c,c,n)+2a/ch(a%c,b%c,c,n)+2b/cf(a%c,b%c,c,n)nm(m+1)2h(c,cb1,a,m1)2f(a,b,c,m1)f(a,b,c,n),a<c  and  a<ch(a,b,c,n)={n(n+1)(n2+1)a/c6+n(n+1)b/c2+h(a%c,b%c,c,n),ac  or  bc12nm(n+1)12f(c,cb1,a,m1)12g(c,cb1,a,m1),a<c  and  a<c\begin{aligned} f(a,b,c,n) & = \begin{cases} \frac{n \, (n + 1)}{2} * \left\lfloor \frac{a}{c} \right\rfloor + (n + 1) * \left\lfloor \frac{b}{c} \right\rfloor + f(a \% c,\, b \% c,\, c,\, n) & , a \ge c \;or\; b \ge c \\ nm - f(c,\, c-b-1,\, a,\, m - 1) & , a < c \;and\; a < c \end{cases} \\ g(a,b,c,n) & = \begin{cases} \frac{n (n + 1) (n \ast 2 + 1) \lfloor a / c \rfloor ^{2}} {6} + (n + 1) \lfloor b / c \rfloor ^{2} + n (n+1) \lfloor a / c \rfloor \lfloor b / c \rfloor + g(a \% c,\, b \% c,\, c,\, n) + 2 \lfloor a / c \rfloor h(a \% c,\, b \% c,\, c,\, n) + 2 \lfloor b / c \rfloor f(a \% c,\, b \% c,\, c,\, n) \\ n m (m + 1) - 2 h(c,\, c - b - 1,\, a,\, m - 1) - 2 f(a,\, b,\, c,\, m-1) - f(a, b, c, n) & , a < c \;and\; a < c \end{cases} \\ h(a,b,c,n) & = \begin{cases} \frac{n (n + 1) ( n * 2 +1) \lfloor a / c \rfloor}{6} + \frac{n (n + 1) \lfloor b / c \rfloor}{2} + h(a \% c,\, b \% c,\, c, n) & , a \ge c \;or\; b \ge c \\ \frac{1}{2} n m (n + 1) - \frac{1}{2} f(c,\, c - b - 1,\, a, m - 1) - \frac{1}{2} g(c,\, c - b - 1,\, a, m - 1) & , a < c \;and\; a < c \end{cases} \end{aligned}

\Huge

\begin{aligned} \end{aligned}

\begin{aligned} \end{aligned}