题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028

题意:

  给你一个正整数n,将n拆分成若干个正整数之和,问你有多少种方案。

  注:"4 = 3 + 1"和"4 = 1 + 3"视为同一种方案。(加数是无序的)

题解1(dp):

  表示状态:

    dp[n][m] = num of methods

    表示用均不超过m的元素组成n的方法数。

  

  如何转移:

    假设当前状态为dp[n][m].

    对于等于m的元素,有两种决策。要么不用,要么用。

      (1)不用:dp[n][m] += dp[n][m-1]

      (2)用: dp[n][m] += dp[n-m][m]

    综上:dp[n][m] = dp[n][m-1] + dp[n-m][m]

  

  找出答案:

    ans = dp[n][n]

  

  边界条件:

    dp[0][i] = 1  (0<=i<=MAX_N)

题解2(母函数):

  要凑出n,每种方案无非是取几个1,几个2,几个3......

  这就是母函数的经典问题啦(取硬币)。

  

  构造母函数:

    G(x) = (1 + x^1 + x^2 + x^3...) * (1 + x^2 + x^4 + x^6...) * (1 + x^3 + x^6 + x^9...) * (1 + x^4 + x^8 + x^12...)...

  

  找出答案:

    x^n项的系数即为答案。

AC Code(dp):

 // dp[n][m] = num of methods
// n: the elems are up to n
// m: each elem won't be not more than m
// dp[n][m] = dp[n][m-1] + dp[n-m][m]
// ans = dp[n][n]
// dp[0][m] = 1 #include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 125 using namespace std; int n;
int dp[MAX_N][MAX_N]; void cal_dp()
{
memset(dp,,sizeof(dp));
for(int i=;i<MAX_N;i++)
{
dp[][i]=;
}
for(int i=;i<MAX_N;i++)
{
for(int j=;j<MAX_N;j++)
{
if(i>=j) dp[i][j]=dp[i][j-]+dp[i-j][j];
else dp[i][j]=dp[i][i];
}
}
} int main()
{
cal_dp();
while(cin>>n)
{
cout<<dp[n][n]<<endl;
}
}

AC Code(Generating Function):

 #include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 125 using namespace std; int n;
int ans[MAX_N];
int temp[MAX_N]; void generating_function(int n)
{
memset(ans,,sizeof(ans));
ans[]=;
for(int i=;i<=n;i++)
{
memset(temp,,sizeof(temp));
for(int j=;j*i<=n;j++)
{
for(int k=;k+j*i<=n;k++)
{
temp[k+j*i]+=ans[k];
}
}
memcpy(ans,temp,sizeof(temp));
}
} int main()
{
generating_function();
while(cin>>n)
{
cout<<ans[n]<<endl;
}
}

HDU 1028 Ignatius and the Princess III:dp or 母函数的更多相关文章

  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 ...

  2. HDU 1028 Ignatius and the Princess III dp整数划分

    http://acm.hdu.edu.cn/showproblem.php?pid=1028 dp[i][j]表示数值为i,然后最小拆分的那个数是j的时候的总和. 1 = 1 2 = 1 + 1 . ...

  3. hdu 1028 Ignatius and the Princess III 简单dp

    题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. hdu 1028 Ignatius and the Princess III 母函数

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  8. HDU 1028 Ignatius and the Princess III (递归,dp)

    以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802  Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...

  9. HDU 1028 Ignatius and the Princess III (动态规划)

    题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...

  10. HDU 1028 Ignatius and the Princess III (生成函数/母函数)

    题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...

随机推荐

  1. 从 JavaScript 到 TypeScript

    本文首发在我的个人博客:http://muyunyun.cn/posts/66a54fc2/ 文中的案例代码已经上传到 TypeScript TypeScript 并不是一个完全新的语言, 它是 Ja ...

  2. haproxy+tomcat集群搭建

    web1和web2的部署可参考我之前的文章<Tomcat集群搭建>,这里就省去该过程了. #安装haproxy- .tar.gz cd haproxy-/ make TARGET=linu ...

  3. VB6之HTTP服务器的实现(二)

    接上篇,这次做了小小的改动和提升.增加了对POST的支持和对其他方法(GET和POST之外的)选择405回复.另外,增加了对CGI的支持,目前可以使用C语言来写(是不是好蠢的赶脚).相对于上篇,整体做 ...

  4. 轻量级代码生成器-OnlyCoder 第二篇

    最近利用业余时间将OnlyCoder又重新打造了一番,使其使用起来更简单.更顺手. 相关的帮助文档也已发布了版本. 界面改版,UI采用了DotNetBar2组件. 还是先看下UI效果吧. 由于使用了  ...

  5. ReactiveSwift源码解析(七) Signal的CombineLatest的代码实现

    本篇博客我们就来聊一下combineLatest()的使用以及具体的实现方式.在之前的<iOS开发之ReactiveCocoa下的MVVM>的博客中我们已经聊过combineLatest( ...

  6. 基于封装通用的EF CRUD 的操作

    1.  Entity Framework是Microsoft的ORM框架,随着 Entity Framework 不断的完善强化已经到达了EF 6.0+ 还是非常的完善的,目前使用的比例相对于其他OR ...

  7. Spring Boot 系列(一)快速入门

    简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置 ...

  8. icon button样式(类似windows桌面图标)

    <Style x:Key="IconButton" TargetType="{x:Type Button}"> <Setter Propert ...

  9. SLAM中的优化理论(一)—— 线性最小二乘

    最近想写一篇系列博客比较系统的解释一下 SLAM 中运用到的优化理论相关内容,包括线性最小二乘.非线性最小二乘.最小二乘工具的使用.最大似然与最小二 乘的关系以及矩阵的稀疏性等内容.一方面是督促自己对 ...

  10. effective java 第2章-创建和销毁对象 读书笔记

    背景 去年就把这本javaer必读书--effective java中文版第二版 读完了,第一遍感觉比较肤浅,今年打算开始第二遍,顺便做一下笔记,后续会持续更新. 1.考虑用静态工厂方法替代构造器 优 ...