- 244's solution
-
P244's Solution
- @ 2026-4-5 11:16:54
注意到内层等于 的因数个数和,而有且只有平方数有奇数个因数,因此原式等于:
$$\sum_{x=1}^{n} \left( \left\lfloor x \right\rfloor \bmod 2 \right)$$按 的奇偶性分类讨论推式子即可。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
long long n;
scanf("%lld", &n);
long long c = sqrtl(n + 0.5);
if (c % 2 == 0) printf("%lld\n", c * (c + 1) / 2);
if (c % 2 == 1) printf("%lld\n", c * (c - 1) / 2 + n - c * c + 1);
}
return 0;
}