类欧几里得算法
Posted lhm-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类欧几里得算法相关的知识,希望对你有一定的参考价值。
求(sum_{x=0}^{n-1}lfloor frac{ax+b}{c} floor)
设(f(a,b,c,n)=sumlimits_{x=0}^{n-1}lfloor frac{ax+b}{c} floor)
进行化简,得
[ egin{aligned} &f(a,b,c,n) =&sum_{x=0}^{n-1}lfloor frac{ax+b}{c} floor =&sum_{x=0}^{n-1}lfloor frac{(a mod c)x + (a - a mod c)x+(b mod c)+(b - b mod c)}{c} floor =&sum_{x=0}^{n-1}lfloor frac{(a mod c)x + (b mod c)}{c} floor + frac{(a - a mod c)x}{c} + frac{b - b mod c}{c} =&sum_{x=0}^{n-1}lfloor frac{(a mod c)x + (b mod c)}{c} floor + lfloor frac{a}{c} floor x + lfloor frac{b}{c} floor =&frac{n(n-1)}{2}lfloor frac{a}{c} floor + n lfloor frac{b}{c} floor + sum_{x=0}^{n-1}lfloor frac{(a mod c)x + (b mod c)}{c} floor =&frac{n(n-1)}{2}lfloor frac{a}{c} floor + n lfloor frac{b}{c} floor + f(a mod c,b mod c, c, n) end{aligned} ]
(code:)
ll f(ll a,ll b,ll c,ll n)
{
if(n<=0) return 0;
return n*(n-1)/2*(a/c)+n*(b/c)+f(c,(a*n+b)%c,a%c,(a%c*n+b%c)/c);
}
以上是关于类欧几里得算法的主要内容,如果未能解决你的问题,请参考以下文章