母函数初级运用 hdu 1028 、hdu 1398、hdu 1085
2012-05-03 21:08
375 查看
母函数即生成函数,有普通型生成函数和指数型生成函数两种,常用于组合数学中求某个问题的方法数。
例:G(x) = a0 + a1x + a2x*2 + a3x^3 +....+ anx^n
其中ai表示i的组合数。
例题1:hdu 1028 Ignatius and the Princess III
题目描述:输入正整数n(n<=120),输出由正整数组成的和为n的组合数。
例题2:hdu 1398 Square Coins
hdu1085 Holding Bin-Laden Captive!
例:G(x) = a0 + a1x + a2x*2 + a3x^3 +....+ anx^n
其中ai表示i的组合数。
例题1:hdu 1028 Ignatius and the Princess III
题目描述:输入正整数n(n<=120),输出由正整数组成的和为n的组合数。
//输入正整数n(n<=120),输出由正整数组成的和为n的组合数。 //G(x)=(x+x^2+x^3+...)*(x^2+x^4+...)*(x^3+x^6+...)*... #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=125; int c1[maxn], c2[maxn]; int solve(int cnt){ int i, j, k; for(i=0; i<=cnt; i++){ c1[i]=1; c2[i]=0; } for(i=2; i<=cnt; i++){ //第i项 for(j=0; j<=cnt; j++) //枚举每一位 for(k=0; k+j<=cnt; k+=i) //枚举第i项的每一位 c2[j+k]+=c1[j]; for(j=0; j<=cnt; j++){ c1[j]=c2[j]; c2[j]=0; } } return c1[cnt]; } int main(){ //freopen("1.txt", "r", stdin); int n; solve(120); while(scanf("%d", &n)!=EOF){ printf("%d\n", c1 ); } return 0; }
例题2:hdu 1398 Square Coins
//输入正整数n(n<=300),输出由完全平方数组成的和为n的组合数。 //G(x)=(x+x^2+x^3+...)*(x^4+x^8+...)*(x^9+x^18+...)*... #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=305; int c1[maxn], c2[maxn]; int solve(int cnt){ int i, j, k; for(i=0; i<=cnt; i++){ c1[i]=1; c2[i]=0; } for(i=2; i*i<=cnt; i++){ for(j=0; j<=cnt; j++) for(k=0; k+j<=cnt; k+=i*i) c2[j+k]+=c1[j]; for(j=0; j<=cnt; j++){ c1[j]=c2[j]; c2[j]=0; } } return c1[cnt]; } int main(){ //freopen("1.txt", "r", stdin); int n; while(scanf("%d", &n)&&n){ printf("%d\n", solve(n)); } return 0; }
hdu1085 Holding Bin-Laden Captive!
//输入面值为1,2,5的硬币的数量,求第一个不能用他们组成的数。 //G(x)=(x+x^2+x^3+...)*(x^2+x^4+...)*(x^5+x^10+...) (每组的长度有限,分别对应硬币数量) #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=1000000; int c1[maxn], c2[maxn], a[3], v[3]={1, 2, 5}; int solve(int cnt){ int i, j, k, t; memset(c1, 0, sizeof(c1)); memset(c2, 0, sizeof(c2)); for(i=0; i<=cnt&&i<=a[0]; i++) c1[i]=1; for(i=1; i<=2; i++){ for(j=0; j<=cnt; j++) for(k=0, t=0; k+j<=cnt&&t<=a[i]; k+=v[i], t++) c2[j+k]+=c1[j]; for(j=0; j<=cnt; j++){ c1[j]=c2[j]; c2[j]=0; } } return c1[cnt]; } int main(){ //freopen("1.txt", "r", stdin); int n; while(scanf("%d%d%d", &a[0], &a[1], &a[2])&&(a[0]+a[1]+a[2])){ n=a[0]+a[1]*2+a[2]*5; solve(n+1); n=1; while(n){ if(!c1 ){ printf("%d\n", n); break; } n++; } } return 0; }
相关文章推荐
- hdu 1028 Ignatius and the Princess III +hdu 1085 Holding Bin-Laden Captive!(母函数)
- 母函数的应用 :HDU 1028/ HDU 1328 / HDU 1085
- 母函数的应用 :HDU 1028/ HDU 1328 / HDU 1085
- hdu 1085 Holding Bin-Laden Captive! 母函数的基本运用,,还是不难的
- 杭电1398,1028,1085//赤裸裸的母函数
- Ignatius and the Princess III (HDU 1028) ——母函数(另解DP)
- HDU 1028 Ignatius and the Princess III(母函数)
- hdu 1028 Ignatius and the Princess III 母函数
- HDU 1085 (母函数)
- [ACM] hdu 1085 Holding Bin-Laden Captive! (母函数变形)
- 杭电hdu 1398 Square Coins 母函数
- hdu 1028 Ignatius and the Princess III (母函数)
- HDU 1028 Ignatius and the Princess III(母函数)
- hdu 1028 母函数模板
- 母函数模板 HDU-1028
- HDU -- 1085 Holding Bin-Laden Captive!(母函数)
- HDU 1028 Ignatius and the Princess III (母函数模板)
- hdu 1398 整数划分变形 (母函数)
- 【HDU 1398 Square Coins】+ 母函数
- HDU 1085 Holding Bin-Laden Captive! (母函数)