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;

const int MAX = 252;
int ans[MAX] = {};
int c1[MAX][MAX], c2[MAX][MAX];     // c1[系数][累加的个数]

void getAns()
{
    ans[0] = 1;
    const int t[] = {0, 1, 5, 10, 25, 50};

    for(int n=1; n<MAX; ++n)
    {
        memset(c2, 0, sizeof(c2));
        memset(c1, 0, sizeof(c1));

        for(int i=0; i<=n; ++i)
        {
            c1[i][i] = 1;       // 初始化
        }

        for(int i=2; i<=5; ++i)
        {
            for(int j=0; j<=n; ++j)
            {
                for(int c=0; c<MAX; ++c)    // 遍历 系数为 j, 但是个数不同的情况
                {
                    if (c1[j][c]==0)
                        continue;

                    for(int k=0; k+j<=n; k+=t[i])
                    {
                        c2[k+j][c+k/t[i]] += c1[j][c];
                    }
                }
            }

            memcpy(c1, c2, sizeof(c2));
            memset(c2, 0, sizeof(c2));
        }

        for(int i=1; i<=100; ++i)   // 只取不超过100的结果
        {
            ans[n] += c1[n][i];
        }
    }
}

int main(void)
{
    //freopen("in.txt", "r", stdin);

    getAns();   // 初始化,打表

    int n = 0;
    while(cin>>n)
    {
        printf("%d\n", ans[n]);
    }

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 http://blog.csdn.net/core__code

时间: 2024-10-20 16:28:06

HDU 2069 Coin Change 母函数求解的相关文章

UVa 674 &amp; 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() { in

(简单母函数进阶版,暴力)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: 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: 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 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 (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二维母函数

显然母函数,有一个地方需要注意.当输入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[

LeetCode OJ 322. Coin Change DP求解

题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accepted: 15289 Total Submissions: 62250 Difficulty: Medium You are given coins of different denominations and a total amount of money amount. Write a func