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