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

方法有三种:

一、完全背包。

限制条件:所用数字不大于 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. 直接使用security.basic.path无效|——springboot2.0以上的security的配置

    问题 springcloud 版本 为 Finchley.RELEASEspringboot 版本为 2.0.3.RELEASE 现在有需求,/swagger-ui.html 页面需要添加登录认证,但 ...

  2. Git初始化及配置

    >>>>Git简介 >>>>官网下载Git >>>>安装,一路next 安装成功后,鼠标右键里就有Git bash here和G ...

  3. 使用 Zabbix 监控 Jenkins

    笔者最近的工作涉及到使用 Zabbix 监控 Jenkins.在谷歌上搜索到的文章非常少,能操作的就更少了.所以决定写一篇文章介绍如何使用 Zabbix 监控 Jenkins. 下图为整体架构图: 整 ...

  4. markdown语法测试集合

    这篇文章包含markdown语法基本的内容, 目的是放在自己的博客园上, 通过开发者控制台快速选中, 从而自定义自己博客园markdown样式.当然本文也可以当markdown语法学习之用. 在mar ...

  5. luogu5007 DDOSvoid 的疑惑 (树形dp)

    我们来算每个点出现在的集合的个数 设f[i]为i出现的集合个数,g[i]是只选子树i 可以有多少种选法 那就有$g[i]=1+\prod\limits_{j是i的孩子}{g[j]} , f[i]=f[ ...

  6. 逗号分隔的字符串与List互转

    将逗号分隔的字符串转换为List // 将逗号分隔的字符串转换为List String str = "a,b,c"; // 1.使用JDK,逗号分隔的字符串-->数组--&g ...

  7. A1122. Hamiltonian Cycle

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  8. 【洛谷P1403】约数研究

    题目大意:求\[\sum\limits_{i=1}^n\sum\limits_{d|i}1\] 题解:交换求和顺序即可. \[\sum\limits_{i=1}^n\sum\limits_{d|i}1 ...

  9. 用宏定义代替printf函数

    来自:http://blog.csdn.net/yannanxiu/article/details/52506451 #define _DEBUG_ 1 #if _DEBUG_ #define PR( ...

  10. android studio adb.exe已停止工作(全面成功版 进程的查询和开启)

    先输入adb看是否存在. 如果不存在则:在系统path里添加C:\Users\nubia\AppData\Local\Android\sdk\platform-tools 因为这个目录里有adb 或者 ...