ZOJ1353 Unimodal Palindromic Decompositions解题报告

今天刷题遇见的,觉得有必要记录一下~~

原题如下:


A sequence of positive integers is Palindromic if it reads the same forward and backward. For example:

23 11 15 1 37 37 1 15 11 23

1 1 2 3 4 7 7 10 7 7 4 3 2 1 1

A Palindromic sequence is Unimodal Palindromic if the values do not decrease up to the middle value and then (since the sequence is palindromic) do not increase from the middle to the end For example, the first example sequence above is NOT Unimodal Palindromic while the second example is.

A Unimodal Palindromic sequence is a Unimodal Palindromic Decomposition of an integer N, if the sum of the integers in the sequence is N. For example, all of the Unimodal Palindromic Decompositions of the first few integers are given below:

1: (1)
2: (2), (1 1)
3: (3), (1 1 1)
4: (4), (1 2 1), (2 2), (1 1 1 1)
5: (5), (1 3 1), (1 1 1 1 1)
6: (6), (1 4 1), (2 2 2), (1 1 2 1 1), (3 3), (1 2 2 1), ( 1 1 1 1 1 1)
7: (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)
8: (8), (1 6 1), (2 4 2), (1 1 4 1 1), (1 2 2 2 1), (1 1 1 2 1 1 1), ( 4 4), (1 3 3 1), (2 2 2 2), (1 1 2 2 1 1), (1 1 1 1 1 1 1 1)

Write a program, which computes the number of Unimodal Palindromic Decompositions of an integer.


Input

Input consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end.


Output

For each input value except the last, the output is a line containing the input value followed by a space, then the number of Unimodal Palindromic Decompositions of the input value.


Sample Input

2
3
4
5
6
7
8
10
23
24
131
213
92
0


Sample Output

2 2
3 2
4 4
5 3
6 7
7 5
8 11
10 17
23 104
24 199
131 5010688
213 1055852590
92 331143


这道题说白了就是一道简单的动态规划问题,开始以为是递归的题,结果用递归写了一个,严重超时,连测试数据都过不了╯﹏╰

之后从网上搜了一下,几乎没什么结题报告,(估计是因为太简单,大家都没什么问题<( ̄▽ ̄)> 没办法,暂时水平还比较凹~~)突然看到了教练在题目上写的:“递归的思路,dp的代码”,于是便用dp重写,成功AC,代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
unsigned num[1000][1000];
void f()
{
    memset(num,0,sizeof(num));
    num[1][1]=1;
    num[2][1]=1;
    num[2][2]=1;
    for(int i=3;i<1000;i++)
    {
        if(i%2==0) num[i][i/2]=1;
        num[i][i]=1;
        int m=i/3;
        for(int j=1;j<=m;j++)
        {
            int t=i-2*j;
            for(int k=j;k<=t;k++)
                num[i][j]+=num[t][k];
        }

    }
}
int main()
{
    int n;
    f();
    while(cin>> n&&n!=0)
    {
        unsigned sum=0;
        for(int i=1;i<=n;i++)
        sum+=num[n][i];
        cout << n<<" "<<sum<<endl;
    }
    return 0;
}
思路就是分析一个数的最左边的数,依次存储在num数组中

num数组如下所示:

              1          2          3          4          5          6          7  …

1            1

2            1          1

3            1          0          1

4            2          1          0          1

5            2          0          0          0          1

6            4          1          1          0          0          1

7            3          1          0          0          0          0           1


例如对于7来说:

7=1+5+1

7=2+3+2

7=7

所以7的总数应当是5的总数中最左边的数大于等于1的部分加上3的总数中最左边数大于等于2的部分再加上1,依次递归循环

(注意:存储数据时数据类型应当是unsigned,以免出现大数时不能正常表示)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章