https://www.luogu.org/problemnew/show/P1028

只用递归会超时,需要用递归型动规,用一个数组保存已经算过的值,避免重复计算。

求数字为n的方案数的最优子结构为:求数字从1到n/2的方案数之和再加上这个数字本身,即fn=f1+f2+...+f(n/2)+1,

边界为:1的符合条件的方案数只有它自己,即f1=1.

例:

f(6)=f(1)+f(2)+f(3)+1=1+2+2+1=6.

f1=1,f2=f1+1=1+1=2,f3=f1+1=1+1=2。

 #include<iostream>
#include<cstring>
using namespace std;
//int cnt = 1;
int dp[];
int f(int n)//数字为n的方案数
{
if (n == )//1满足条件的只有它自己
{
dp[n] = ;
return dp[n];
}
if (dp[n] != )
{
return dp[n];
}
int e = n / ;
int sum = ;
for (int i = ; i <= e; ++i)
{
sum += f(i);
}
dp[n] = sum + ;//不为1的方案数除了从1到n/2的方案数之和还得加上这个数字自己
return dp[n];
}
int main()
{
int n;
memset(dp, , sizeof(dp));
cin >> n; cout << f(n)<< endl;
return ;
}

最新文章

  1. 用 Blend 给Windows Phone 应用创建 示例数据
  2. 当年只会C# 所以写C++就成这样了! log4cplus -&gt; log4net
  3. JavaScript 命名空间
  4. shell text process code
  5. GridView中的GridView1_RowCommand事件
  6. MyBatis 元素类型为 &quot;configuration&quot; 的内容必须匹配 &quot;.....
  7. 南阳理工ACM954--N!
  8. mysql补充(1)校对集utf8_unicode_ci与utf8_general_ci
  9. RH133读书笔记(2)-Lab 2 Working with packages
  10. how tomcat works 札记(两)----------一个简单的servlet集装箱
  11. HTML5和CSS3
  12. 让你快速了解并掌握如何进行iOS开发技能
  13. Phpstrom操作Git从服务器端克隆代码到本地
  14. java返回数据工具类
  15. Java中栈的应用,括号匹配
  16. base | AtomicIntegerT类
  17. Node_初步了解(4)小爬虫
  18. 怎么归档老日志的shell脚本
  19. 玩转X-CTR100 | X-PrintfScope波形显示
  20. mstsc本地驱动器

热门文章

  1. FastDFS的实现
  2. 【FFmpeg】FFmpeg常用基本命令(转载)
  3. P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)
  4. LOJ#510. 「LibreOJ NOI Round #1」北校门外的回忆(线段树)
  5. c++ swap函数
  6. ASP.Net 知识点总结(四)
  7. [NOIP2004]火星人
  8. rman 问题
  9. [转]微信开发.Net 接入示例
  10. SQL编程语句