acm pku 1221 Unimodal palindromic decompositions的逻辑推理过程
来源:互联网 发布:阿里万网域名注册 编辑:程序博客网 时间:2024/04/25 06:16
UNIMODAL PALINDROMIC DECOMPOSITIONS Description A sequence of positive integers is Palindromic if it reads the same forward and backward. For example: 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 Source Greater New York 2002 分析: 最初拿到这个题目的时候,还是比较茫然的,有点无从下手的感觉,在网上查询了一下,发现该问题就是一个逻辑性的问题,可以做如下推理: 对任意一个正整数N,要想求得其Unimodal palindromic decompositions(UPD),可以先假设N的所有UPD已经分好,则其逻辑如下(用itg[N][i]表示,对正整数N,其UPD分解中,最小的数为大于等于i的不同分解数目), 1) 对任意正整数i(i<N),若对正整数N的分解中存在i,则itg[N][i]可表示为N的分解中最小数等于i和最小数大于i,即大于等于i+1的部分,即: itg[N][i] = itg[N-2i][i] + itg[N][i+1] 2) 初始化的问题,对于整数0的分解,根据1)中的讨论,有itg[0][i] = 1 (i=1,2, . . ., N);另外,当i>N/2时,itg[N][i] = 1,这是显然的; 3) 另外,根据作者遇到的情况,其结果可能超过长整型long,所以需要用到unsigned long型作为结果输出。 代码实现: #include <string.h> #include "iostream" using namespace std; #define N 512 unsigned long itg[N][N]; //最初使用long, 结果WA1次 int main(void) { long n ; int i, j; cin >> n; while(n != 0) { for(i = 0; i <= n; i++) memset(itg[i], 0, sizeof(unsigned long)*N); for(i = 1; i <= n; i++) { itg[0][i] = 1; for(j = i/2+1; j <= i; j++) itg[i][j] = 1; } for(i = 2; i <= n; i++ ) { for(j = i/2; j > 0; j--) itg[i][j] = itg[i-2*j][j] + itg[i][j+1]; } cout << n << " " << itg[n][1] << endl; cin >> n; } return 0; } 提交结果: Problem: 1221 User: uestcshe Memory: 696K Time: 16MS Language: C++ Result: Accepted
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. 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
- acm pku 1221 Unimodal palindromic decompositions的逻辑推理过程
- 1221--UNIMODAL PALINDROMIC DECOMPOSITIONS
- POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS
- POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS
- poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS 动态规划
- poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS(母函数)
- [母函数]POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS
- POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS【DP】
- poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS (母函数)
- poj 1221 dp(UNIMODAL PALINDROMIC DECOMPOSITIONS)
- poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS 整数拆分
- OpenJudge(1221)——UNIMODAL PALINDROMIC DECOMPOSITIONS
- POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS 笔记
- POJ1221 UNIMODAL PALINDROMIC DECOMPOSITIONS
- POJ1121 UNIMODAL PALINDROMIC DECOMPOSITIONS
- poj1221 UNIMODAL PALINDROMIC DECOMPOSITIONS
- ZOJ 1353 Unimodal Palindromic Decompositions
- UNIMODAL PALINDROMIC DECOMPOSITIONS 动态规划
- 触发器
- ubuntu 安装bochs-2.4.5
- 心情
- 数据结构——并查集
- 怎样才能让文本域的格式保持word的格式
- acm pku 1221 Unimodal palindromic decompositions的逻辑推理过程
- JTAG基本原理及仿真器性能比较
- ubuntu vi编辑器方向键出ABCD解决办法
- 二叉树的递归算法
- CentOS 5.3 Linux系统挂载NTFS/FAT32分区
- 我的求职经历
- PROTEL所画PCB各层的意思
- 汇编语言的重要性
- web页面.jq+js特效问题.有中卡卡的感觉