题意是求所给的数能够被拆分成的不同组合数目。

方法有三种:

一、完全背包。

限制条件:所用数字不大于 n。

目标:求分解种数(组合出 n 的方法数)。

令 dp[ i ][ j ] = x 表示 用前 i 种数字组合出数字 j 有 x 种方法。

状态转移方程:dp[ i ][ j ] = dp[ i -1 ][ j ] + dp[ i ][ j - num[i] ]

方程解释:前 i 种数字组合出数字 j 的方法数 = 前 i - 1 种数字组合出数字 j 的方法数(不用第 i 种数字)+ 至少用一次第 i 种数字的方法数。

用滚动数组求解,代码如下:

 #include <bits/stdc++.h>
using namespace std;
int dp[];
void init()
{
dp[] = ;
for(int i = ; i <= ; ++i)
for(int j = i; j <= ; ++j)
dp[j] += dp[j-i];
}
int main()
{
int n;
init();
while(~scanf("%d",&n))
printf("%d\n",dp[n]);
return ;
}

二、分治。

令 sol(a, b) 表示 a 被最大数字为 b 的数字分解成的种数。则

当 a == 1 && b == 1 时,只能分解成 1 种;

当 a < 1 || b < 1 时,一种也没有,即只能分解成 0 种;

当 a == b 时,则 分解种数 = 含 b 的数字分解种数(仅 1 种) +  不含 b 的数字分解种数,即 sol(a, b) = 1 + sol(a, b - 1);

当 a < b 时,则 分解种数 = 最大数字为 a 的分解种数,即 sol(a, b) = sol(a, a);

当 a > b 时,则 分解种数 = 没有 b 的数字分解种数 + 至少含有 1 个 b 的数字分解种数,即 sol(a, b) = sol(a, b-1) + sol(a-b, b)。

打表求解,代码如下:

 #include <bits/stdc++.h>
using namespace std;
int a[]={,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,
,,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,};
//数组的求解方法
//int sol(int a,int b)
//{
// if(a==1||b==1) return 1;
// else if(a<1||b<1) return 0;
// else if(a==b) return sol(a,b-1)+1;
// else if(a>b) return sol(a,b-1)+sol(a-b,b);
// else if(a<b) return sol(a,a);
//}
int main()
{
int n;
while(~scanf("%d",&n))
printf("%d\n",a[n]);
return ;
}

三、母函数。

本题的做法与 HDU 1284 类似,要查看母函数的相关讲解请点这里

HDU 1028(数字拆分 分治)的更多相关文章

  1. hdu acm 1028 数字拆分Ignatius and the Princess III

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

  2. Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数

    Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...

  3. HDU 1028 整数拆分 HDU 2082 找单词 母函数

    生成函数(母函数) 母函数又称生成函数.定义是给出序列:a0,a1,a2,...ak,...an, 那么函数G(x)=a0+a1*x+a2*x2+....+ak*xk +...+an* xn  称为序 ...

  4. hdu,1028,整数拆分的理解

    #include"iostream"using namespace std;int main() { int n,i,j,k; int c[122],temp[122]; //c[ ...

  5. HDU 1028 整数拆分问题 Ignatius and the Princess III

    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 (生成函数/母函数)

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

  7. ACM: HDU 1028 Ignatius and the Princess III-DP

     HDU 1028 Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Form ...

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

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

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

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

随机推荐

  1. 【XSY1545】直径 虚树 DP

    题目大意 ​ 给你一棵\(n\)个点的树,另外还有\(m\)棵树,第\(i\)棵树与原树的以\(r_i\)为根的子树形态相同.这\(m\)棵树之间也有连边,组成一颗大树.求这棵大树的直径长度. \(n ...

  2. 用webpack2.0构建vue2.0单文件组件超级详细精简实例

    npm init -y 初始化项目  //-y 为自动生成package.json,如果需要自行配置,去掉-y即可 安装各种依赖项 npm install --save vue 安装vue2.0 np ...

  3. 【Gym - 100947G】Square Spiral Search

    BUPT 2017 summer training (for 16) #1C 题意 A new computer scientist is trying to develop a new memory ...

  4. 【 HDU4773 】Problem of Apollonius (圆的反演)

    BUPT2017 wintertraining(15) #5G HDU - 4773 - 2013 Asia Hangzhou Regional Contest problem D 题意 给定两个相离 ...

  5. Nginx 添加 PHP 支持

    背景介绍默认安装的Nginx是无法打开php文件的,需要修改相关配置才能支持php 安装yum -y install epel-release yum -y install nginx yum ins ...

  6. LOJ #2537. 「PKUWC 2018」Minimax (线段树合并 优化dp)

    题意 小 \(C\) 有一棵 \(n\) 个结点的有根树,根是 \(1\) 号结点,且每个结点最多有两个子结点. 定义结点 \(x\) 的权值为: 1.若 \(x\) 没有子结点,那么它的权值会在输入 ...

  7. 「SCOI2016」美味 解题报告

    「SCOI2016」美味 状态极差无比,一个锤子题目而已 考虑每次对\(b\)和\(d\)求\(c=d \ xor \ (a+b)\)的最大值,因为异或每一位是独立的,所以我们可以尝试按位贪心. 如果 ...

  8. 通俗讲解MOSFET

    一位工程师曾经对我讲,他从来不看MOSFET数据表的第一页,因为“实用”的信息只在第二页以后才出现.事实上,MOSFET数据表上的每一页都包含有对设计者非常有价值的信息.但人们不是总能搞得清楚该如何解 ...

  9. rxjs学习笔记

    api List Rx.Observable.amb(...args) -存在竞争关系,amb里的流只能触发一个,并且忽略其他未处理的流. eq: <body> <input id= ...

  10. 洛谷T31039 九尾狐吃棉花糖

    小伙伴出的题. 一眼看出是状压DP裸题.回忆poj2288 islands and bridges,然后就很好写了. 啪啪啪打了个状压DP出来(晚上寝室写的,其实是记忆化搜索),发现sum总是INF ...