题目链接: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. RandomAccessFile乱码问题

    转自:http://www.cnblogs.com/xudong-bupt/archive/2013/04/20/3028980.html     Thanks Java对文件的读.写随机访问,Ran ...

  2. Mongodb 参数说明及常见错误处理

       在 CentOS7 上安装 MongoDB 1 通过 SecureCRT 连接至 CentOS7 服务器: 2 进入到 /usr/local/ 目录:cd /usr/local 3 在当前目录下 ...

  3. 揭开Socket编程的面纱(留着自己慢慢看)

    对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Socket在哪里呢?3. Socket ...

  4. 使用JDK自带的MessageDigest计算消息摘要

    使用JDK自带的MessageDigest计算消息摘要 上代码 /** * 使用JDK自带MessageDigest */ public class MessageDigestUtils { /** ...

  5. BootLoader--改进(基于2440)

    BootLoader--改进 之前编写的Bootloader启动内核时间使用差不多7秒钟的时间,大多都是用在CPU将内核从Nandflash读取到SDRam中,故首先想到的方法是改变CPU时钟频率. ...

  6. Django学习(七)---添加新文章页面

    在template中添加add_article.html页面 (form  input)请求方法使用post 这个页面涉及到了两个响应函数 1)显示页面的响应函数  2)表单提交的响应函数 add_a ...

  7. jQuery知识点整合

    一.jQuery介绍 jQuery是一个函数库,一个js文件,页面用script标签引入js文件就可以使用 <script type="text/javascript" sr ...

  8. luogu P3398 仓鼠找sugar [LCA]

    题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室(c) ...

  9. VSCode自定义配色方案

    说明 本文更新于2017-07-24,使用VSCode 1.14.1,操作系统为Windows. 配置文件 "文件-首选项-颜色主题"即可显示所有可用的颜色主题,上下选择后Ente ...

  10. Spring源码情操陶冶-AbstractApplicationContext#prepareRefresh

    前言-阅读源码有利于陶冶情操,本文承接前文Spring源码情操陶冶-AbstractApplicationContext 约束: 本文指定contextClass为默认的XmlWebApplicati ...