hdu 1028 Ignatius and the Princess III(母函数入门+模板)
2024-10-19 19:56:57
Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.
"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
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
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
最新文章
- Quartz.net持久化与集群部署开发详解
- Bootstrap<;基础二十七>; 多媒体对象(Media Object)
- 【百度百科】对焦Focus
- ACM : Travel-并查集-最小生成树 + 离线-解题报告
- css之属性部分
- asp.net负载均衡方案[转]
- OC2_ARC MRC混合编程
- html天气预报小插件
- Block formatting context
- 面试(2)-java-se-HashSet和TreeSet
- Android权限管理知识学习记录
- 104 - kube-scheduler源码分析 - predicate整体流程
- UOJ#7. 【NOI2014】购票 点分治 斜率优化 凸包 二分
- HDU contest808 ACM多校第7场 Problem - 1008: Traffic Network in Numazu
- 3、SourceTree通过PUTTY连接GitLab
- 解决windows 下 mysql命令行导入备份文件 查询时乱码的问题
- Dubbo(4)消费Dubbo服务
- WP8.1学习系列(第十八章)——Windows Phone 交互和可用性
- 自动下载google reader里面的星标文章
- 团队计划第二期Backlog
热门文章
- 面试题-Java基础-面向对象
- TCP connect EADDRNOTAVAIL(99)错误原因分析
- 谷歌游览器对<;input type=&#39;file&#39;>; change只能响应1次解决和样式的改变
- Attribute的理解和认识
- mac安装lavaral
- linux服务器没网情况下手动安装软件几个方法
- 手机端rem自适应布局实例
- Python学习笔记——基础篇【第七周】———FTP作业(面向对象编程进阶 &; Socket编程基础)
- 常用网页标签重置CSS
- MyBatis-防止Sql注入以及sql中#{}与${}取参数的区别