天天看點

洛谷 P1403 約數研究

連結: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 }      

轉載于:https://www.cnblogs.com/harutomimori/p/10485546.html