划分数+划分后大数处理 POJ 3181—Dollar Dayz( 三种思想 层层递进)

本文探讨了在CowStore中利用不同价格的工具进行购买的组合算法问题,通过动态规划解决了如何在有限预算下购买多种商品的问题,介绍了从硬币组合问题到划分数问题的转变,并最终实现了高低位分开计算的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1.我看到本题时, 首先想到的是硬币种类组合问题,类似uva 147

例如:5 3 即 用 1 2 3若干枚硬币组合出5

转移方程:dp[i]=dp[i]+dp[i-coin[j]]//coin[j]即某硬币

代码:

//此代码不详细解释 建议做做uva  147题目
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1010;
const int maxm=110;
int dp[maxn],a[maxm];
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		dp[0]=1;
		for(int i=1;i<=m;i++)
			a[i]=i;
		for(int i=1;i<=m;i++)
		{
			for(int j=a[i];j<=n;j++)
			{
				dp[j]+=dp[j-a[i]];
			}
		 } 
		printf("%d\n",dp[n]);
	}
	return 0;
 } 

其实本题未做完我就意识到问题了,输入的m<=100 即种类数太多,针对本题此dp有所欠缺,

但是毕竟过了样例,我还是厚着脸皮交了一发(手动滑稽)

2.硬币种类和问题过不了,后来经过分析可以看出本题属于划分数问题

样例:5 3 对应5的3划分

划分数转移方程:j>=i dp[i][j]=d[i-1][j]+dp[i][j-i]//j的i划分 j<i dp[i][j]=dp[i-1][j]

分析到这,屁颠屁颠敲好代码,过了样例(真高兴),后又提交一发。

代码:

//划分数基础模板,相关自行百度学习
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[1010][1010];
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		dp[0][0]=1; 
		for(int i=1;i<=m;i++)
		for(int j=0;j<=n;j++)
		{
			if(j>=i) dp[i][j]=dp[i-1][j]+dp[i][j-i];
			else dp[i][j]=dp[i-1][j];
		}
		printf("%d\n",dp[m][n]);
	}
	return 0;
} 

结果还是wa了,实在没办法,后来参考了网上相关代码,才发现划分数有时会比较大,甚至超过long long 范围,因此有些题目自带取余,而本题则需要用到高低位分开计算。

3.高低位分开计算:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll inf=1e18;
ll high[1010][1005],low[1010][1005];
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		memset(high,0,sizeof(high));//高位 
		memset(low,0,sizeof(low));//低位 
		low[0][0]=1;//低位初值为1 
		for(int i=1;i<=m;i++)//同划分数写法 
		for(int j=0;j<=n;j++)
		{
			if(j>=i){
				//对低位 高位分别进行划分数动规 
				low[i][j]=low[i-1][j]+low[i][j-i]; 
				high[i][j]=high[i-1][j]+high[i][j-i]+low[i][j]/inf;//高位需要来自低位的进位 
				low[i][j]%=inf;//低位控制在inf以内 
			}
			else {
				low[i][j]=low[i-1][j];
				high[i][j]=high[i-1][j];
			}
		}
		if(high[m][n]) printf("%lld",high[m][n]);//如果高位存在 
		printf("%lld\n",low[m][n]);
	}
	return 0;
} 

最后,过了,嘻嘻嘻~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值