POJ 3181 Dollar Dayz(dp,背包)

dp[i][j] 表示 用 前i种前能 凑成j 元的方法数。

dp[i][j] = dp[i-1][j] + dp[i][j- (i+1)]  因为我i是从0开始的。。 所以i+1 表示第i中的值

最后要用一下高精度

题目:

H - Dollar Dayz
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status
Appoint description:

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 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 int N,K;
 6 int dp[1000+10][1000+10];
 7 
 8 void epl(int a,int b)
 9 {
10     for(int i=0;i<=60;i++)
11     {
12         dp[a][i] = dp[b][i];
13     }
14 }
15 
16 void add(int a,int b)
17 {
18     for(int i=0;i<60;i++)
19     {
20         dp[a][i]+= dp[b][i];
21         if(dp[a][i]>=10)
22         {
23             dp[a][i]%=10;
24             dp[a][i+1] ++;
25         }
26     }
27 }
28 
29 int main()
30 {
31     cin>>K>>N;
32     dp[0][0]=1;
33     for(int i=0;i<N;i++)
34     {
35         for(int k=0;k<=K;k++)
36         {
37             //dp[i+1][k] = dp[i][k];
38             if( k>=i+1)
39             {
40                 //dp[i+1][k]+= dp[i+1][k-i-1];
41                 add(k,k-i-1);
42             }
43         }
44     }
45     int t = 60;
46     while(t>0 && dp[K][t]==0)t--;
47     for(int i=t;i>=0;i--)
48     {
49         printf("%d",dp[K][i]);
50     }
51     //cout<<dp[N][K];
52     printf("
");
53     return 0;
54 }
原文地址:https://www.cnblogs.com/doubleshik/p/3538016.html