pku acm 1221
2011-02-27 02:00
176 查看
UNIMODAL PALINDROMIC DECOMPOSITIONS
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.
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2580 | Accepted: 1200 |
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. 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时的所有解
代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> __int64 s[301][301]; int n; int main() { int i,j,t; memset(s,0,sizeof(0)); for (i=1;i<=300;i++) for (j=i;j>=0;j--) s[i][j] = 1; for (i=0;i<=300;i++) s[0][i]=1; for (i=2;i<=300;i++) for (j=i/2;j>=1;j--) { s[i][j] = s[i-2*j][j] + s[i][j+1]; } while (scanf("%d", &n), n!=0) { printf("%d %I64d/n", n, s [1]); } }[/code]
相关文章推荐
- 整数分解和划分 - 兼 ACM PKU POJ 1221 解题报告
- acm pku 1221 Unimodal palindromic decompositions的逻辑推理过程
- PKU ACM 1125
- ACM PKU 题目分类(完整整理版本)
- PKU ACM 1046
- acm pku 1068 Parencodings的实现
- PKU ACM 2479-Maximum sum
- http://acm.pku.edu.cn/JudgeOnline/problem?id=1131 八进小数转十进制小数
- PKU ACM- 1007 题 java DNA Sorting
- 有向图的汇点 -- 兼 ACM PKU POJ 2186 ( Popular Cows ) 解题报告
- pku acm 2488 源码
- pku acm 1023
- pku acm 1010
- PKU ACM 3274 数组hash
- Pku acm 1014 Dividing 动态规划题目解题报告(十七)
- Pku acm 2075 Tangled in Cables数据结构题目解题报告(十一)最小生成树:prim算法&amp;amp;二叉查找树
- PKU ACM 1012 JOSEPH问题
- ACM基本算法分类、推荐学习资料和配套pku习题
- 约瑟夫 (Joseph) -- ACM PKU 1012 解题报告
- http://acm.pku.edu.cn/JudgeOnline/problem?id=2891 欧几里德线性同余方程