hdu 1028 Ignatius and the Princess III(母函数入门+模板)
Description
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
Input
Output
Sample Input
4
10
20
Sample Output
5
42
627 解析:
母函数入门题,首先母函数
G(x) = (1 + x^1 + x^2..+x^n)(1 + x^2 + x^4 + x^6 + ...)(1 + x^3 + x^6 +..)(..)(1 + x^n) 其中(1 + x^1 + x^2..+x^n)中x的指数为1出现的次数 x^3 ==》 x^(1*3)
其余同理,比如第二个表达式 (1 + x^2 + x^4 + x^6 + ...) x^2 ==> x^(2*1) x^6 ==> x^(2*3)
乘法原理的应用:每一个表达式 表示的都是 某个变量的所有取值
【比如第一个表达式 表示'1'可以取的值(即n拆分后'1'出现的次数)可以为 {0,1,2...n}】
每个变量的所有取值的乘积 就是问题的所有的解(在本问题中表现为‘和’)
例子:4 = 2 + 1 + 1就是 x^(1 * 2)【'1'出现2次】
* x^(2 * 1)【'2'出现1次】
* x^(3 * 0)【'3'出现0次】
* x^(4 * 0)【..】
的结果
所以 G(x)展开后 其中x^n的系数就是 n的拆分解个数
# include <stdio.h> int main()
{
int C1[], C2[], n; while(scanf("%d", &n) != EOF)
{
for(int i = ; i <= n; i++)//初始化 第一个表达式 目前所有指数项的系数都为1
{
C1[i] = ;
C2[i] = ;
} for(int i = ; i <= n; i++)//第2至第n个表达式
{
for(int j = ; j <= n; j++)//C1为前i-1个表达式累乘后各个指数项的系数
{
for(int k = ; j + k <= n; k += i)//k为第i个表达式每个项的指数 第一项为1【即x^(i * 0)】(指数k=0),第二项为x^(i * 1)(指数为k=i), 第三项为x^(i * 2)... 所以k的步长为i
{
C2[j + k] += C1[j];//(ax^j)*(x^k) = ax^(j+k) -> C2[j+k] += a 【第i个表达式每一项的系数都为1; a为C1[j]的值(x^j的系数); C2为乘上第i个表达式后各指数项的系数】
}
}
for(int j = ; j <= n; j++)//刷新当前累乘结果各指数项的系数
{
C1[j] = C2[j];
C2[j] = ;
}
}
printf("%d\n",C1[n]);
} return ;
}
出处http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=21943&messageid=2&deep=1
hdu 1028 Ignatius and the Princess III(母函数入门+模板)的更多相关文章
- hdu 1028 Ignatius and the Princess III 母函数
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- HDU 1028 Ignatius and the Princess III 整数的划分问题(打表或者记忆化搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Time Limit: 2000/1 ...
- HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1028 Ignatius and the Princess III(DP)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1028 Ignatius and the Princess III (n的划分)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028 Ignatius and the Princess III (生成函数/母函数)
题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...
- HDU 1028 Ignatius and the Princess III (递归,dp)
以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802 Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...
- HDU 1028 Ignatius and the Princess III (动态规划)
题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...
随机推荐
- payoneer注册充值提现海外收款费用官方解答
从事海外贸易的朋友,会发现收款是一大难题.Paypal是老牌支付平台,但费率高昂.其实,Payoneer是新兴的收款工具,非常适合做外贸的卖家使用,提现灵活,费率低,免费注册账号后,可直接获得美国.英 ...
- 随机love'...
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux 分区挂载方案
/boot 1G swap 2G(看内存决定) / 10-15G /home 5G
- 面试题-Java基础-布局管理器
1.什么是布局管理器? 布局管理器用来在容器中组织组件.
- CODE[VS]-寻找子串位置-字符串处理-天梯青铜
题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述 ...
- 持续集成Jenkins+sonarqube部署教程
1 引言 1.1 文档概要 本文主要介绍jenkins,sonar的安装与集成,基于ant,maven构建.用一个例子介绍jenkins的编译打包部署,代码检查.最后集成jenkins.(现阶段只是简 ...
- 日期的本质是double
日期的本质是double,在多语种时用起来更方便.
- swift 经典代码收集 和 赏析
代码1:protocol PickableEnum { var displayName: String { get } var permanentID: String { get } static v ...
- Python学习笔记——基础篇【第五周】——常用模块学习
模块介绍 本节大纲: 模块介绍 time &datetime模块 (时间模块) random (随机数模块) os (系统交互模块) sys shutil (文件拷贝模块) j ...
- iOS参考工具和资源
图片: Glyphish(图标资源) 资源: SwiftGuide:这份指南汇集了Swift语言主流学习资源,并以开发者的视角整理编排. 27款iOS开源库,让你的开发溜到飞起 创业者的新春礼包—优秀 ...