UVa 674 & hdu 2069 Coin Change (母函数,dp)

链接:uva 674

题意:有5中货币,价值分别为 50-cent, 25-cent, 10-cent, 5-cent,1-cent,数量都为无限个,

给定一个数 n,求用上述货币组成价值为 n 的方法有多少?

分析:因为n<=7489,可以用 母函数 或 dp 打表

对于dp状态方程为: dp[j]+=dp[j-c[i]]

母函数:

#include<stdio.h>
int c1[7500],c2[7500],w[5]={1,5,10,25,50};;
void mhs()
{
    int i,j,k;
    for(i=0;i<=7489;i++){
        c1[i]=1;
        c2[i]=0;
    }
    for(i=1;i<5;i++){
        for(j=0;j<=7489;j++)
            for(k=0;j+k<=7489;k+=w[i])
                c2[j+k]+=c1[j];
        for(j=0;j<=7489;j++){
            c1[j]=c2[j];
            c2[j]=0;
        }
    }
}
int main()
{
    int n;
    mhs();
    while(scanf("%d",&n)!=EOF)
        printf("%d\n",c1[n]);
    return 0;
}

dp:

#include<stdio.h>
int dp[7500],c[5]={1,5,10,25,50};
int main()
{
    int n,i,j;
    dp[0]=1;
    for(i=0;i<5;i++)
        for(j=c[i];j<=7489;j++)
            dp[j]+=dp[j-c[i]];
    while(scanf("%d",&n)!=EOF)
        printf("%d\n",dp[n]);
    return 0;
}

链接:hdu 2069

这题咋一看去跟上一题一样,样例神马的都一样,心中窃喜,拿上一题代码之间复制粘贴,

结果瞬间就WA,顿时懵了,很忧伤、、、只得好好瞅瞅题了、、、

题意:这题也是有5种货币,分别为50-cent, 25-cent, 10-cent, 5-cent,1-cent,数量都为无限个,

给定一个数 n,要求用所给货币,求用不超过100个货币,组成价值为 n 的方法个数。

分析:这题只是比上题多了个所用货币数额的限制条件,因为n<=250,同样还是可以用母函数或dp打表做,

但是因为要记录用了多少个货币,要从一维扩展为二维,

母函数:

#include<stdio.h>
int c1[300][105],c2[300][105],s[300];
int w[5]={1,5,10,25,50};
void mhs()
{
    int i,j,k,p;
    for(i=0;i<=100;i++)
        c1[i][i]=1;
    for(i=1;i<5;i++){
        for(j=0;j<=250;j++)
            for(k=0;j+k*w[i]<=250;k++)
                for(p=0;k+p<=100;p++)    //限制所用货币不超过100
                    c2[j+k*w[i]][p+k]+=c1[j][p];
        for(j=0;j<=250;j++)
            for(p=0;p<=100;p++){
                c1[j][p]=c2[j][p];
                c2[j][p]=0;
            }
    }
    for(j=0;j<=250;j++)
        for(p=0;p<=100;p++)
            s[j]+=c1[j][p];
}
int main()
{
    int n;
    mhs();
    while(scanf("%d",&n)!=EOF)
        printf("%d\n",s[n]);
    return 0;
}

dp

#include<stdio.h>
int dp[105][300],c[5]={1,5,10,25,50};
int main()
{
    int n,i,j,k,s[300]={0};
    dp[0][0]=1;                  //0可以用0个货币组成,这也算一种方法
    for(i=0;i<5;i++)
        for(j=1;j<=100;j++)
            for(k=c[i];k<=250;k++)
                dp[j][k]+=dp[j-1][k-c[i]];
    for(i=0;i<300;i++)
        for(j=0;j<=100;j++)
            s[i]+=dp[j][i];  //求能用0-100组成价值为 i的总方法数
    while(scanf("%d",&n)!=EOF)
        printf("%d\n",s[n]);
    return 0;
}

UVa 674 & hdu 2069 Coin Change (母函数,dp)

时间: 2025-01-02 14:34:05

UVa 674 & hdu 2069 Coin Change (母函数,dp)的相关文章

HDU 2069 Coin Change 母函数求解

HDU 2069 Coin Change 母函数求解 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2069 这题比普通的母函数求解题目复杂一点,多了组成个数的限制, 要求组合个数不能超过 100 个硬币,所以将 c1, c2 定义为二维数组, c1[i][j] 表示 c1[结果][组成该结果的个数] ,然后多了一层遍历个数的循环. // 母函数求解 #include <bits/stdc++.h> using namespace std; cons

hdu 2069 Coin Change (dp )

dp #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> using namespace std; int op[10]={1,5,10,25,50}; int dp[300][300]; void fun() { int i,j,k; memset(dp,0,sizeof(dp)); dp[0][0]=1; for(j

HDU 2069 Coin Change (经典DP)

Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14500    Accepted Submission(s): 4879 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1

hdu 2069 Coin Change 背包。本来打算用母函数再写一遍的,发现代码极其相似,就没写

Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14982    Accepted Submission(s): 5070 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and

(简单母函数进阶版,暴力)hdu 2069 Coin Change

Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14857    Accepted Submission(s): 5024 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and

HDU 2069 Coin Change

Coin Change Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 206964-bit integer IO format: %I64d      Java class name: Main Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We

hdu 2069 Coin Change(完全背包)

Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16592 Accepted Submission(s): 5656 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent.

HDU 2069二维母函数

显然母函数,有一个地方需要注意.当输入0时,只有一种方法,所以输出1. 代码: 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 int c1[255][111], c2[255][111]; 7 main() 8 { 9 10 int n, i, j, k, l; 11 int a[

uva 674 Coin Change 经典dp入门

Coin Change Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UVA 674 Appoint description: Description Download as PDF Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to m