hdu 2069 完全揹包dp

A - Coin Change

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money. 

For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent. 

Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins. 

 

Input

The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.

 

Output

For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.

 

Sample Input


 

11 26

 

Sample Output


 

4 13

這道題有個坑點,題目不能超過100個硬幣。也就是101 不能用101個一塊的

除開這個就是完全揹包了。 我們加一個維度,表示當前用的硬幣 dp[j][k] 表示當前在j元錢用了k個硬幣一共有多少情況。

狀態轉移方程 : dp【j】【k】 += dp【j - v【i】】【k - 1】

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#include <bitset>
#define  LL long long
#define  ULL unsigned long long
#define mod 100000000
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int maxn = 1e4 + 5;
int n,m;
int v[5] = {1,5,10,25,50};
int dp[maxn][105],dp2[maxn][105];
int main()
{
  while(~scanf("%d",&n)){
    mem(dp,0);
    dp[0][0] = 1;
    for(int i = 0; i < 5; i++){
      for(int j = 1; j <= 100; j++){
        for(int k = v[i]; k <= n; k++){
           dp[k][j] += dp[k - v[i]][j - 1];
        }
      }
    }
    int ans = 0;
    for(int i = 0; i <= 100; i++){
      ans += dp[n][i];
    }
    printf("%d\n",ans);
  }

  return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章