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

完全背包+高精度数

没有优化直接wa,是数值范围太小
#include<iostream>
using namespace std;
int dp[][];
int main(){
int n,k;
cin>>n>>k;
dp[][]=;
for(int i=;i<=k;i++){
for(int j=;j<=n;j++){
for(int k=;k*i<=j;k++){
dp[i][j]+=dp[i-][j-k*i];
}
}
}
cout<<dp[k][n];
return ;
}

改用unsigned long long还是wa。那就用两个unsigned long long一个存低位一个存高位。unsigned long long的范围是1844674407370955161,所以用一个比它小一个数量级的数,

100000000000000000。
#include<iostream>
using namespace std;
unsigned long long dp[][][];
#define LIMIT_ULL 100000000000000000
int main(){
int n,k;
cin>>n>>k;
dp[][][]=;//低位
for(int i=;i<=k;i++){
for(int j=;j<=n;j++){
for(int k=;k*i<=j;k++){
dp[i][j][]+=dp[i-][j-k*i][];
dp[i][j][]+=dp[i-][j-k*i][];
dp[i][j][]+=dp[i][j][]/LIMIT_ULL;//低位的进位加到高位
dp[i][j][]=dp[i][j][]%LIMIT_ULL;//低位去除进位
}
}
}
if(dp[k][n][]){
cout<<dp[k][n][];
}
cout<<dp[k][n][];
return ;
}

以上比你不是最优的,可能会TLE

dp[i][j]+=dp[i-1][j-i*k]可以优化成dp[i][j]=dp[i-1][j]+dp[i-1][j-k]

因为当k>1时的计算结果,已经保存在了dp[i-1][j-k]

#include<iostream>
using namespace std;
unsigned long long dp[][][];
#define LIMIT_ULL 100000000000000000
int main(){
int n,k;
cin>>n>>k;
dp[][][]=;
for(int i=;i<=k;i++){
for(int j=;j<=n;j++){
if(j<i){
dp[i][j][]=dp[i-][j][];
dp[i][j][]=dp[i-][j][];
}
else{
dp[i][j][]=dp[i-][j][]+dp[i][j-i][];
dp[i][j][]=dp[i-][j][]+dp[i][j-i][];
dp[i][j][]+=dp[i][j][]/LIMIT_ULL;
dp[i][j][]=dp[i][j][]%LIMIT_ULL;
}
}
}
if(dp[k][n][]){
cout<<dp[k][n][];
}
cout<<dp[k][n][];
return ;
}

最新文章

  1. OpenCV2.3.1在Win7+VS2010下的配置过程(转)
  2. Visual Studio 2013 的 Xamarin 安装教程
  3. Zlib 在windows上的编译
  4. ios cell展示可滑动的图片
  5. 设计模式--原型(Prototype)模式
  6. 关于ie6下拖动滚动条时,div抖动的问题解决
  7. 简单的神经元算法实现(python)
  8. POJ 3126 Prime Path 素数筛,bfs
  9. oracle用户管理实例
  10. 网络子系统42_ip协议处理函数_数据帧的接收
  11. Yii框架下不同contoller之间的方法调用
  12. Java面试题积累
  13. [BZOJ]1069: [SCOI2007]最大土地面积
  14. 解决持久化数据太大,单个节点的硬盘无法存储的问题;解决运算量太大,单个节点的内存、CPU无法处理的问题
  15. mininet安装过程记录
  16. axios中的this指向问题
  17. 关于Revit API修改元素参数的问题?
  18. Chart:Amcharts
  19. Jmeter 接口测试知识梳理——持续集成篇
  20. .NET 日期数据的格式化方法

热门文章

  1. sharpsvn 继续,解决文件locked 问题,
  2. (转)Java程序员简历模板
  3. 5C - A == B ?
  4. N! (大数,优化)
  5. RAC初步使用
  6. html中 &amp;nbsp; 和空格的区别
  7. Creating Your Own PHP Helper Functions In Laravel
  8. Web应用获取文件路径的方法
  9. 【UI测试】--帮助设施
  10. c++中的log函数