Dollar Dayz
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4231 | Accepted: 1646 |
Description
Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are:
1 @ US$3 + 1 @ US$2 1 @ US$3 + 2 @ US$1 1 @ US$2 + 3 @ US$1 2 @ US$2 + 1 @ US$1 5 @ US$1Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).
Input
A single line with two space-separated integers: N and K.
Output
A single line with a single integer that is the number of unique ways FJ can spend his money.
Sample Input
5 3
Sample Output
5
Source
<span style="color: rgb(0, 153, 0); font-family: 'ms shell dlg'; font-size: 14px; line-height: 28px; white-space: pre-wrap;">考虑边界状态:</span><br style="color: rgb(0, 153, 0); font-family: 'ms shell dlg'; font-size: 14px; line-height: 28px; white-space: pre-wrap; clear: both;" /><span style="color: rgb(0, 153, 0); font-family: 'ms shell dlg'; font-size: 14px; line-height: 28px; white-space: pre-wrap;">M = 1 或者 N = 1 只有一个划分 既: F(1,1) = 1</span><br style="color: rgb(0, 153, 0); font-family: 'ms shell dlg'; font-size: 14px; line-height: 28px; white-space: pre-wrap; clear: both;" /><span style="color: rgb(0, 153, 0); font-family: 'ms shell dlg'; font-size: 14px; line-height: 28px; white-space: pre-wrap;">M = N : 等于把M - 1 的划分数加 1 既: F(N,N) = F(N,N-1) + 1 </span><br style="color: rgb(0, 153, 0); font-family: 'ms shell dlg'; font-size: 14px; line-height: 28px; white-space: pre-wrap; clear: both;" /><span style="color: rgb(0, 153, 0); font-family: 'ms shell dlg'; font-size: 14px; line-height: 28px; white-space: pre-wrap;">M > N: 按理说,N 划分后的序列中最大数是不会超过 N 的,所以 F(N,M ) = F(N,N)</span><br style="color: rgb(0, 153, 0); font-family: 'ms shell dlg'; font-size: 14px; line-height: 28px; white-space: pre-wrap; clear: both;" /><span style="color: rgb(0, 153, 0); font-family: 'ms shell dlg'; font-size: 14px; line-height: 28px; white-space: pre-wrap;">M < N: 这个是最常见的, 他应该是序列中最大数为 M-1 的划分和 N-M 的划分之和, 比如F(6,4),上面例子第三行, 他应该等于对整数 3 的划分, 然后加上 2 的划分(6-4) 所以 F(N,M) = F(N, M-1) + F(N-M,M)</span>
ac代码
#include<stdio.h>
#include<string.h>
__int64 a[1010][110],b[1010][110],inf=1;
int main()
{
int n,k,i;
for(i=0;i<18;i++)
inf*=10;
while(scanf("%d%d",&n,&k)!=EOF)
{
int i,j;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i=0;i<=k;i++)
a[0][i]=1;
for(j=1;j<=k;j++)
{
for(i=1;i<=n;i++)
{
if(i<j)
{
a[i][j]=a[i][j-1];
b[i][j]=b[i][j-1];
}
else
{
a[i][j]=(a[i-j][j]+a[i][j-1])%inf;
b[i][j]=b[i-j][j]+b[i][j-1]+(a[i-j][j]+a[i][j-1])/inf;
}
}
}
if(b[n][k])
printf("%I64d",b[n][k]);
printf("%I64d\n",a[n][k]);
}
}