poj 算法基础 编程题#1:UNIMODAL PALINDROMIC DECOMPOSITIONS

本文介绍了一种名为“单峰回文分解”的数学问题及其算法解决方案,该问题要求找出所有可能的单峰回文序列,这些序列的元素之和等于给定的整数N。文章提供了详细的算法实现步骤,并附上了完整的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编程题#1:UNIMODAL PALINDROMIC DECOMPOSITIONS

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

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 consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end.

 

输出

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. See the example on the next page.

样例输入

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

 

样例输出

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

 

提示

N < 250

 


 1 #include<iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int N;
 5 long upNums[251][251];
 6 
 7 //计算最大数小于max和为N上升数列的组合数
 8 long upNum(int N, int max) {
 9     long count = 0;
10     if (upNums[N][max] != -1) return upNums[N][max]; // 如果储存了,直接return
11     if (max == 1 || N == 0) {
12         upNums[N][max] = 1;
13         return 1;
14     }
15     if (max < 1) {//最大数不能小于1
16         upNums[N][max] = 0;
17         return 0;
18     }
19     if (max > N) {// 如果最大数大于和,那么转化为求upNum(max, max)
20         upNums[N][max] = upNum(N,N);
21         return upNums[N][N];
22     }
23     for (int i = 1; i <= max; ++i) {
24         count += upNum(N-i, i);
25     }
26     upNums[N][max] = count;
27     return count;
28 }
29 
30 //计算和为N的单峰序列组合数
31 long conbinationNum(int N) {
32     bool even = (N + 1) % 2;//判断N是奇数还是偶数
33     long count = 0;
34     for (int i = 1; i <= N; ++i) {
35         int base, numOfI = 1;
36         if (even) {
37             if ((i % 2)) {
38                 base = 2; //如果N是偶数,i是奇数,那么中间i的个数只能是偶数个
39                 numOfI = 2;
40             } else { // 如果N是偶数,i是偶数,那么中间i的个数可以数奇数个也可以是偶数个
41                 base = 1;
42             }
43             while ((i * numOfI) <= N) {
44                 count += upNum((N- (i * numOfI))/2, i - 1);
45                 numOfI += base;
46             }
47         } else {
48             if ((i % 2)) {
49                 base = 2;//如果N是奇数,i只能是奇数,那么中间i的个数只能是奇数个
50                 while ((i * numOfI) <= N) {
51                     count += upNum((N- (i * numOfI))/2, i - 1);
52                     numOfI += base;
53                 }
54             }
55         }
56     }
57     return count;
58 }
59 
60 int main()
61 {
62     for (int i = 0; i < 251; ++i) {
63         for (int j = 0; j < 251; ++j) {
64             upNums[i][j] = -1;
65         }
66     }
67     cin>>N;
68     while(N) {
69         cout<<N<<" "<<conbinationNum(N)<<endl;
70         cin>>N;
71     }
72     return 0;
73 }

 

转载于:https://www.cnblogs.com/dagon/p/4852625.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值