hdu 1398 Square Coins(母函數|完全背包)

來源:互聯網
上載者:User

標籤:母函數   完全背包   

http://acm.hdu.edu.cn/showproblem.php?pid=1398


題意:有價值為1^2,2^2....7^2的硬幣共17種,每種硬幣都有無限個。問用這些硬幣能夠組成價值為n的錢數共有幾種方案數。


母函數:

#include <stdio.h>#include <iostream>#include <map>#include <set>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL long long#define _LL __int64#define eps 1e-12#define PI acos(-1.0)using namespace std;int c1[310],c2[310];int main(){    int n;    for(int i = 0; i <= 300; i++)    {        c1[i] = 1;        c2[i] = 0;    }    for(int i = 2; i <= 17; i++)    {        for(int j = 0; j <= 300; j++)        {            for(int k = 0; k+j <= 300; k += i*i) //增量變為i*i                c2[k+j] += c1[j];        }        for(int j = 0; j <= 300; j++)        {            c1[j] = c2[j];            c2[j] = 0;        }    }    while(~scanf("%d",&n)&&n)    {        cout << c1[n] << endl;    }    return 0;}


完全背包:每種物品都有一定的價值i*i,數目有無限件,那麼價值為v的背包能放下物品組合的種類為

f[v] = sum{f[v-k*val[i]] | c[i]*k <= V}。初始化f[0] = 1.

#include <stdio.h>#include <iostream>#include <map>#include <set>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL long long#define _LL __int64#define eps 1e-12#define PI acos(-1.0)using namespace std;int dp[300];int main(){int n;while(~scanf("%d",&n)&&n){memset(dp,0,sizeof(dp));dp[0] = 1;for(int i = 1; i <= 17; i++){for(int j = i*i; j <= n; j++)dp[j] += dp[j-i*i];}printf("%d\n",dp[n]);}return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.