"codevs2549/2548" natural number and/or integral solution

Source: Internet
Author: User

Natural numbers and decomposition description

The natural number n is decomposed into the sum of several natural numbers, and the output scheme number.

Input

N, (1≤N≤50)

Output

Number of programmes

Sample Input

5

Sample Output

7

HINT

5 can be divided into

1 1 1) 1 1
1 1 1 2
1 1 3
1 2 2
1 4
2 3
5

Exercises

Solution 1: Full backpack

#include <iostream>#include<cstdio>using namespacestd;intN,ans;intA -]; intMain () {scanf ("%d",&N); a[0] =1;  for(intI=1; i<=n;i++)         for(intj=1; j<=n;j++)        {            if(j>=i) a[j]+ = a[j-i]; } printf ("%d", A[n]);}

Solution 2: Search

x indicates which number is subtracted from the previous step, and rest represents the remaining number.

Get a solution when rest is 0

I:x->rest promise not to repeat.

#include <iostream>#include<cstdio>using namespacestd;intAns,n;voidDfsintXintRS) {    if(rs = =0) {ans++;return; }     for(inti=x;i<=rs;i++) DFS (I,rs-i);}intMain () {scanf ("%d",&N); DFS (1, N); cout<<ans;}

Description of natural number integral solution

The natural number n is decomposed into the product of several natural numbers, and the output scheme number.

Input

Natural number n, (1≤n≤2000000000)

Output

Number of programmes

Sample Input

20

Sample Output

4

HINT

20 can be divided into

20
4 5
2 10
2 2 5

Exercises

As with the above question, when rest equals 1 o'clock, you get an answer.

60 min

#include <iostream>#include<cstdio>using namespacestd;intN,ans;voidDfsintXintRS) {    if(rs = =1) ans++;  for(inti=x;i<=rs;i++)        if(rs% i = =0&& i!=1) DFS (I,rs/i);}intMain () {scanf ("%d",&N); DFS (1, N); printf ("%d", ans);}

The problem data range is too large to be optimized.

When an i is found to make the RS die I=0, a set of solutions are found, and the approximations may continue to decompose

#include <iostream>#include<cstdio>#include<cmath>using namespacestd;intn,ans=1;voidDfsintXintRS) {     for(intI=x;i<=sqrt (RS); i++)        if(rs% i = =0) ans++,dfs (i,rs/i);}intMain () {scanf ("%d",&N); DFS (2, N); printf ("%d", ans);}

"codevs2549/2548" natural number and/or integral solution

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.