Newcoder 39 A.约数个数的和(水~)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/V5ZSQ/article/details/83301756

Description

给个 n n ,求 1 1 n n 的所有数的约数个数的和~

Input

第一行一个正整数 n n

( 1 n 1 0 8 ) (1\le n\le 10^8)

Output

输出一个整数,表示答案

Sample Input

3

Sample Output

5

Solution

a n s = i = 1 n n i ans=\sum\limits_{i=1}^n\lfloor\frac{n}{i}\rfloor ,分块加速或者直接求都行

Code

#include<cstdio>
using namespace std;
typedef long long ll;
int main()
{
	int n;
	scanf("%d",&n);
	ll ans=0;
	for(int i=1,pre;i<=n;i=pre+1)
	{
		pre=n/(n/i);
		ans+=1ll*(n/i)*(pre-i+1);
	}
	printf("%lld\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/V5ZSQ/article/details/83301756