P1403 [AHOI2005]约数研究

P1403 [AHOI2005]约数研究

\(f(x) = \sum_{d | x}{1}\), 求 \(\sum_{i = 1}^{N}f(x)\)\(\sum_{i=1}^{N} \sum_{d | x}{1}\)

Solution

考虑枚举约数 \(d\) , 发现 \(d\)\(1-N\) 中出现 \(\lfloor \frac{N}{d} \rfloor\)
所以答案就为: $$\sum_{d = 1}^{N}\lfloor \frac{N}{d} \rfloor$$
除法分块优化, 复杂度 \(O(\sqrt{N})\)

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
typedef long long LL;
using namespace std;
LL RD(){
    LL out = 0,flag = 1;char c = getchar();
    while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
    while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
    return flag * out;
    }
LL num;
LL get_num(LL n){
	LL ans = 0;
	for(LL l = 1, r;l <= n;l = r + 1){
		r = n / (n / l);
		ans += (n / l) * (r - l + 1);
		}
	return ans;
	}
int main(){
	num = RD();
	printf("%lld\n", get_num(num));
	return 0;
	}
posted @ 2018-08-20 20:43  Tony_Double_Sky  阅读(126)  评论(0编辑  收藏  举报