BZOJ 1968 [Ahoi2005] COMMON Divisor Study

The meaning of the question: Given n, find the sum of the divisors of all numbers from 1 to n

topic link

Consider the contribution of each number to the answer, the contribution of x to the answer is n / x, the complexity is O(n)

It can be optimized to O(√n), and you can fill in the hole when you have time.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<vector>
 7  
 8 using namespace std;
 9 
10 template <typename tn> void read (tn & a) {
11     tn x = 0, f = 1;
12     char c = getchar();
13     while (c < '0' || c > '9'){ if (c == '-') f = -1; c = getchar(); }
14     while (c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar(); }
15     a = f == 1 ? x : -x;
16 }
17 
18 int main() {
19     int n;
20     read(n);
21     long long ans = 0;
22     for (int i = 1; i <= n; ++i) {
23         ans += (long long) (n / i);
24     }
25     printf("%lld\n", ans);
26     return 0;
27 } 
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324522966&siteId=291194637