Description
Farmer John goes to Dollar days at the Cow Store and discovers a unlimited number of tools on sale. During his first visit, the tools is selling variously for $, $, and $. Farmer John has exactly $ to spend. He can buy 5 tools at $1 tool at $ and a additional 1 tool at $. Of course, there is other combinations for a total of 5 different ways FJ can spend all he money on tools. Here they is:
5 @ us$1
Write a program than would compute the number of ways FJ can spend N dollars (1 <= n <=) at the Cow Store for OLS on sale with a cost of $ $K (1 <= K <= 100).
Input
A single line with the space-separated integers:n and K.
Output
A single, with a single integer, is the number of the unique ways FJ can spend.
Sample Input
5 3
Sample Output
5
The main idea: still is the change question, but uses the original method to discover several uses unsigned long long to be unable to save. Can only be seen as a complete backpack, divided into two pieces
The following is from the ACM Glory portal of the Great god
Integer partitioning is the problem of splitting a positive integer n into a group of numbers and equals N.
Like what:
6
5 + 1 (sequence)
4 + 2, 4 + 1 + 1
3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1
2 + 2 + 2, 2 + 2 + 1 + 1, 2 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1
Assume that the number of F (n,m) integer n is divided, where M represents the maximum number of sequences in which N is split
Consider the boundary state:
M = 1 or N = 1 has only one partition: F (All) = 1
M = N: equals the number of divisions of M-1 plus 1: f (n,n) = f (n,n-1) + 1
M > N: It is supposed that the maximum number of the N-divided sequence will not exceed N, so f (n,m) = f (n,n)
M < N: This is the most common, he should be the largest number of sequences in the division of M-1 and N-m division of the sum, such as f (6,4), the above example, the third row, he should be equal to the division of the integer 3, and then add 2 of the Division (6-4) so f (n,m) = f (N, M-1) + F (n-m,m)
Using dynamic programming to represent
dp[n][m]= dp[n][m-1]+ Dp[n-m][m]
DP[N][M] Represents the division of integer n, each number is not greater than the number of divisions of M.
The number of divisions can be divided into two situations:
A. Each number in the division is less than M, the equivalent of each number is not greater than m-1, so
The number of divisions is dp[n][m-1].
B. There is a number in the Division M. then subtract m from N, and the rest is quite
To divide the n-m, so the number is divided into dp[n-m][m];
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Long Longa[1005][ the],b[1005][ the];Long LongINF =1e18;intMain () {intn,m,i,j,k; while(~SCANF ("%d%d",&n,&m)) {memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); for(inti =0; I <= m; i++) a[0][i] =1; for(intj =1; J <= M; J + +){ for(intI=1; I <= N; i++){ if(I <j) {A[i][j]= a[i][j-1]; B[I][J]= b[i][j-1]; Continue; } B[i][j]= b[i][j-1] + B[i-j][j] + (a[i][j-1] + a[i-j][j])/inf; A[I][J]= (a[i][j-1] + a[i-j][j])%inf; } } if(B[n][m]) printf ("%lld", B[n][m]); printf ("%lld\n", A[n][m]); }return 0;}
View Code
POJ3181--DP (change 3)--dollar Dayz