bzoj 1968: [Ahoi2005]COMMON 约数研究 (思维题)

1968: [Ahoi2005]COMMON 约数研究

Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 1997  Solved: 1508 [Submit][Status][Discuss]

Description

Input

只有一行一个整数 N(0 < N < 1000000)。

Output

只有一行输出,为整数M,即f(1)到f(N)的累加和。

Sample Input

3

Sample Output

5

HINT

Source

Day2

思路:如果跟着题目的意思走。。。求每个数的约束个数。。。复杂度是不资瓷的。。。

然而因为是求和,我们可以直接考虑,每个因子对答案的贡献。

容易知道,因子x对答案的贡献为n/x

x的范围为1..n

/* ***********************************************
Author :111qqz
Created Time :2016年11月18日 星期五 21时46分37秒
File Name :code/bzoj/1968.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6int main()
 7{
 8	#ifndef  ONLINE_JUDGE 
 9	freopen("code/in.txt","r",stdin);
10  #endif
11	LL n,ans=0;
12	cin>>n;
13	for ( LL i = 1 ; i <= n ; i++) ans = ans + n/i;
14	cout<<ans<<endl;
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}