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