pku acm 1221
来源:互联网 发布:java如何成为架构师 编辑:程序博客网 时间:2024/06/05 02:30
UNIMODAL PALINDROMIC DECOMPOSITIONS
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2580 Accepted: 1200
Description
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.
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. See the example on the next page.
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
题解只写给新手看,大牛略过。
最近一直在作动态规划的题,但这道还是想了好久,水啊。
先研究一下每一组数据时怎样由前面的推出的。
比如 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个。
将6拆成两个相同的数和另一个大于这个数的数或者0(真拗口)。直接看下面就好:
1、6=1+1+4。那么他可以将4的所有情况两边都加1变成6的解 (1 4 1), (1 1 2 1 1), (1 2 2 1), ( 1 1 1 1 1 1)共4个。
2、6=2+2+2。那么他可以将2的所有最小值大于等于2的情况两边加2 即将(2) 变成 (2,2,2) 共1个
3、6=3+3+0。注意只有另一个数是0的时候才可以这么拆 如 5= 2+2+1 是不可以的。那么他可以将0 的所有情况两边加3,变成3,3 共1个。这里默认0的情况总数是1.
4、6本身自己 (6) 就是一组 共1个
所以 6的解共4+1+1+1=7个。
由以上分析得到状态转移方程 s[i][j] = s[i-2*j][j] + s[i][j+1];(1<=j<=i/2)
其中i表示数据的和,就是题中的n,j表示i的所有解中所有数大于等于j的情况总数(之所以这样定义可以参考上面分析中的第2步)
具体实现
1、将s[i][j] (i>=j>i/2) 内容预处理成1(因为对于j>i/2时 所有的s[i][j]都是1,如s[6][4]=s[6][5]=s[6][6]=1 只有(6)一个)。
2、将s[i][j] (i<j) 内容预处理成0 (因为根据定义j不可能比i大。)
3、将s[0][j] 内容预处理成1 (因为当需要调用s[0][j]时表示一个数拆成了完全相同的两个数,结果当然是一个了,)
4、在for (i=2;i<=nax;i++)的每次循环中加入for (j=i/2;j>=1;j--)循环,每次逆推结果
程序实现过程
比如i=6时
1、j=3 s[6][3] = s[6-3*2][3] + s[6][4] = 1 + 1 =2 ; 表示6中解大于3的有两种(3,3) (6);
2、j=2 s[6][2] = s[2][2] + s[6][3] = 1 + 2 =3;
3、j=1 s[6][1] = s[4][1] + s[6][2] = 4 + 3 =7; s[6][1]中储存n=6时的所有解
代码:
- pku acm 1221
- pku-acm
- ACM-PKU
- PKU ACM 题目分类
- acm pku 1088 滑雪
- PKU ACM 1579
- PKU ACM 1163
- PKU ACM 1125
- PKU ACM 2081
- Pku acm 2250 Compromise
- PKU ACM 1004
- PKU ACM 1045
- PKU ACM 1046
- PKU ACM 1003
- acm PKU题型分类
- 滑雪-PKU ACM 1088
- ACM PKU 1177 Picture
- PKU ACM 1159-Palindrome
- 为何我用System.IO.Compression.GZipStream解压缩,速度这么慢?
- poj 2413 java How many Fibs?
- 高精度计算 poj acm 1503 java
- android sdk安装问题——a folder failed to be renamed or moved on SDK install的处理方法
- [转载]java.toString() ,(String),valueOf
- pku acm 1221
- pku acm 1183
- PKU ACM 1218 java THE DRUNK JAILER
- 新浪博客整体搬家!!!!
- pku acm 1922 Ride to School
- PKU ACM 2013 java Symmetric Order
- PKU ACM-1029题 java False coin
- PKU ACM-1013题 java Counterfeit Dollar
- PKU ACM-1008题 java Maya Calendar