hdu 1028 Ignatius and the Princess III (整数拆分)
来源:互联网 发布:科比体测数据 编辑:程序博客网 时间:2024/05/19 12:12
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028
题目大意:整数拆分,把一个整数拆分,以4为例
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
问能拆分成几种情况?
分析:直接用母函数来解。
普通型母函数:(感觉这东西很难理解, 但是理解之后就很简单)
首次理解一个多项式,(1 + a1x)(1 + a2x)(1 + a2x)...(1 + anx) = 1 + (a1 + a2 + ... + an)x + (a1a2 + a1a3 + ... + a1an)x^2 + ... + a1a2...anx^n
由上式可得:
1、x项的系数是n个元素中选取一个元素组合的全体
2、x^2项的系数是n个元素中选取两个元素组合的全体
由上式可得:
1、x项的系数是n个元素中选取一个元素组合的全体
2、x^2项的系数是n个元素中选取两个元素组合的全体
n、x^n项的系数是n个元素中选取n个元素组合的全体
所以可以得到(1 + x)^n = 1 + C(n,1)x + C(n,2)x^2 + ... + C(n,n)x^n
由此就可以构造一个母函数G(x)= a0 + a1x + a2x^2 + a3x^3 + ... + anx^n
其实学到这里我也是一脸懵逼的,完全不知道这到底有什么用,那么接下来直接看一个例子吧。
eg:有质量为1,2,3的砝码各一枚,问:
(1)、可以称出多少种不同质量的物品?
(2)、要称出质量为3的物品,有几种可能的方案?
解:构造母函数G(x)= (1 + x)(1 + x^2 ) (1 + x^3)= 1 + x + x^2 + 2x^3 + x^4 + x^5 + x^6
(1)、可以称出多少种不同质量的物品?
(2)、要称出质量为3的物品,有几种可能的方案?
解:构造母函数G(x)= (1 + x)(1 + x^2 ) (1 + x^3)= 1 + x + x^2 + 2x^3 + x^4 + x^5 + x^6
由上式可知:
(1)、可以称出1-6,6中质量,各项的系数就是每种质量的方案数量
(2)、x^3的系数为2,所以可以称出质量为3的物品的可能方案数为2(幂指数为质量)
了解了母函数,接下来只要用程序把母函数展开就好了,结合代码自己理解吧
ac代码:
#include<cstdio>#define M 125int c1[M], c2[M];//c1为保存,c2为临时保存void init(){for(int i = 0; i <= M; i++)//保存第一个小括号里的指数{c1[i] = 1;c2[i] = 0;}for(int i = 2; i <= M; i++)//从第二个小括号里的指数开始到第M个{for(int j = 0; j <= M; j++)//从j个小括号的第一项开始到第M项for(int k = 0; k + j <= M; k = k + i)//已保存的项的指数c2[k + j] = c2[k + j] + c1[j];for(int j = 0; j <= M; j++){c1[j] = c2[j];c2[j] = 0;}}}int main(){int n;init();while(scanf("%d", &n) != EOF)printf("%d\n", c1[n]);return 0;}
若理解有误,请指出。。。
阅读全文
0 0
- hdu 1028 Ignatius and the Princess III (整数拆分)
- HDU 1028 Ignatius and the Princess III 整数的拆分
- HDU 1028 Ignatius and the Princess III【整数拆分】
- HDU 1028 Ignatius and the Princess III(整数拆分问题)
- HDU 1028-Ignatius and the Princess III(拆分整数-母函数-无限次)
- Hdu 1028 Ignatius and the Princess III(整数拆分问题,DP)
- hdu 1028 Ignatius and the Princess III(整数拆分,母函数或dp)
- hdu 1028 Ignatius and the Princess III(整数划分)
- HDU 1028-Ignatius and the Princess III-母函数-整数拆分
- hdu 1028 Ignatius and the Princess III(整数拆分较大数据量模板)
- hdu 1028 Ignatius and the Princess III 整数拆分 母函数/dp
- HDU 1085 Ignatius and the Princess III (母函数-整数拆分)
- hdu 1028Ignatius and the Princess III(整数划分)
- hdu 1028 Ignatius and the Princess III 【整数划分】
- Ignatius and the Princess III (HDU_1028) 母函数 + 整数拆分
- B - Ignatius and the Princess III 整数拆分问题
- hdu 1028 Ignatius and the Princess III
- HDU 1028 Ignatius and the Princess III
- LTE学习笔记--PHY--扫频
- MongoDB的基本使用
- Android面试题整理-1
- 简单的理解android四大组件
- 第一篇:初识Python
- hdu 1028 Ignatius and the Princess III (整数拆分)
- PAT b1011-b1015题解
- 拦截器链模式(责任链)
- Android面试题整理-2
- 获取Java虚拟机正在运行所有线程的堆栈信息
- javascript消息框
- Java8新特性--Lambda表达式
- 《加密与解密》笔记四(二)
- React简介