UNIMODAL PALINDROMIC DECOMPOSITIONS

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5430   Accepted: 2641

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

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

题意:

    给一个正整数,求出它的Unimodal Palindromic的个数,所谓的Unimodal Palindromic就是一系列数,单调递增再递减,并且第一个和最后一个数相同,第二个跟倒数第二个数相同,即第i个跟第n-i+1个数相同。

 

思路:

把它的Unimodal Palindromic分成两部分:一部分是最小数是j的,就是第一个跟最后一个数等于j的有几个;第二部分是最小数大于j的,可以是j+1,j+2…..的有几个。

dp[i][j]表示和为i,最小数是j的序列的个数。那么第一部分就是dp[i-j*2][j],

意思就是和为去掉了首尾两个数后的和,最小数是j;第二部分就是dp[i][j+1].最小数大于j的情况的个数。状态转移方程:

dp[i][j] = dp[i-2*j][j] + dp[i][j+1]

初始化:

①.dp[0][j]初始值1.因为当需要调用dp[0][j]时,表示拆成了两个相同的数。有一个

②.dp[i][j](i<j< font="">)初始值0,不可能的情况

③.dp[i][j] (i>=j >i/2) 初始值1,j>i/2时所有s[i][j]都是1,那个就是i本身。

 //2016.8.22
#include<cstdio>
#include<cstring>
#define ll long long using namespace std; const int N = ;
ll dp[N][N]; int main()
{
int n;
memset(dp, , sizeof(dp));
for(int i = ; i < N; i++)
dp[][i] = ;
for(int i = ; i < N; i++)
for(int j = i/+; j <= i; j++)
dp[i][j] = ;
for(int i = ; i < N; i++)
for(int j = i/; j > ; j--)
dp[i][j] = dp[i-*j][j]+dp[i][j+];
while(scanf("%d", &n)!=EOF && n)
{
printf("%d %lld\n", n, dp[n][]);
} return ;
}

POJ1221(整数划分)的更多相关文章

  1. 51nod p1201 整数划分

    1201 整数划分 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 将N分为若干个不同整数的和,有多少种不同的划分方式,例如:n = 6,{6} {1,5} {2, ...

  2. 2014北大研究生推免机试(校内)-复杂的整数划分(DP进阶)

    这是一道典型的整数划分题目,适合正在研究动态规划的同学练练手,但是和上一个随笔一样,我是在Coursera中评测通过的,没有找到适合的OJ有这一道题(找到的ACMer拜托告诉一声~),这道题考察得较全 ...

  3. 整数划分 (区间DP)

    整数划分(四) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近 ...

  4. nyoj 90 整数划分

    点击打开链接 整数划分 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 将正整数n表示成一系列正整数之和:n=n1+n2+-+nk,  其中n1≥n2≥-≥nk≥1,k≥ ...

  5. 整数划分 Integer Partition(二)

    本文是整数划分的第二节,主要介绍整数划分的一些性质. 一 先来弥补一下上一篇文章的遗留问题:要求我们所取的 (n=m1+m2+...+mi )中  m1 m2 ... mi连续,比如5=1+4就不符合 ...

  6. 整数划分 Integer Partition(一)

    话说今天百度面试,可能是由于我表现的不太好,面试官显得有点不耐烦,说话的语气也很具有嘲讽的意思,搞得我有点不爽.Whatever,面试中有问到整数划分问题,回答这个问题过程中被面试官搞的不胜其烦,最后 ...

  7. 51nod1201 整数划分

    01背包显然超时.然后就是一道神dp了.dp[i][j]表示j个数组成i的方案数.O(nsqrt(n)) #include<cstdio> #include<cstring> ...

  8. NYOJ-571 整数划分(三)

    此题是个非常经典的题目,这个题目包含了整数划分(一)和整数划分(二)的所有情形,而且还增加了其它的情形,主要是用递归或者说是递推式来解,只要找到了递推式剩下的任务就是找边界条件了,我觉得边界也是非常重 ...

  9. BZOJ1263: [SCOI2006]整数划分

    1263: [SCOI2006]整数划分 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 677  Solved: 332[Submit][Status] ...

随机推荐

  1. 二分查找javascript

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Struts2的整体回顾(Action, 拦截器, 值栈, OGNL表示式, ModelDriven)

    ValueStack里有map(request, session, attr, parameters)和对象栈. Map调用的方法: ActionContext.getContext().put(k, ...

  3. andorid之摄像头驱动流程--MTK平台

    原文地址:andorid之摄像头驱动流程--MTK平台 作者:守候心田 camera成像原理: 景物通过镜头生产光学图像投射到sensor表面上,然后转为模拟电信号,经过数模变成数字图像信号,在经过D ...

  4. mrql初级教程-使用(er)

    最近使用mrql做xml文件解析,使用xpath来进行判断 使用的方法如下,其中t.mrql文件如下: v =args[1];store ty:=source(xml,args[0],{"p ...

  5. golang中container/heap包源码分析

    学习golang难免需要分析源码包中一些实现,下面就来说说container/heap包的源码 heap的实现使用到了小根堆,下面先对堆做个简单说明 1. 堆概念 堆是一种经过排序的完全二叉树,其中任 ...

  6. 控制流之break

    break语句是用来 终止 循环语句的,即哪怕循环条件没有称为False或序列还没有被完全递归,也停止执行循环语句.一个重要的注释是,如果你从for或while循环中 终止 ,任何对应的循环else块 ...

  7. mysql用存储过程插入百万条数据, 及查询优化

    查看所有存储过程: show procedure status; 查看详细存储过程 ptest: show create procedure ptest; 存储过程插入数据: create table ...

  8. 【转】聊聊HTTPS和SSL/TLS协议

    要说清楚 HTTPS 协议的实现原理,至少需要如下几个背景知识.1. 大致了解几个基本术语(HTTPS.SSL.TLS)的含义2. 大致了解 HTTP 和 TCP 的关系(尤其是“短连接”VS“长连接 ...

  9. HTML 学习笔记 JQuery(表单,表格 操作)

    表单应用 一个表单有3个基本组成部分 (1)表单标签:包含处理表单数据所用的服务器端程序URL 以及数据提交到服务器的方法 (2)表单域:包含文本框 密码框 隐藏框 多行文本框 复选框 单选框 下拉选 ...

  10. android UI线程安全问题

    在Android的子线程去更新UI的内容,会导致不确定的异常. 因为Android有个模式是,单一线程模型:Android UI工具箱(toolkit)不是一个线程安全的,并且它总是被放在主线程上操作 ...