洛谷P1403约数和(数学公式,找规律)

题目链接:https://www.luogu.org/problemnew/show/P1403#sub

 

这题难点以及关键是找到,f(i)=n/i。

每个f(i)单个看很难找到规律,但求和时就会发现,f(i)=n/i。自己动手模拟下就明白。

很简单的数学常识嘛:

1-n的因子个数,可以看成共含有2因子的数的个数+含有3因子的数的个数……+含有n因子的数的个数

但在1~n中含有“2”这个因子的数有n/2个,3有n/3个,以此类推,公式就出来了

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <iomanip>
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <cmath>
 8 using namespace std;
 9 typedef long long ll;
10 typedef unsigned long long ull;
11 const int maxn=1e6+5;
12 int a[maxn];
13 int n;
14 
15 int main()
16 {
17     ios::sync_with_stdio(false); cin.tie(0);
18 
19     cin>>n;
20 
21     int s=0;
22     for(int i=1;i<=n;i++)
23     {
24         s+=n/i;
25     }
26 
27     cout<<s<<endl;
28 
29     return 0;
30 }

 

完。

posted @ 2018-10-16 10:36  RedBlack  阅读(262)  评论(0编辑  收藏  举报