[ABC420G] sqrt(n²+n+X) 题解
ToBeDetermined
·
·
题解
这道题应该有很多做法。我的做法是尝试消掉 $n^2$,使得关于 $n$ 只出现了一次项。
$$
\begin{aligned}
&设\ \sqrt{n^2 + n + X} = n + k (k \in \mathbb{Z})\\
&\therefore n^2 + n + X = n^2 + 2nk + k^2\\
&\therefore n + X = 2nk + k^2\\
&\therefore X = k^2 + 2kn - n
\end{aligned}
$$
那么就可以枚举 $k$ 解出 $n$,当 $n$ 是整数是才是合法解。$k$ 是根号级别的,但是为了保险,$k$ 的值域可以开大一点。
[赛时提交记录](https://atcoder.jp/contests/abc420/submissions/68769089),稍微卡了点常,所以写法有点沟槽。应该不卡常就用 set 去重也能过。