HDU 1028(数字拆分 分治)
题意是求所给的数能够被拆分成的不同组合数目。
方法有三种:
一、完全背包。
限制条件:所用数字不大于 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(数字拆分 分治)的更多相关文章
- 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 ...
- Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数
Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...
- HDU 1028 整数拆分 HDU 2082 找单词 母函数
生成函数(母函数) 母函数又称生成函数.定义是给出序列:a0,a1,a2,...ak,...an, 那么函数G(x)=a0+a1*x+a2*x2+....+ak*xk +...+an* xn 称为序 ...
- hdu,1028,整数拆分的理解
#include"iostream"using namespace std;int main() { int n,i,j,k; int c[122],temp[122]; //c[ ...
- HDU 1028 整数拆分问题 Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028 Ignatius and the Princess III (生成函数/母函数)
题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...
- 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 ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- HDU 1028 Ignatius and the Princess III (递归,dp)
以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802 Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...
随机推荐
- 【XSY1515】【GDKOI2016】小学生数学题 组合数学
题目描述 给你\(n,k,p\)(\(p\)为质数),求 \[ \sum_{i=1}^n\frac{1}{i}\mod p^k \] 保证有解. \(p\leq {10}^5,np^k\leq {10 ...
- 【XSY2519】神经元 prufer序列 DP
题目描述 有\(n\)点,每个点有度数限制,\(\forall i(1\leq i\leq n)\),让你选出\(i\)个点,再构造一棵生成树,要求每个点的度数不超过度数限制.问你有多少种方案. \( ...
- hdu 5510 Bazinga (KMP+暴力标记)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 思路: 一开始直接用KMP莽了发,超时了,后面发现如果前面的字符串被后面的字符串包含,那么我们就 ...
- MongoDB 数据恢复与导出
MongoDB登录mongo --host localhost --port 27017 -uroot -pdbpasswd --authenticationDatabase admin查看所有dbs ...
- 「TJOI2015」线性代数 解题报告
「TJOI2015」线性代数 和牛客某题很像 在和里面有\(B_{i,j}\)要求是\(A_i,A_j\)都为\(1\),和里面减去\(C_i\)要求\(A_i\)为\(1\),然后先把贡献也就是\( ...
- One-hot encoding 独热编码
http://blog.sina.com.cn/s/blog_5252f6ca0102uy47.html
- 洛谷P4553 80人环游世界
题目描述 https://www.luogu.org/problemnew/show/P4553 题解 思路比较显然,把图建出来,一个国家拆成两个点,中间设置上下界,然后跑费用流. 我把源那边的流量也 ...
- JSP、EL、JSTL
JSP(Java Server Pages) 什么是JSP Java Server Pages(Java服务器端的页面) 使用JSP:SP = HTML + Java代码 + JSP自身的东西.执行J ...
- Quick Guide to Microservices with Spring Boot 2.0, Eureka and Spring Cloud
https://piotrminkowski.wordpress.com/2018/04/26/quick-guide-to-microservices-with-spring-boot-2-0-eu ...
- LOJ#2541 猎人杀
解:step1:猎人死了之后不下台,而是继续开枪,这样分母不变...... 然后容斥,枚举猎人集合s,钦定他们在1之后死.定义打到1的时候结束,枚举游戏在i轮时结束. 发现式子是一个1 + x + x ...