HDU 1028 Ignatius and the princess III (Integer Division)

Source: Internet
Author: User
Tags integer division

Ignatius and the princess III Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 12731 accepted submission (s): 8999


Problem description "Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.

"The second problem is, given an positive integer N, we define an equation like this:
N = A [1] + A [2] + A [3] +... + A [m];
A [I]> 0, 1 <= m <= N;
My question is how many different equations you can find for a given n.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
So the result is 5 when n is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it! "
 
Inputthe input contains several test cases. Each test case contains a positive integer N (1 <= n <= 120) which is mentioned above. The input is terminated by the end of file.
 
Outputfor each test case, you have to output a line contains an integer p which indicate the different equations you have found.
 
Sample Input
41020
 
Sample output
542627
 


The integer division problem is one of the classical propositions in the algorithm. The explanation of this problem involves the basics of recursion. The so-called Integer Division refers to writing a positive integer N as follows:
N = m1 + M2 +... + Mi; (where Mi is a positive integer and 1 <= mi <= N), then {M1, M2 ,..., mi} is a division of N.
If {M1, M2 ,..., the maximum value in mi} cannot exceed M, that is, Max (M1, M2 ,..., mi) <= m, it is called an M division of N. Here we will record the number of M divisions of N as F (n, m );
For example, when n = 4, there are five divisions: {4}, {3, 1}, {2, 2}, {2, 1}, {1, 1 };
Note that 4 = 1 + 3 and 4 = 3 + 1 are considered to be the same division.
This problem is used to obtain the number of all N partitions, that is, F (N, N ). Next we will consider the F (n, m) method;
1. recursion:
Based on the relationship between N and M, consider the following situations:
(1) When n = 1, no matter how much m is (M> 0), there is only one division: {1 };
(2) When M = 1, no matter how many N values are, there is only one division of N 1, {, 1,..., 1 };
(3) When n = m, there are two possible conditions:
(A). If the Division contains N, only one is {n };
(B) If n is not included in the Division, the maximum number in the Division must be smaller than N, that is, all (n-1) Division of N.
Therefore, F (n, n) = 1 + f (n, n-1 );
(4) When n <m, it is equivalent to F (n, n) because it cannot be a negative number in the Division );
(5) but n> M, whether or not the maximum M is included in the Division can be divided into two situations:
(). The Division contains M, that is, {M, {x1, x2 ,... XI }}, {x1, x2 ,... the sum of Xi} is n-M, so in this case
F (n-M, m)
(B) if the Division does not contain M, all values in the Division are smaller than m, that is, the division of n (m-1), and the number is F (m-1 );
Therefore, F (n, m) = f (n-M, m) + f (m-1 );
To sum up:
F (n, m) = 1; (n = 1 or M = 1)
F (n, n); (n <m)
1 + f (N m-1); (n = m)
F (n-M, m) + f (m-1); (n> m)

However, recursion usually times out, so you can use DP to solve the problem. (This part is excerpted from: http://blog.csdn.net/niushuai666/article/details/6600977)

#include"stdio.h"#include"string.h"#include"math.h"#include"queue"using namespace std;#define N 121#define M 1000int f[N][N];/*int fun(int n,int m){    if(n==1||m==1) return 1;    else if(n>m) return fun(n-m,m)+fun(n,m-1);    else if(n==m) return 1+fun(n,m-1);    else if(n<m) return fun(n,n);}*/int main(){    int n,i,j;    f[1][1]=1;    for(i=1;i<N;i++)    {        for(j=1;j<N;j++)        {            if(i==1||j==1)                f[i][j]=1;            else if(i<j)                f[i][j]=f[i][i];            else if(i==j)                f[i][j]=1+f[i][j-1];            else if(i>j)                f[i][j]=f[i-j][j]+f[i][j-1];        }    }    while(~scanf("%d",&n))    {        printf("%d\n",f[n][n]);    }    return 0;}



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.