基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
 收藏
 关注
将N分为若干个不同整数的和,有多少种不同的划分方式,例如:n = 6,{6} {1,5} {2,4} {1,2,3},共4种。由于数据较大,输出Mod 10^9 + 7的结果即可。

Input
  1. 输入1个数N(1 <= N <= 50000)。
Output
  1. 输出划分的数量Mod 10^9 + 7
Input示例
  1. 6
Output示例
  1. 4

这个DP想得真叫我累啊,还想不出来。后来问了夹克老师,发现这个DP真是经典啊。

用dp[i][j]表示j个数组成i的个数。那么对于dp[i][j]每一个j来说,注意是对于每一个j来说,来源就是两个,一个就是插入j时的dp[i-j][j-1]个数。

然后就是因为要不同整数,不同的来源在哪里,就是将[i-j][i](其含义是i个数组成了i-j)中的每一个数加1,这点这是奇妙啊,就组成了dp[i][j]的剩余部分。

所以dp[i][j]=dp[i-j][i]+dp[i-j][i-1]

这个题真是好题。

代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <vector>
  5. #include <string>
  6. #include <cstring>
  7. #pragma warning(disable:4996)
  8. using namespace std;
  9.  
  10. const int maxn = 50000 + 100;
  11. const int mod = 1e9 + 7;
  12. int dp[maxn][350];
  13.  
  14. int main()
  15. {
  16. //freopen("i.txt","r",stdin);
  17. //freopen("o.txt","w",stdout);
  18.  
  19. int n;
  20. scanf("%d", &n);
  21. dp[0][0] = 1;
  22. for (int i = 1; i<7; i++)
  23. {
  24. for (int j = 0; j <= n; j++)
  25. {
  26. if (j - i >= 0)
  27. dp[j][i] = (dp[j - i][i] + dp[j - i][i - 1]) % mod;
  28. }
  29. }
  30. int ans = 0;
  31. for (int i = 1; i<350; i++)
  32. ans = (ans + dp[n][i]) % mod;
  33. cout << ans << endl;
  34.  
  35. return 0;
  36. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

51nod 1201:整数划分 超级好的DP题目的更多相关文章

  1. 51nod 1201 整数划分 dp

    1201 整数划分 基准时间限制:1 秒 空间限制:131072 KB   收藏  关注 将N分为若干个不同整数的和,有多少种不同的划分方式,例如:n = 6,{6} {1,5} {2,4} {1,2 ...

  2. 51nod 1201 整数划分 基础DP

    1201 整数划分  基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 将N分为若干个不同整数的和,有多少种不同的划分方式,例如:n = 6,{6} ...

  3. 51Nod 1201 整数划分 (经典dp)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1201 题意不多说了. dp[i][j]表示i这个数划分成j个数 ...

  4. 51nod 1201 整数划分

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1201 DP转移方程:dp[i][j] = dp[i-j][j]+dp[i ...

  5. 51nod p1201 整数划分

    1201 整数划分 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 将N分为若干个不同整数的和,有多少种不同的划分方式,例如:n = 6,{6} {1,5} {2, ...

  6. NYOJ90 整数划分(经典递归和dp)

    整数划分 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 将正整数n表示成一系列正整数之和:n=n1+n2+…+nk,  其中n1≥n2≥…≥nk≥1,k≥1.  正 ...

  7. 【51NOD】1201 整数划分

    [题意]将n划分成不同正整数的和的方案数. [算法]动态规划 [题解] 暴力:f[i][j]:只用前1..i的数字,总和为j的方案数 本质上是01背包,前i个物体,总质量为j的方案数 f[i][j]= ...

  8. 51nod1201 整数划分

    01背包显然超时.然后就是一道神dp了.dp[i][j]表示j个数组成i的方案数.O(nsqrt(n)) #include<cstdio> #include<cstring> ...

  9. HDU 5230 ZCC loves hacking 大数字的整数划分

    http://acm.hdu.edu.cn/showproblem.php?pid=5230 把题目简化后,就是求 1---n - 1这些数字中,将其进行整数划分,其中整数划分中不能有重复的数字,如果 ...

随机推荐

  1. 六、ibatis1.2.8查询性能优化,实现百万数据zip导出

    经测试发现将查询的结果100万数据(池子中共有大概14亿的数据)写入Excle文件并进行压缩导出zip文件最耗时的地方竟然在查询,因此本篇文章主要是针对如何在spring+ibatis1.2.8中优化 ...

  2. tomcat安装apr报错解决

    参考http://www.cnblogs.com/nuccch/p/7598361.html 1.no c complie 安装gcc解决 2.rm: cannot remove `libtoolT' ...

  3. Mac安装jdk

    jdk:https://blog.csdn.net/zw235345721/article/details/78702254 mysql:https://www.jianshu.com/p/fd3aa ...

  4. Day3-M-Cable master POJ1064

    Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...

  5. Day2-K-Red and Black-HDU1312

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  6. dstat 监控命令详解

    一.工具介绍 dstat的man手册对于该工具的解释: dstat - versatile tool for generating system resource statistics 系统资源多用途 ...

  7. python3列表操作

    1.Python列表脚本操作符 2.Python列表截取 切片的公式:[start : end : step] 1)切片的取值: list1 = [1, 4, 9, 16, 25] print(lis ...

  8. python类的对象使用

    class student():    def __init__(self,age,sex):        self.age = age        self.sex = sex  #self._ ...

  9. 连接数据库 - (mysql-thinkphp) (2)

    1.现在conf里面写好选择的数据库 选择好了以后 2.在index里面输入 查询mysql数据库里面的表tables_priv的所有数据 public function index() { $res ...

  10. api文档方法参数

    in型参数,带信息进去用: out型参数,方法执行结束,带着信息出来 如: CreateProcessW(     _In_opt_ LPCWSTR lpApplicationName,     _I ...