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

方法有三种:

一、完全背包。

限制条件:所用数字不大于 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. Linux block(1k) block(4k) 换算 gb

    输入 df  显示1k blocks  大小   再输入  df -h  显示 gb换算大小  结论 block(1k) 计算公式为:  block(1k)   /1024/1000  = xx gb ...

  2. 【Gym 100971G】Repair

    BUPT 2017 summer training (for 16) #1B 题意 Alex is repairing his country house. He has a rectangular ...

  3. 【BZOJ5288】[HNOI2018]游戏(拓扑排序)

    [BZOJ5288][HNOI2018]游戏(拓扑排序) 题面 BZOJ 洛谷 题解 去年省选的时候这题给我乱搞整过去整过去了,也是虐心了.... 所以当然是来讲正儿八经的正确做法啦. 很明显,我们需 ...

  4. linux deb系 rpm系 配置永久IP

    rpm: 1.IP a 查看网卡名 ens256 2.uuidgen ens256 生成UUID 3./etc/sysconfig/network-scripts add ifcfg-ens256 4 ...

  5. python查找字符串所有子串

    https://blog.csdn.net/jiangjiang_jian/article/details/79453856 [s[i:i + x + 1] for x in range(len(s) ...

  6. can物理信号-----------显性和隐性

    can信号使用差分电压传送,两条信号线被称为CAN_H和CAN_L.静态时均是2.5v左右,此时状态表示为逻辑“1”,也可以叫做隐性.用CAN_H比CAN_L高表示逻辑“0”,称为显性,此时通常电压值 ...

  7. javascript之奇淫技巧

    最近准备面试,复习一下javascript,整理了一些javascript的奇淫技巧~ //为兼容ie的模拟Object.keys() Object.showkeys = function(obj) ...

  8. Mybatis 缓存失效的几种情况

    1 不在同一个sqlSession对象中 下面比较下载同一个sqlSession和不在同一sqlSession下面的两种情况: 同一sqlSession: @Test public final voi ...

  9. bzoj2252 矩阵距离

    很好奇这种 普及- 的题为什么会是权限题...... 我一开始想用枚举 + 搜索,看书后发现自己脑抽了.直接BFS即可. #include <cstdio> #include <qu ...

  10. js小结

    1,浏览器对json支持的方法: JSON.parse(jsonstr);将string转为json的对象. JSON.stringify(jsonobj);将json对象转为string. 2,js ...