# 反演
# 二项式反演
如果有 :
f(x)=∑i=0n(−1)i∗(in)∗g(i)
那么有 :
g(x)=∑i=0n(−1)i∗(in)∗f(i)
# 单位根反演
[k∣n]=k1i=0∑k−1(ωkn)i
# 类欧几里得算法
f(a,b,c,n)g(a,b,c,n)h(a,b,c,n)=i=0∑n⌊cai+b⌋=i=0∑n⌊cai+b⌋2=i=0∑ni∗⌊cai+b⌋
# f(a,b,c,n)=∑i=0n⌊cai+b⌋
(1). 对于 a≥c 或 b≥c 时
f(a,b,c,n)=i=0∑n⌊cai+b⌋=i=0∑n⌊c(⌊a/c⌋c+a%c)i+⌊b/c⌋c+b%c⌋=i=0∑n(⌊a/c⌋i+⌊b/c⌋+⌊c(a%c)i+b%c⌋)=2n(n+1)∗⌊a/c⌋c+(n+1)∗⌊b/c⌋c+i=0∑n⌊c(a%c)i+b%c⌋=2n(n+1)∗⌊a/c⌋c+(n+1)∗⌊b/c⌋c+f(a%c,b%c,c,n)
(2). 对于 a<c 且 b<c 时
记 m=⌊can+b⌋ , 则有 :
f(a,b,c,n)=i=0∑n⌊cai+b⌋=i=0∑nj=0∑m−1[j<⌊cai+b⌋]=i=0∑nj=0∑m−1[j+1≤cai+b]=i=0∑nj=0∑m−1[jc+c−b≤ai]=i=0∑nj=0∑m−1[ajc+c−b−1<i]=j=0∑m−1i=0∑n[ajc+c−b−1<i]=j=0∑m−1(n−⌊ajc+c−b−1⌋)=nm−f(c,c−b−1,a,m−1)
综上,可得以下式子 :
f(a,b,c,n)={2n(n+1)∗⌊ca⌋+(n+1)∗⌊cb⌋+f(a%c,b%c,c,n)nm−f(c,c−b−1,a,m−1),a≥corb≥c,a<canda<c
# g(a,b,c,n)=∑i=0n⌊cai+b⌋2
(1). 对于 a≥c 或 b≥c 时
g(a,b,c,n)=i=0∑n⌊cai+b⌋2=i=0∑n⌊c(⌊a/c⌋+a%c)i+(⌊b/c⌋+b%c)⌋2=i=0∑n(⌊a/c⌋i+⌊b/c⌋+⌊c(a%c)i+b%c⌋)2=i=0∑n((⌊a/c⌋i+⌊b/c⌋)2+⌊c(a%c)i+(b%c)⌋2+2i(⌊a/c⌋+⌊b/c⌋)⌊c(a%c)i+b%c⌋)=i=0∑n(⌊a/c⌋i+⌊b/c⌋)2+i=0∑n⌊c(a%c)i+(b%c)⌋2+i=0∑n2(⌊a/c⌋i+⌊b/c⌋)⌊c(a%c)i+b%c⌋=i=0∑n⌊a/c⌋2i2+i=0∑n⌊b/c⌋2+i=0∑n2i⌊a/c⌋⌊b/c⌋+i=0∑n⌊c(a%c)i+(b%c)⌋2+i=0∑n2⌊a/c⌋i⌊c(a%c)i+b%c⌋+i=0∑n2⌊b/c⌋⌊c(a%c)i+b%c⌋=⌊a/c⌋2i=0∑ni2+i=0∑n⌊b/c⌋2+2⌊a/c⌋⌊b/c⌋i=0∑ni+i=0∑n⌊c(a%c)i+(b%c)⌋2+2⌊a/c⌋i=0∑ni⌊c(a%c)i+b%c⌋+2⌊b/c⌋i=0∑n⌊c(a%c)i+b%c⌋=6n(n+1)(n∗2+1)⌊a/c⌋2+(n+1)⌊b/c⌋2+n(n+1)⌊a/c⌋⌊b/c⌋+g(a%c,b%c,c,n)+2⌊a/c⌋h(a%c,b%c,c,n)+2⌊b/c⌋f(a%c,b%c,c,n)
(2). 对于 a<c 且 b<c 时
已知 x2=(2∑i=1xi)−x=(2∑i=0xi)−x
记 m=⌊can+b⌋, 则有 :
g(a,b,c,n)=i=0∑n⌊cai+b⌋2=2i=0∑nj=0∑⌊cai+b⌋j−i=0∑n⌊cai+b⌋=2i=0∑nj=1∑mj[j<⌊cai+b⌋]−f(a,b,c,n)=2i=0∑nj=1∑mj[ajc+c−b−1<i]−f(a,b,c,n)=2i=0∑nj=0∑m−1(j+1)[ajc+c−b−1<i]−f(a,b,c,n)=2j=0∑m−1i=0∑n(j+1)[ajc+c−b−1<i]−f(a,b,c,n)=2j=0∑m−1(j+1)(n−⌊ajc+c−b−1⌋)−f(a,b,c,n)=2nj=1∑mj−2j=0∑m−1(j+1)⌊ajc+c−b−1⌋−f(a,b,c,n)=nm(m+1)−2h(c,c−b−1,a,m−1)−2f(a,b,c,m−1)−f(a,b,c,n)
综上,可得以下式子 :
g(a,b,c,n)={6n(n+1)(n∗2+1)⌊a/c⌋2+(n+1)⌊b/c⌋2+n(n+1)⌊a/c⌋⌊b/c⌋+g(a%c,b%c,c,n)+2⌊a/c⌋h(a%c,b%c,c,n)+2⌊b/c⌋f(a%c,b%c,c,n)nm(m+1)−2h(c,c−b−1,a,m−1)−2f(a,b,c,m−1)−f(a,b,c,n),a<canda<c
# h(a,b,c,n)=∑i=0ni⌊cai+b⌋
(1). 对于 a≥c 或 b≥c 时
h(a,b,c,n)=i=0∑ni∗⌊cai+b⌋=i=0∑ni∗⌊c(⌊a/c⌋c+a%c)i+⌊b/c⌋c+b%c⌋=i=0∑ni∗(⌊a/c⌋i+⌊b/c⌋+⌊c(a%c)i+b%c⌋)=i=0∑ni2⌊a/c⌋+i=0∑ni⌊b/c⌋+i=0∑ni∗⌊c(a%c)i+b%c⌋=6n(n+1)(n∗2+1)⌊a/c⌋+2n(n+1)⌊b/c⌋+h(a%c,b%c,c,n)
(2). 对于 a<c 且 b<c 时
记 G=⌊ajc+c−b−1⌋, 则有 :
h(a,b,c,n)=i=0∑ni∗⌊cai+b⌋=i=0∑nij=0∑m−1[j<⌊cai+b⌋]=i=0∑nj=0∑m−1i∗[ajc+c−b−1<i]=j=0∑m−1i=0∑ni∗[G<i]=j=0∑m−1i=G+1∑ni=21j=0∑m−1(n+G+1)(n−G)=21j=0∑m−1(n2−G2+n−G)=21j=0∑m−1n2−21j=0∑m−1G2+21j=0∑m−1n−21j=0∑m−1G=21(n2m+nm−f(c,c−b−1,a,m−1)−g(c,c−b−1,a,m−1))=21nm(n+1)−21f(c,c−b−1,a,m−1)−21g(c,c−b−1,a,m−1)
综上,可得以下式子 :
h(a,b,c,n)={6n(n+1)(n∗2+1)⌊a/c⌋+2n(n+1)⌊b/c⌋+h(a%c,b%c,c,n)21nm(n+1)−21f(c,c−b−1,a,m−1)−21g(c,c−b−1,a,m−1),a≥corb≥c,a<canda<c
# 总结:
f(a,b,c,n)g(a,b,c,n)h(a,b,c,n)={2n(n+1)∗⌊ca⌋+(n+1)∗⌊cb⌋+f(a%c,b%c,c,n)nm−f(c,c−b−1,a,m−1),a≥corb≥c,a<canda<c={6n(n+1)(n∗2+1)⌊a/c⌋2+(n+1)⌊b/c⌋2+n(n+1)⌊a/c⌋⌊b/c⌋+g(a%c,b%c,c,n)+2⌊a/c⌋h(a%c,b%c,c,n)+2⌊b/c⌋f(a%c,b%c,c,n)nm(m+1)−2h(c,c−b−1,a,m−1)−2f(a,b,c,m−1)−f(a,b,c,n),a<canda<c={6n(n+1)(n∗2+1)⌊a/c⌋+2n(n+1)⌊b/c⌋+h(a%c,b%c,c,n)21nm(n+1)−21f(c,c−b−1,a,m−1)−21g(c,c−b−1,a,m−1),a≥corb≥c,a<canda<c