ZOJ 1353 Unimodal Palindromic Decompositions(动态规划)

Unimodal Palindromic Decompositions

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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

 

动态规划 递推

观察12的单调回文分解形式中所有开头和结尾均为2的分解:

282

22422

222222

2442

假设用d[n][i]表示将n分解成单调回文中最左边的数为i的个数,例如d[12][2]表示将12分解成单调回文中最左边的数为2的个数,它的值等于以下各项之和:

d[12-2*2][2]:已经将12分解成了2...2这种形式,所以这项表示将8分解成单调回文中最左边的数为2的个数

d[12-2*2][3]:这项表示将8分解成单调回文中最左边的数为3的个数

...

 递推公式为:d[n][i] += d[n-2*i][j] (0<i<n/3  0<j<=i)

 初始条件:d[i][i] = 1 (0<i<=n)  如果i是偶数,比如12可以表示成{6,6},所以d[i][i/2] = 1;

View Code
 1 # include<stdio.h>
 2  # include<string.h>
 3  # define maxn 512
 4  
 5  long long  d[maxn][maxn];
 6  
 7  int main(){
 8      memset(d,0,sizeof(d));
 9      int i,j;
10      //初始化
11      for(i=1;i<maxn;i++)
12      {
13          d[i][i]=1;
14          if(i%2==0) d[i][i/2]=1;  //如果i是偶数,则可分解成i/2i/2这种形式
15      }
16      d[2][1]=1;
17      d[3][1]=1;
18      d[4][1]=2;
19      d[4][2]=1;
20      
21      for(i=5;i<maxn;i++)
22      {
23          for(j=1;j<maxn;j++)
24          {
25              if(i-2*j>=j)
26              {
27                  for(int m=j;m<=i-2*j;m++)
28                      d[i][j] += d[i-2*j][m];
29              }
30              else
31                  break;
32          }
33      }
34      int n;
35      while(scanf("%d",&n) && n)
36      {
37          long long  ans=0;
38          for(i=1;i<=n;i++)
39              ans += d[n][i];
40          printf("%d %lld\n",n,ans);
41      }
42      return 0;
43  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值