Calculation of (number theory) numbers

Source: Internet
Author: User

Title Description Description

We need to find out the number of characters with the following properties (the natural number of inputs N):

First enter a natural number n (n<=1000), and then treat this natural number as follows:

1. Do not make any treatment;

2. Add a natural number to the left of it, but the natural number cannot exceed half of the original number;

3. After adding a number, continue to follow this rule until no more natural numbers can be added.

Enter a description Input Description

A number n

Output description Output Description

Number of numbers that satisfy the condition

Sample input Sample Input

6

Sample output Sample Output

6

Data range and Tips Data Size & Hint

The 6 numbers were:

6

16

26

126

36

136

Analysis:

1. The difficulty seems to be small, but it takes a lot of time to do it by hand, because the amount of data that needs to be repeated is too large. Of course, we can also take the side of the method of recursive side storage, but the amount of computation is still not small, and then further thinking, in fact, can be used as the following recursive method to do;
2. For example, F (6) is required, after analysis, we know that: f (6) =f (1) +f (2) +f (3) +1, that is, the number of answers to F (6) is the sum of the number of answers to all the natural numbers that can be taken before it (6 can be taken by three natural numbers), The last plus 1 means that the number 6 itself is also an answer;
3. So, we can know F (n) =f (1) +f (2) +......f (Trunc (N/2)) +1;
4. Therefore, F (n) is required, we simply use the above formula to compile a recursive process, the F (2) to f (n) All can be obtained, for F (1000) and no more than 1 seconds to obtain the results.
The second type of algorithm:
1. For f (7) =f (6) is obvious, namely F (2n+1) =f (2n). So what is the relationship between F (8) and F (7)?
2. The analysis shows that the difference between f (8) and F (7) is that F (8), in addition to all cases containing F (7), adds an additional F (4), namely: F (8) =f (7) +f (4). Therefore available: f (2n) =f (2n-1) +f (n). Simply by compiling a recursive small process or by using a recursive method.

1#include <iostream>2 using namespacestd;3 intMain ()4 {5     inti,n,ans,sum[1001];6sum[0] =0, Sum [1] =1;7Cin>>N;8      for(i =2; I <= N; i++)9     {TenAns = sum[i/2] +1; Onesum [i] = sum[i-1] +ans; A     } -Cout<<sum[n]-sum[n-1]<<Endl; -     return 0; the}
View Code

Calculation of (number theory) numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.