Ignatius and the Princess

Description

       "Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.       
"The second problem is, given an positive integer N, we define an equation like this:          N=a[1]+a[2]+a[3]+...+a[m];          a[i]>0,1<=m<=N;        My question is how many different equations you can find for a given N.        For example, assume N is 4, we can find:          4 = 4;          4 = 3 + 1;          4 = 2 + 2;          4 = 2 + 1 + 1;          4 = 1 + 1 + 1 + 1;        so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"

Input

The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.

Output

For each test case, you have to output a line contains an integer P which indicate the different equations you have found.

Sample Input

4
10
20

Sample Output

5
42
627
 
刚看到题目是k个数相加为n的题目,第一反应是求1到n元的一次不定方程求解,用插板的方法就能求出通项,但是考虑到这k元未知数是没有先后关系的,也就是说任意(a,a)和(a,a)是相同的。于是不能这样考虑。
然后考虑到每组解必然有个最大值,于是设了这样一个函数(或者数组)f(n,k),表示和为n且最大数为k的上述元数不定的方程的解的个数。这样一来题目要求求得就是f(n,1)到f(n,n)的和了。
然后我们考虑,对于f(n,k)如果去掉最大值k,那么其子问题就是求和为n-k且最大值小于等于k的上述方程的解的个数。 于是得到递推方程:
但是考虑到n-k或许会比k还要小,于是更新递推方程为:
然后打表求f(n,k)即可,此外用f(n,0)来存所有f(n,i)的和。用数组实现。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <queue>
  10. #include <string>
  11. #include <vector>
  12. #define inf 0x3fffffff
  13.  
  14. using namespace std;
  15.  
  16. int a[125][125];
  17.  
  18. int main()
  19. {
  20. //freopen ("test.txt", "r", stdin);
  21. memset (a, 0, sizeof(a));
  22. for (int i = 1; i <= 120; ++i)
  23. {
  24. for (int j = 1; j <= i; ++j)
  25. {
  26. if (i == 1 || i == j)
  27. {
  28. a[i][j] = 1;
  29. }
  30. else
  31. {
  32. for (int k = 1; k <= j && k <= i-j; ++k)
  33. {
  34. a[i][j] += a[i-j][k];
  35. }
  36. }
  37. a[i][0] += a[i][j];
  38. }
  39. }
  40. int n;
  41. while(scanf("%d", &n) != EOF)
  42. {
  43. printf ("%d\n", a[n][0]);
  44. }
  45. return 0;
  46. }

ACM学习历程—HDU1028 Ignatius and the Princess(组合数学)的更多相关文章

  1. ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)

    Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...

  2. ACM学习历程—HDU2068 RPG的错排(组合数学)

    Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R是公主,P是草儿 ...

  3. 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 ...

  4. HDU1028 Ignatius and the Princess III 【母函数模板题】

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  5. hdu1028 Ignatius and the Princess III(递归、DP)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  6. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  7. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  8. ACM学习历程—HDU5521 Meeting(图论)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...

  9. ACM学习历程—HDU2476 String painter(动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...

随机推荐

  1. php计算两个经纬度地点之间的距离(转)

    php计算两个指定的经纬度地点之间的距离,这个在做计算给定某个地点的经纬度,计算其附近的商业区,以及给定地点与附近各商业区之间的距离的时候,还是用的到的.下面是具体的函数代码以及用法示例. 关于如何获 ...

  2. mysql索引类型normal,unique,full text

    normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用作索引时,可设置为unique full textl: 表示 全文搜索的索引. FULL ...

  3. MagicalRecord使用教程【转载】

    原文地址:http://www.ithao123.cn/content-96403.html 下面是在xcode5.1下ARC环境中的使用教程 1. 将 MagicalRecord 文件夹拖入到工程文 ...

  4. rtems 4.11 部分m4文件分析

    本来想把configure.ac和各种m4文件分析明白,发现有点困难,不过好在也能理解一些. 基本教程 首先要明白m4,参见这个教程,写得不错,不论怎么样m4替换来替换去的,还真是不那么容易懂,好在我 ...

  5. python学习(五)列表

    #!/usr/bin/python # 列表的学习, 列表的概念不陌生, 就是熟悉一下python中的列表是如何操作的 # 1. 序列的操作 L = [ 123, 'spam', 1.23] # 里面 ...

  6. hdu 5071 Chat-----2014acm亚洲区域赛鞍山 B题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 Chat Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. coreos 之flannel

    提要: coreos 中 flannel 工具是coreos 网络划分工具.通过flannel 划分子网并向etcd 注册网络信息.可以做到宿主机集群中容器间网络通信. 1. 启动etcd2 服务: ...

  8. redis客户端连接,最大连接数查询与设置

    ##redis客户端连接数 redis通过监听一个TCP端口或socket的方式接收来自客户端的连接, 当与客户端建立连接后,redis内部会进行如下操作:()客户端socket会被设置为非阻塞模式, ...

  9. liunx 安装工具总结

    1  下载相关文件,比如hadoop 2  解压文件 tar -zxcf xxx.tar.gz 3  mv xxx 到指定目录:通常安装到/usr/local 或者自己建个目录 /usr/develo ...

  10. 【Android】图片(文件)上传的请求分析结构

    怎么在android中上传文件,即怎么用Java向服务器上传文件.上传图片,这是个老问题了,在网上能搜到现成的代码,很多朋友用起来也比较熟了,但是为什么这么写,可能很多朋友并不清楚,这篇文章就来分析一 ...