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];
                }
            }
        }
    }
}
时间: 2024-07-31 09:22:52

HDOJ 1028 Ignatius and the Princess III(递推)的相关文章

hdu 1028 Ignatius and the Princess III (母函数)

点击打开链接 Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16394    Accepted Submission(s): 11552 Problem Description "Well, it seems the first problem is too easy. I

HDU1028-Ignatius and the Princess III

Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12789    Accepted Submission(s): 9045 Problem Description "Well, it seems the first problem is too easy. I will let

hd1028Ignatius and the Princess III【母函数】

Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12832    Accepted Submission(s): 9080 Problem Description "Well, it seems the first problem is too easy. I will let

UVa 10049 Self-describing Sequence:自描述序列&amp;amp;二分递推

10049 - Self-describing Sequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=990 Solomon Golomb's self­describing sequence is the only non­decreasing

“大整数阶乘”问题的递推算法

/* 标题:<<系统设计师>>应试编程实例-[递推算法程序设计] 作者:成晓旭 时间:2002年09月11日(11:52:00-16:26:00) 实现递推算法的大整数阶乘处理函数 时间:2002年09月16日(18:38:00-20:02:00) 实现"斐波那契数列"问题的递推算法函数 */ //:============================"大整数阶乘"问题的递推算法=========================== #d

UVa 10519 !! Really Strange !! (递推)

10519 - !! Really Strange !! Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1460 思路:注意到题目所说"Every two area have exactly two points to intersect and

c++问题-递推 数的划分问题一

问题描述 递推 数的划分问题一 把正整数N分解成M个正整数的和,即使M个数相同但顺序不同也认为是不同的方案,要求总方案数.如3=1+2跟3=2+1是两个不同的方案

递推求解专题练习

hdoj2044--一只小蜜蜂... Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示.   Input 输入数据的第一行是一个整数N,表示测试实例的个数,然后是N 行数据,每行包含两个整数a和b(0<a<b<50). Output 对于每个测试实例,请输出蜜蜂从蜂房a爬到蜂房b的可能路线数,每个实例的输出占一行. Sample Input 2 1 2 3 6 Sam

算法--递推策略

递推法是一种重要的数学方法,在数学的各个领域中都有广泛的运用,也是计算机用于数值计算的一个重要算法.这种算法特点是:一个问题的求解需一系列 的计算,在已知条件和所求问题之间总存在着某种相互联系的关系,在计算时,如果可以找到前后过程之间的数量关系(即递推式),那么,从问题出发逐步推到已 知条件,此种方法叫逆推.无论顺推还是逆推,其关键是要找到递推式.这种处理问题的方法能使复杂运算化为若干步重复的简单运算,充分发挥出计算机擅长于重 复处理的特点. 递推算法的首要问题是得到相邻的数据项间的关系(即递推