链接:https://www.luogu.org/problemnew/show/P1403
思路:1~n所有数的约数个数的和等于1的倍数个数和+2的倍数个数和+......所以f(n)=Σ(n/i)
代码:
1 #include<bits/stdc++.h> 2 #define inf 0x3f3f3f3f 3 typedef long long ll; 4 const int M = int(1e5)*2 + 5; 5 using namespace std; 6 7 int a[M]; 8 9 10 int main() 11 { 12 int n; cin >> n; 13 14 /*for (int i = 1; i <= n; i++) //这样写会re 15 a[i] = n / i + a[i - 1]; 16 cout << a[n] << endl;*/ 17 int ans = 0; 18 for (int i = 1; i <= n; i++) 19 ans += n / i; 20 cout << ans << endl; 21 22 return 0; 23 }