求(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);
}