[Luogu p1403] [AHOI2005] about the number of research

Portal

Face questions

Title Description

The scientists on the expedition Samuel planet was rich in energy reserves, which makes large-scale computer Samuel II space station in operation long as possible. Since last year, a year of hard work and achieved good results, small-linking is allowed to study mathematics with Samuel II.

Small problem with the number of recent studies related to peace, his statistics for each positive number \ (N \) the number of divisors of, and to \ (f (N) \) to represent. For example, \ (12 \) divisors are \ (1,2,3,4,6,12 \) , so \ (F (12) =. 6 \) . The following table shows some \ (f (N) \) values:

\(N\) \(1\) \(2\) \(3\) \(4\) \(5\) \(6\)
\(f(N)\) \(1\) \(2\) \(2\) \(3\) \(2\) \(4\)

Now you find:

\[ \sum_{i=1}^n f(i) \]

Input and output formats

Input Format

Enter an integer \ (n-\) .

Output Format

Output answer.

Sample input and output

Input Sample # 1

3

Sample Output # 1

5

Explanation

  • For \ (20 \% \) data, \ (N \ Leq 5000 \) ;
  • For \ (100 \% \) data, \ (. 1 \ Leq N \ Leq. 6 10 ^ \) .

analysis

If pure analog, then, is the simple practice of natural \ (n \) within a few are all seeking again the number of factors. Seeking a number of complexity of the number of factor \ (O (\ sqrt {I}) \) , then this simple approach is \ (O (n \ sqrt { n}) \) level. But we had a better approach.

We use smart brain, we can come up with, we can enumerate \ (i (1 \ Le i \ the n-Le) \) , find the factor \ (i \) in \ (\ 1 \ sim n) appears in how many times. So how many times it appears? No need to enumerate the second floor, the result is \ (\ the n-lfloor / i \ rfloor \) , as to why? You think, ah, a factor \ (i \) in \ (1 \ \ sim n) there have been many times in fact, that is to say \ (n \) Within how many \ (i \) multiples, \ (i \) multiple of each \ (i \) number appears once, the answer of course is \ (\ the n-lfloor / i \ rfloor \) . So we have a \ (O (n) \) approach: seeking
\ [\ sum_ {i = 1
} ^ n \ lfloor n / i \ rfloor \] code implementation is very simple, but in order We paste code:

Code

/*
 * @Author: crab-in-the-northeast 
 * @Date: 2020-02-24 16:52:07 
 * @Last Modified by:   crab-in-the-northeast 
 * @Last Modified time: 2020-02-24 16:53:12 
 */
#include <iostream>
#include <cstdio>

int n;
long long ans;

int main() {
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) ans += n / i;
    printf("%lld\n",ans);
    return 0;
}

Evaluation results

AC 100R31016131

Guess you like

Origin www.cnblogs.com/crab-in-the-northeast/p/luogu-p1403.html