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

编程题#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 }
时间: 2024-12-25 16:55:52

poj 算法基础 编程题#1:UNIMODAL PALINDROMIC DECOMPOSITIONS的相关文章

POJ 算法基础 编程题#2: 滑雪

编程题#2: 滑雪 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长的滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17 18 19

POJ C++程序设计 编程题#10:输出指定结果二

编程题#10:输出指定结果二 来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 总时间限制: 1000ms 内存限制: 1024kB 描述 通过填空使得程序输出的结果符合下面的要求. #include <iostream> #include <map> using namespace std; // 在此处补充你的代码 int A::count = 0; void func(B b) { } int mai

POJ C++程序设计 编程题#4 字符串操作

编程题#4: 字符串操作 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 给定n个字符串(从1开始编号),每个字符串中的字符位置从0开始编号,长度为1-500,现有如下若干操作: copy N X L:取出第N个字符串第X个字符开始的长度为L的字符串. add S1 S2:判断S1,S2是否为0-99999之间的整数,若是则将其转化为整数做加法,若不是,则作字符串加法,返回

POJ C++程序设计 编程题#3 Set 编程作业—STL2

编程题#3:Set 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 5000ms 内存限制: 100000kB 描述 现有一整数集(允许有重复元素),初始为空.我们定义如下操作: add x 把x加入集合 del x 把集合中所有与x相等的元素删除 ask x 对集合中元素x的情况询问 对每种操作,我们要求进行如下输出. add 输出操作后集合中x的个数 del 输出操作前集合中x的个数 ask 先输出0或1表示x是否曾

java基础编程题

java基础编程题 1.打印出如下图案 1 public class Prog19{ 2 public static void main(String[] args){ 3 int n = 5; 4 printStar(n); 5 } 6 7 //打印星星 8 private static void printStar(int n){ 9 //打印上半部分 10 for(int i=0;i<n;i++){ 11 for(int j=0;j<2*n;j++){ 12 if(j<n-i) 1

POJ C++程序设计 编程题#9:人群的排序和分类

编程题#9:人群的排序和分类 来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 总时间限制: 1000ms 内存限制: 1024kB 描述 对人群按照输入的信息进行排序和分类. #include <iostream> #include <set> #include <iterator> #include <algorithm> using namespace std; // 在此处补

POJ C++程序设计 编程题#2 编程作业—文件操作与模板

编程题#2: 实数的输出格式 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 1000kB 描述 利用流操纵算子实现: 输入一个实数,先以非科学计数法输出,小数点后面保留5位有效数字:再以科学计数法输出,小数点后面保留7位有效数字. 注意:在不同系统.编译器上的输出格式略有不同,但保证在程序中采用默认格式设置一定能在OJ平台上得到正确结果. 输入 以非科学计数法表示的一个正实数,保证可以用dou

POJ C++程序设计 编程题#3 编程作业—文件操作与模板

编程题#3: 整数的输出格式 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 1000kB 描述 利用流操纵算子实现: 输入一个整数,先将该整数以十六进制输出,然后再将该整数以10个字符的宽度输出,宽度不足时在左边补0. 注意:在不同系统.编译器上的输出格式略有不同,但保证在程序中采用默认格式设置一定能在OJ平台上得到正确结果. 输入 一个正整数,保证可以用int类型存储. 输出 第一行:以十六进

POJ C++程序设计 编程题#5 计算数组的低3位之和

编程题#5:计算数组的低3位之和 来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 总时间限制: 1000ms 内存限制: 1024kB 描述 输入一个正整数构成的数组a[0], a[1], a[2], ... , a[n-1], 计算它们的二进制低3位之和. #include <iostream> #include <vector> #include <algorithm> using nam