为了字节跳动的面试,复习一波算法

题目描述:输入一个n,输出n的所有组成方案,例如:

n=4,有

4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;

5种方案。

看到网上的很多答案使用母函数之类的。其实这个问题可以用背包的思想解决。

可以理解为有n个物品,第i个物品的容量和价值都为i,而且每个物品可以取任意次。求最后的方案数

众所周知,完全背包的转移方程是F[v]=max{F[v],F[v-C[i]]+W[i]}

计数的话,只需要改成F[v]=sum{F[v],F[v-C[i]]},即F[v]+=F[v-C[i]]

初始化F[0]=1

AC代码:

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

HUOJ 1028 Ignatius and the Princess III(完全背包计数问题)相关推荐

  1. hdu 1398 Square Coins/hdu 1028 Ignatius and the Princess III

    两道母函数的模板题: http://acm.hdu.edu.cn/showproblem.php?pid=1398 View Code #include<iostream>#include ...

  2. HDU 1028 Ignatius and the Princess III

    //强行递推. xx[i][j]表示i数中第j个开头的组合种类. /* 最终结果[i]为 sum of(xx[i][j])  (j from 1  to i); xx[i][j]=sum of (xx ...

  3. hdu 1028 Ignatius and the Princess III 母函数入门

    传送门 文章目录 题意: 思路: 题意: 给你一个数nnn,问你有多少种方案用1−n1-n1−n的数能组成nnn,数的使用次数无限制. n≤120n\le120n≤120 思路: 考虑构造母函数. 对 ...

  4. LETTers比赛第三场 --1002 Ignatius and the Princess III解题报告

    提交人:侯建鹏 提交日期:2012/4/16 1002 Ignatius and the Princess III 解题思路(一):动态规划 先设置int p[N][N]; Dp 方程为:p[i][j ...

  5. HDU 1028 HDU Ignatius and the Princess III

    简单的钱币兑换问题,就是钱的种类多了一点,完全背包. #include<cstdio> #include<cstring> int main () {int i,j,dp[12 ...

  6. HDU-1028 Ignatius and the Princess III(生成函数)

    题意 给出$n$,问用$1$到$n$的数字问能构成$n$的方案数 思路 生成函数基础题,$x^{n}$的系数即答案. 代码 #include <bits/stdc++.h> #define ...

  7. hdu1027 Ignatius and the Princess II (全排列 amp; STL中的神器)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...

  8. hdu-1029 Ignatius and the Princess IV

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目类型: 给一个奇数个列的数组,其中一定存在某个数字,该数字的个数是大于一半的,问这个数字是 ...

  9. E - Ignatius and the Princess IV

    ####E - Ignatius and the Princess IV "OK, you are not too bad, em- But you can never pass the n ...

最新文章

  1. 机器学习/数据挖掘/算法岗位面试题汇总
  2. pyinstaller打包教程及错误RuntimeError: Unable to open ./shape_predictor_68_face_landmarks.dat
  3. python多个日志模块怎么隔离_在多个模块中使用Python日志logging
  4. 使用控件时提示“未声明标识符“的解决方法
  5. php数组的奇数_用php输出一个数组中的偶数或奇数的方法
  6. ZOJ 3829 贪心 思维题
  7. 今年的hsctf里遇到了一个比较少见的nds逆向题目,侥幸拿下一血
  8. 【5分钟一本书】暗时间:用时间管理把握产品的进度条
  9. #1415 : 后缀数组三·重复旋律3 (最长公共子串)
  10. winform程序最大化时,页面显示不全的问题
  11. linux的apache安装在哪个目录,在linux系统下apache的默认安装路径在哪
  12. 四、PHP基础——会话技术Cookie 和 Session
  13. python基础(18)之 异常处理
  14. “贵妇”必备的高价糖水,我给燕窝上了10年智商税
  15. Introspector内存溢出的原理解析
  16. 大龄程序员失业后,看他们是如何破局突围的?
  17. SQL数据库恢复挂起
  18. 逆向研究QCA9563固件,查看和修改GPIO定义
  19. Python使用pyodbc访问数据库操作方法详解
  20. 其实就是为了能有字幕特效,用MeGUI + AVS压制PSP MP4AVC视频02 - 安装准备篇

热门文章

  1. linux升级补丁包,linux 升级补丁
  2. HDFS读写流程(重点,有些面试官总喜欢问到,记录一下)
  3. 2019榆次初三计算机考试时间,2019年晋中中考考试时间安排,晋中中考考试科目科目...
  4. 爱奇艺微服务标准技术架构实践
  5. oppo手机root管家,oppo root软件
  6. 智能手环设计与实现(一)--项目需求
  7. PHP图片表情制作微信QQ斗图生成源码 自适应手机端
  8. git error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)
  9. 几种能量及能量平衡方程
  10. 如何高效地阅读英文文献?