NOIP培训Day9 [1]约数研究

约数研究

(num.pas/c/cpp)

时限1s,空间256MB

【题目背景】

F(n)表示n的约数个数,现在给出n,要求求出f(1)到f(n)的总和。

【输入格式】

输入一行,一个整数n

【输出格式】

输出一个整数,表示总和

【样例输入】

3

【样例输出】

5

【数据范围】

20%:N<=5000

100%:N<=1000000

【题解】

这题完全是一道水题!

具体代码实现:(By Tony)

/*
 * Number theory Problem  -  Sum of divisors
 * Code By TonyFang
 * Mail To tony-fang@foxmail.com
 * Copyright TonyFang (in CodeForces) 2013
 * All rights reserved.
 * Copyright TonyFang 2000-2013 (now)
 * All rights reserved.
 * num.cpp   /    input:num.in     /     output:num.out
 */
# include <iostream>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <math.h>
# include <algorithm>
using namespace std;
int main() {
	freopen("num.in","r",stdin);
	freopen("num.out","w",stdout);
	long long n,ans = 0;
	cin >> n;
	for (int i = 1;i <= n / 2;i ++)
		ans += n / i;
	if (n & 1) ans += n / 2 + 1;
	else ans += n / 2;
	cout << ans << endl;
	return 0;
}

 

posted @ 2013-07-19 18:55  TonyFang  阅读(399)  评论(0编辑  收藏  举报