版本比较
FRT 搭积木 I 题解
正文
增加 6 行删除 6 行
123345676 行未改动141516171819201819202122232425262728292829303132332 行未改动## 题目分析### 友情提醒因为 $0\le n<2^{63},0<k<2^{63}$, 因为 $0\le n<2^{63},0<k<2^{63}$,所以开 `unsigned long long`。
有时精度问题会导致答案多 $1$,比如 $\sqrt{10^{12}-1}=999999.9999995\dots\approx10^6$,向下取整为 $10^6$,但实际为 $10^6-1$。
6 行未改动### $\texttt t$ 部分- 中值换元法>时间复杂度 $\Theta(1)$。>>令未知数 $\Delta,\delta$, >$1+2+\dots+\Delta=n$, >$\delta=\Delta+\dfrac{1}{2}$, >令未知数 $\Delta,\delta$,>$1+2+\dots+\Delta=n$,>$\delta=\Delta+\dfrac{1}{2}$,>则 $ans=\lfloor\Delta\rfloor$。>>$$\begin{aligned}1+2+\dots+\Delta&=n\\\frac{\Delta(\Delta+1)}{2}&=n\\\Delta(\Delta+1)&=2n\\\left(\delta-\frac{1}{2}\right)\left(\delta+\frac{1}{2}\right)&=2n\\\delta^2-\left(\frac{1}{2}\right)^2&=2n\\\delta^2-\frac{1}{4}&=2n\\\delta^2&=2n+\frac{1}{4}\\\because\delta&>0\\\therefore\delta&=\sqrt{2n+\frac{1}{4}}\\\Delta+\frac{1}{2}&=\sqrt{2n+\frac{1}{4}}\\\Delta&=\sqrt{2n+\frac{1}{4}}-\frac{1}{2}\\ans&=\left\lfloor\sqrt{2n+\frac{1}{4}}-\frac{1}{2}\right\rfloor\end{aligned}$$
- 解二元一次方程>时间复杂度 $\Theta(1)$。>>令未知数 $x$, >$1+2+\dots+x=n$, >令未知数 $x$,>$1+2+\dots+x=n$,>则 $ans=\lfloor x\rfloor$。>>$$\begin{aligned}1+2+\dots+x&=n\\\frac{x(x+1)}{2}&=n\\x(x+1)&=2n\\x^2+x&=2n\\x^2+x+(-2n)&=0\\x&=\frac{-1\pm\sqrt{1^2-4\times1\times(-2n)}}{2\times1}\\x&=\frac{-1\pm\sqrt{1+8n}}{2}\\\because x&\ge0\\\therefore x&=\frac{\sqrt{1+8n}-1}{2}\\x&=\sqrt{\frac{1+8n}{4}}-\frac{1}{2}\\x&=\sqrt{\frac{1}{4}+2n}-\frac{1}{2}\\ans&=\left\lfloor\sqrt{2n+\frac{1}{4}}-\frac{1}{2}\right\rfloor\end{aligned}$$
2 行未改动