总时间限制: 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.

样例输入


样例输出


提示

N < 250

解题思路

刚刚入门动规,果然踩坑了,这道题写了一个小时,调了一个小时,不过也算是独立完成的第一道动规题(虽然写着写着就写成递归了orz),纪念一下。

子问题划分见代码,每次只进行一次分解,比如将8分解为(4,4)或者(1,6,1)、(2,4,2)。

奇数情况进入下一次迭代,偶数情况则终止迭代,比如8中的(4,4)不用再次迭代,(2,2,2,2)的情况在(2,4,2)中会被考虑到。总而言之,分解之后中间有数就迭代,中间没数了就完成使命了。

因为要满足单峰条件,所以进入子问题之后要告知上一位数字以进行比较。

需要注意的问题和技巧

TLE:太多冗余的计算了,开一个二维upd数组记录结果,避免重复迭代和循环。

WA:N<250,还是超了int的范围,要用unsigned int。

AC代码

#include<iostream>
#include<cstring>
using namespace std; unsigned int upd[][];//保存结果,防止重复计算 int UPD(int s,int pre)//pre是当前位前一位的数字
{
if (upd[s][pre]) return upd[s][pre];
unsigned int cnt = ;
cnt++;//它自己也算是单峰
for (int j = ; j <= s / ; j++)
{
if (s - * j == && j >= pre) cnt++;
if ((s - * j) >= j && j >= pre) cnt += UPD(s - * j, j);
}
upd[s][pre] = cnt;
return cnt;
} int main()
{
int sum;
memset(upd, , sizeof(upd));
while (cin >> sum)
{
if (sum == )break;
unsigned int n_ = UPD(sum, );
cout << sum << " " << n_ << endl;
}
return ;
}

POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS的更多相关文章

  1. poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS (母函数)

    /* 给出一个数n,把它拆分成若干个数的和,要求最大的数在中间并向两边非递增.问拆法有多少种. 母函数.枚举中间的那一个数.由于左右对称.所以仅仅须要求左边部分的方案就可以. 注意,左右两部分的取数必 ...

  2. poj 3790 Recursively Palindromic Partitions

    /*摘抄自博客:Recursively Palindromic Partitions Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...

  3. poj 3790 Recursively Palindromic Partitions (递推)

    题目 题意:求输入的数字的递归回文. 思路:答案等于这个数字一半之前的所有的 之和. #include <iostream> #include <cstdio> #includ ...

  4. POJ 1221

    #include <iostream> #define MAXN 500 using namespace std; unsigned dp[MAXN][MAXN]; int main() ...

  5. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  6. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  7. ACM/ICPC 之 DP-整数划分问题初探 (POJ1221)

    写下这道题的原因很简单= =,因为这一题的状态转移方程不好找,另一方面,我看到很多针对这一题写的解题报告都把累加状态说得模棱两可,甚至直接说成了一个单一状态,弄得本是菜鸟的我硬生生折磨了一上午画了几个 ...

  8. POJ1221(整数划分)

    UNIMODAL PALINDROMIC DECOMPOSITIONS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 543 ...

  9. POJ 2823 UESTCoj 1221 Sliding Window 单调队列 经典入门题

    题意:给出一个序列,求出每连续k个数字中最大的数和最小的数. 这是道单调队列裸题,直接写就行了. 本来用deque写出来后,发现在poj上硬是超时了,在discuss上看很多人也在抱怨超时的问题,据说 ...

随机推荐

  1. pageContext 和 config 内置对象

    forword("目标页面")  : 使当前页面跳转到另一个目标页面 include("目标页面") ;使当前页面包含另一个页面的信息

  2. 转发: JS中的call()和apply()方法和区别 --小白变色记

    一.方法定义: apply:调用一个对象的一个方法,用另一个对象替换当前对象.例如:B.apply(A, arguments);即A对象应用B对象的方法. call:调用一个对象的一个方法,用另一个对 ...

  3. Oracle EXPDP导出数据

    Oracle expdp导出表数据(带条件): expdp student/123456@orcl dumpfile=student_1.dmp logfile=student_1.log table ...

  4. C# 基础回顾: volatile 关键字

    有些人可能从来没看到过这个关键字,这也难怪,因为这个关键字并不常用.那这个关键字到底有什么用呢? 我在网上搜索这个关键字的时候,发现很多朋友都有一个错误的认识 ------ 认为这个关键字可以防止并发 ...

  5. 使用docker来创建一个etcd集群

    docker run -d --name etcd1 --network etcdnet --ip 172.25.0.101 -p 23791:2379 -e ETCDCTL_API=3 -v /ro ...

  6. linux patch 简单学习

    使用patch 我们可以方便的进行软件补丁包处理,以下演示一个简单的c 项目补丁处理 原代码 app.c #include <stdio.h> int main(){ printf(&qu ...

  7. PHP生成随机数;订单号唯一

    //8-12位随机数 function makeRand($num=){ $strand = (; if(strlen($strand)<$num){ $strand = str_pad($st ...

  8. 【JZOJ6225】【20190618】计数

    题目 对于一个01串,定义\(f(s)\)为\(f(s) = \sum_{i=0}^{\lfloor \frac{|s|}{2} \rfloor -1 }[s_i=s_{|s|-1-i}]\) 定义\ ...

  9. nginx配置url重定向&反代

    一.重定向 正则表达式匹配: * ~ 为区分大小写匹配 * ~* 为不区分大小写匹配 * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配: * -f和!-f用来判断是否存在文件 ...

  10. 每日Android一问等你来解答-什么是Activity的生命周期?

    关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 什么是Activity的生命周期? 生命周期: 对于生命周期我们 ...