HDOJ 1028 Ignatius and the Princess III(递推)
Problem 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
思路:
(i,j)(i>=j)代表的含义是i为n,j为划分的最大的数字。
边界:a(i,0) = a(i, 1) = a(0, i) = a(1, i) = 1;
i|j==0时,无论如何划分,结果为1;
当(i>=j)时,
划分为{j,{x1,x2…xi}},{x1,x2,…xi}的和为i-j,
{x1,x2,…xi}可能再次出现j,所以是(i-j)的j划分,所以划分个数为a(i-j,j);
划分个数还需要加上a(i,j-1)(累加前面的);
当(i < j)时,
a[i][j]就等于a[i][i];
import java.util.Scanner;
public class Main{
static int a[][] = new int[125][125];
public static void main(String[] args) {
dabiao();
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
System.out.println(a[n][n]);
}
}
private static void dabiao() {
for(int i=0;i<121;i++){
a[i][0]=1;
a[i][1]=1;
a[0][i]=1;
a[1][i]=1;
}
for(int i=2;i<121;i++){
for(int j=2;j<121;j++){
if(j<=i){
a[i][j]=a[i][j-1]+a[i-j][j];
}else{
a[i][j]=a[i][i];
}
}
}
}
}
HDOJ 1028 Ignatius and the Princess III(递推)的更多相关文章
- HDOJ 1028 Ignatius and the Princess III (母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdoj 1028 Ignatius and the Princess III(区间dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 思路分析:该问题要求求出某个整数能够被划分为多少个整数之和(如 4 = 2 + 2, 4 = 2 ...
- 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 整数的划分问题(打表或者记忆化搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Time Limit: 2000/1 ...
- hdu 1028 Ignatius and the Princess III(DP)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 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 (n的划分)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028 Ignatius and the Princess III伊格和公主III(AC代码)母函数
题意: 输入一个数n,求组合成此数字可以有多少种方法,每一方法是不记录排列顺序的.用来组成的数字可以有1.2.3....n.比如n个1组成了n,一个n也组成n.这就算两种.1=1,2=1+1=2,3= ...
随机推荐
- 【原创】FAT32系统硬盘的数据恢复实例,这篇文章也是从自己QQ空间的,转移过来的
FAT32系统硬盘的数据恢复实例软件:win hex(16进制的编辑器)恢复内容:在FAT32中恢复一张图片1.首先我们格式化一个分区为空,让分区的系统选为FAT32系统格式化.如下: psb.jpg ...
- Linux磁盘管理:lvcreate 常用命令
查看当前LV及PV信息: [root@rusky ~]# hostnamectl Static hostname: localhost.localdomain Transient hostname: ...
- Apache MINA NioSocketAcceptor类的实现
NioSocketAcceptor 继承AbstractPollingIoAcceptor,实现SocketAcceptor接口 public final class NioSocketAccepto ...
- Python进阶:函数式编程实例(附代码)
Python进阶:函数式编程实例(附代码) 上篇文章"几个小例子告诉你, 一行Python代码能干哪些事 -- 知乎专栏"中用到了一些列表解析.生成器.map.filter.lam ...
- node.js动态调试
supervisor node ./bin/www npm start 不会动态运行和调试程序,相当于动态部署
- HTML 学习笔记
1HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. 并且只有这6种标题,标题中加多个空格,和一个空格没区别,标题文字前后加默认空格会被去除. ...
- 二、T4模板
上文带大家见识了下T4,这里呢开始介绍T4模板有关的内容.关于T4模板介绍和使用网上一搜一箩筐,想深入研究的可以自行去找些资料,这里只介绍接下来我们需要使用的一些知识,不会面面俱到的讲T4模板所有的知 ...
- c#字符串驻留机制
http://www.cnblogs.com/instance/archive/2011/05/24/2056091.html
- iOS GorupBy
转自: IOS 数组分组 Grouped NSArray 1 2 3 4 5 6 7 8 NSMutableSet *set=[NSMutableSet set]; [_list enumera ...
- iOS横竖屏切换的一些坑(持续更新)
最近在做视频类的App,遇到视频滚动播放的坑,紧接着就是横竖屏问题.之前太过天真不想做横竖屏配置.只是想旋转视频View,但是分享什么的包括AlertView还是竖屏样式,项目着急上线(1周提交一次也 ...