【清华集训2014】Sum

类欧几里得算法真是厉害

计算奇数的个数即可

$$ cnt = \sum_{d = 1}^{n}{ \lfloor {d \sqrt{r}} \rfloor - 2 \lfloor \frac{\lfloor {d \sqrt{r}} \rfloor}{2} \rfloor}$$

我们实际上要计算的是

$$\sum_{d = 1}^{n}{\lfloor \frac{d(a \sqrt{r} + b)}{c} \rfloor}$$

$$x = \lfloor \frac{(a \sqrt{r} + b)}{c} \rfloor $$

$$b_1 = b - x \times c$$

$$Lim = \lfloor \frac{n(a \sqrt{r} + b_1)}{c} \rfloor$$

\begin{eqnarray} \sum_{d = 1}^{n}{\lfloor \frac{d(a \sqrt{r} + b)}{c} \rfloor}&=& x\frac{n(n + 1)}{2} + \sum_{d = 1}^{n}{\lfloor \frac{d(a \sqrt{r} + b_1)}{c} \rfloor}\nonumber\\ &=&x\frac{n(n + 1)}{2} + \sum_{d = 1}^{n} {\sum_{k = 1}^{\infty}{k <= \frac{d(a \sqrt{r} + b_1)}{c}}}\nonumber\\ &=&x\frac{n(n + 1)}{2} + \sum_{k = 1}^{Lim} {\sum_{d = 1}^{n}{d >= \frac{ck}{a \sqrt{r} + b_1}}}\nonumber\\ &=&x\frac{n(n + 1)}{2} + Lim \times n - \sum_{k = 1}^{Lim}{\lfloor \frac{k(ca\sqrt{r} - cb_1)}{a^2r - b_1^2} \rfloor}\nonumber\ \end{eqnarray}

 

转化成子问题,递归解决

复杂度的话,每次减去整数部分,相当于分子对分母做类似取摸的操作(实数貌似没有定义取摸),每次分子至少减少一半,所以时间复杂度为$O(logn)$

 

posted on 2015-04-24 11:20  Dyzerjet  阅读(452)  评论(0编辑  收藏  举报