4117: simple integer division problems (dynamic programming)

 

Total time limit: 
100ms
 
Memory Limit: 
65536kB
description

The positive integer n represented as a series of positive and integer, n = n1 + n2 + ... + nk, where n1> = n2> = ...> = nk> = 1, k> = 1.
This represents a positive integer n is a positive integer n is called partitioning. Dividing the number of different positive integer n is called the division number n, a positive integer.

Entry
Standard test input comprising a plurality of sets of data. Each test is an integer N (0 <N <= 50).
Export
Data, the number of divisions N of the output for each test.
Sample input
5
Sample Output
7
prompt
5, 4+1, 3+2, 3+1+1, 2+2+1, 2+1+1+1, 1+1+1+1+1
1  @ solving ideas: knapsack problem completely typical dynamic planning
 2  // If the inputs to 5, which can be considered as comprising five items, value respectively 1,2,3,4,5
 3  / / each item can be selected multiple times, value will eventually have accumulated a selected number of articles 5
 . 4  // can use a one-dimensional array scroll
 5  // state transition equation: dp [j] = dp [ ji] + dp [ J] 
. 6 #include <bits / STDC ++ H.>
 . 7  the using  namespace STD;
 . 8  int main () {
 . 9      int N;
 10      the while (CIN >> N) {
 . 11          int DP [ 1010 ];
 12 is          Memset (DP, 0 , the sizeof (DP));
 13 is         dp[0] = 1;
14         for(int i=1; i<=N; ++i)
15             for (int j = i; j <= N; ++j) {
16                 dp[j] = dp[j - i] + dp[j];
17             }
18         cout << dp[N] << endl;
19     }
20     return 0;
21 }

 

Guess you like

Origin www.cnblogs.com/aiqinger/p/12601646.html