基础算法 第五周动规2 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 consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end. N<250

输出

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 UNIMODAL PALINDROMIC DECOMPOSITIONS_第1张图片

样例输出

基础算法 第五周动规2 UNIMODAL PALINDROMIC DECOMPOSITIONS_第2张图片
---------------以下是本人的代码---------------
#include
using namespace std;

long long int Record[252][252] = { 0 };//记录数字N中包含几种以i打头的序列
int main()
{
	Record[1][1] = 1; Record[2][1] = 1; Record[2][2] = 1;
	for (int N = 3; N < 250; N++)
	{
		if (N % 2 == 0) Record[N][N / 2] = 1;
		for (int i = 1; 2 * i <= N; i++)//以i打头的序列能拆分成(i, N-2i, i),由于需要升序,所以i只能搭配(N-2i)中大于等于i开头的序列
			for (int j = i; j <= N - 2 * i; j++)
				Record[N][i] += Record[N - 2 * i][j];
		Record[N][N] = 1;//每个元素都包含自身这个组合
	}
	int N = 0;
	cin >> N;
	while (N)
	{
		long long int sum = 0;
		for (int i = 1; i <= N / 2; i++)
			sum += Record[N][i];
		sum++;
		cout << N << " " << sum << endl;
		cin >> N;
	}
	return 0;


你可能感兴趣的:(学习,C++,programming)