首页 > 其他 > 详细

poj3181 Dollar Dayz

时间:2015-06-30 13:03:20      阅读:311      评论:0      收藏:0      [点我收藏+]

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$1
Write 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

题意:给你两个数n,k,让你用1到k这k个数表示n,问有几种方法,本质是整数的拆分。因为最后结果比较大,超过long long ,所以用两个long long连接起来,设两个数组a[][],b[][]分别表示没有超过long long的部分以及超过long long 部分。

其中a[n][m]表示n用一些数拆分,其中最大的数不超过m的方案数,画图可以看到,当n<m时,a[i][j]=a[i][i];当n>=m时,a[i][j]=(a[i][j-1]+a[i-j][j])%inf;初始化的时候要令a[0][i]=1,因为a[2][2]=a[0][2]+a[2][1];

6
5 + 1
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

#include<stdio.h>
#include<string.h>
#define ll long long
ll a[1006][106],b[1006][106];
ll inf;
int main()
{
int n,m,i,j;
inf=1;
for(i=1;i<=18;i++){
inf*=10;
}
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i=1;i<=m;i++){
a[0][i]=1;
}
for(i=1;i<=n;i++){
a[i][1]=1;
}
for(j=1;j<=m;j++){
a[1][j]=1;
}

for(j=2;j<=m;j++){
for(i=2;i<=n;i++){
if(i<j){
a[i][j]=a[i][i];
b[i][j]=b[i][i];
}
else{
a[i][j]=(a[i][j-1]+a[i-j][j])%inf;
b[i][j]=b[i][j-1]+b[i-j][j]+(a[i][j-1]+a[i-j][j])/inf;
}
//printf("%d %d %d\n",i,j,a[i][j]);
}
}
if(b[n][m])
printf("%lld",b[n][m]);
printf("%lld\n",a[n][m]);
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj3181 Dollar Dayz

原文:http://blog.csdn.net/kirito_acmer/article/details/46692231

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!