前言
每次看了很快就忘了,干脆写一篇博客,来加深记忆。
定义
设
[f(a,b,c,n)=sum_{i=0}^{n}lfloordfrac{ai+b}{c}
floor
]
[g(a,b,c,n)=sum_{i=0}^{n}ilfloordfrac{ai+b}{c}
floor
]
[g(a,b,c,n)=sum_{i=0}^{n}{lfloordfrac{ai+b}{c}
floor}^2
]
[m=lfloordfrac{an+b}{c}
floor
]
一、f(a,b,c,n)
当a>=c时,(=f(a\%c,b,c,n)+lfloordfrac{a}{c}
floor*n(n+1)/2)
当b>=c时,(=f(a,b\%c,c,n)+lfloordfrac{b}{c}
floor*(n+1))
然后
[=sum_{i=0}^{n}lfloordfrac{ai+b}{c}
floor
]
我们将(dfrac{ai+b}{c})当作一条以i为自变量的直线,
于是原式就等于这个直角梯形内的整点个数,
[=sum_{i=0}^{n}sum_{j=1}^{m}[lfloordfrac{ai+b}{c}
floor>=j]
]
[=sum_{i=0}^{n}sum_{j=0}^{m-1}[lfloordfrac{ai+b}{c}
floor>=j+1]
]
[=sum_{i=0}^{n}sum_{j=0}^{m-1}[ai+b>=jc+c]
]
[=sum_{i=0}^{n}sum_{j=0}^{m-1}[ai+b>jc+c-1]
]
[=sum_{i=0}^{n}sum_{j=0}^{m-1}[ai>jc+c-b-1]
]
[=sum_{i=0}^{n}sum_{j=0}^{m-1}[i>lfloordfrac{jc+c-b-1}{a}
floor]
]
[=sum_{j=0}^{m-1}sum_{i=0}^{n}[i>lfloordfrac{jc+c-b-1}{a}
floor]
]
[=sum_{j=0}^{m-1}(n-sum_{i=0}^{n}[i<=lfloordfrac{jc+c-b-1}{a}
floor])
]
[=nm-f(c,c-b-1,a,m-1)
]
时间复杂度类似与扩展欧几里得。
二、g(a,b,c,n)
//坑