HDU 1028 Ignatius and the Princess III "integer division"

Source: Internet
Author: User

Ignatius and the Princess III Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 15730 Accepted Submission (s): 11092
Links: Hdu 1028
Problem Description "Well, it seems the first problem are too easy. I'll let you know how foolish is later. "feng5166 says.

"The second problem is, given an positive integer N, we define a equation like this:
N=A[1]+A[2]+A[3]+...+A[M];
a[i]>0,1<=m<=n;
My question is what 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;
The result is 5 while N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" are the same in this problem. Now, do it! "
Inputthe input contains several test cases. Each test case contains a positive an integer N (1<=n<=120) which is mentioned above. The input is terminated by the end of file.
Outputfor each of the test case and you had to output a line contains an integer P which indicate the different equations you had Found.
Sample Input
41020
Sample Output
542627Test Instructions

Test instructions: The number of forms in which N is divided into the number of 1~n in a naked integer division.

Analysis

There are many ways to solve the problem of integer partitioning, there are DP solutions, and a complete backpack solution. Here only a recursive solution is given, using dp[n][m] to represent the largest number of M in the division of integer n, then there is dp[n][m] =

Dp[n, m]= 1; (N=1 or M=1)

Dp[n, n]; (n<m)

1+ dp[n, m-1]; (n=m)

DP[N-M,M]+DP[N,M-1]; (n>m)

Here are links to other practices of Daniel:

Female function, recursive full backpack

Reference Code

 #include <cmath> #include <queue> #include <vector> #include < cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm>using namespace std;//#pragma comment (linker, "/stack:1024000000,1024000000") #define FIN Freo Pen ("Input.txt", "R", stdin) #define FOUT freopen ("Output.txt", "w", stdout) #define CASE (T) for (scanf ("%d", & Amp T);    t--;) const int MAXN = + 5;int dp[maxn][maxn];int dfs (int n, int m) {if (dp[n][m]! =-1) return dp[n][m];    if (N < 1 | | m < 1) return dp[n][m] = 0;    if (n = = 1 | | m = = 1) return dp[n][m] = 1;    if (n < m) return dp[n][m] = DFS (n, N);    if (n = = m) return dp[n][m] = DFS (n, m-1) + 1; return dp[n][m] = DFS (n, m-1) + DFS (n-m, m);}    int main () {//FIN;    int N;    while (~SCANF ("%d", &n)) {printf ("%d\n", DFS (n, n)); } return 0;} 

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1028 Ignatius and the Princess III "integer division"

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.