总时间限制: 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. SQL Server 父子迭代查询语句,树状查询

    这个也有用: -- Get childs by parent idWITH TreeAS( SELECT Id,ParentId FROM dbo.Node P WHERE P.Id = 21 -- ...

  2. shell脚本sed的基本用法

    sed 我们首先准备了一个测试文件 1. s 替换  将文件中的This替换cyy 在替换的时候如果加入了 -i 选项就会真的替换,但是只会替换每一行的第一个 -n 和 -p 一起使用表示的是打印那些 ...

  3. telegraf 学习二 几个概念

    telegraf 自身包好了自己处理metrics 的数据模型,以及出炉方法 metrics Telegraf指标是用于在处理期间对数据建模的内部表示.这些指标完全基于InfluxDB的数据模型,包含 ...

  4. prisma 已经支持mongodb了

    好久没有关注prisma 的版本迭代了,记得在去年12月份左右的时候,mongodb 在github 上还只是一个草案, 官方文档也没有相关的详细介绍,今天留意了下,居然已经支持了,还是很给力的(my ...

  5. LOJ6609 无意识的石子堆【加强版】【容斥原理,计数】

    题目描述:在一个\(n\times m\)的网格中,放\(2n\)个棋子,使每一行和每一列都不超过两个棋子.求方案数\(\mathrm{mod} \ 943718401\). 数据范围:\(n\le ...

  6. 在Matlab中的tick可以调整方向

    需要将axis对话框的More property打开,修改TickDir,可从In改成Out.

  7. [USACO5.1]二维凸包模板

    传送门 Description 求\(n\)个点凸包的周长 Solution  计算几何打暴力必备 Code  #include<bits/stdc++.h> #define reg re ...

  8. vue使用axios发送请求,都会发送两次请求

    vue 使用axios,每次的请求都会发送两次,第一次的请求头为options CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sha ...

  9. PHP 之循环创建文件夹

    /** * 循环创建文件夹 * @param string $dir 需要创建的文件夹路径 * @param integer $mode 文件夹权限 * @return bool 返回创建是否成功 * ...

  10. Koa 操作 Mongodb 数据库

    node-mongodb-native的介绍 使用基于官方的 node-mongodb-native 驱动,封装一个更小.更快.更灵活的 DB 模块, 让我们用 nodejs 操作 Mongodb 数 ...