HDU 2069 Coin Change 母函數求解,hducoin

來源:互聯網
上載者:User

HDU 2069 Coin Change 母函數求解,hducoin

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.