CF1862D 题解
洛谷传送门 & CF 传送门
思路
首先我们来讨论一下最优策略。我们设最终的数字有
我们还要计算
二分的右边界不能直接定
代码
# include <bits/stdc++.h>
typedef long long ll; //不开 ll 见祖宗
using namespace std;
ll t, n, ans, m, l, r, mid;
ll f () {
cin >> n;
m = n * 2;
ans = l = 2, r = 2648956421;
while (l <= r) { //二分
mid = l + r >> 1;
if (mid * (mid - 1) > m)
r = mid - 1;
else
l = mid + 1, ans = mid;
}
return ans + n - ans * (ans - 1) / 2;
}
int main () {
cin >> t;
while (t --)
cout << f () << '\n';
return 0;
}