题意:给出一个数,求1到n之间的所有数的约数之和。
思路:对于
2
<
=
i
<
=
s
q
r
t
(
n
)
2<=i<=sqrt(n)
2<=i<=sqrt(n),约数i出现的次数是
n
/
i
−
1
n/i-1
n/i−1次,对于
i
>
s
q
r
t
(
n
)
i>sqrt(n)
i>sqrt(n),他将在每次求
2
<
=
i
<
=
s
q
r
t
(
n
)
2<=i<=sqrt(n)
2<=i<=sqrt(n)时,满足条件的会被计算一次。
int main()
{
//freopen("in.txt", "r", stdin);
cin >> t;
int cot = 0;
while (t--)
{
int n;
scanf("%d", &n);
ll ans = 0;
f(i, 2, sqrt(n))
{
ans += (n / i - 1)*i;
if (n / i > sqrt(n))
{
int st = sqrt(n)+1;
int ed = n / i;
ans += (ll)(st + ed)*(ed - st + 1) / 2;
}
}
printf("Case %d: ", ++cot);
cout << ans << endl;
}
return 0;
}