POJ 1121-UNIMODAL PALINDROMIC DECOMPOSITIONS 动态规划

POJ动态规划题目全面总结 PKU Online Judge上面很全面的动态规划试题总结。动态规划是ACM考点中最重要的一大类算法之一,对于工作人员来说,动态规划也是实际开发中经常会遇到的算法。这是POJ上面很多DP题目的总结与深刻分析。利于算法学习,学长给的,在此分享 立即下载

题目来源:http://poj.org/problem?id=1221

解题报告:

这道题求的是给定一个数字n,求n可以拆成多少种unimodal palindromic decompositions。也就是首先满足回文串的性质,然后前半部分是递增顺序的。

我的解法是:

取f[n][k],代表对n,取回文串的第一个数>=k时的palindromic decompositions的种数。

那么,对f[n][k],先计算f'[n][k]代表回文串的第一个数为k是的Palindromic decompositions的种数。

那么有f'[n][k] = f[n-2*k][k],其中1<=k <=n/2

于是f[n][k] = f'[n][k] + f'[n][k+1] + ... + f[n][n]

最后要求的就是f[n][1]


#include <iostream>
using namespace std;

#define N 300

long long *f[N];  //注意精度,用long long, 因为这个WA了好几次。

void compute(int n);

long long get(int n, int k) {
	if (f[n][k]!=0)
		return f[n][k];
	else
		compute(n);
	return f[n][k];
}

void compute(int n) {
	if (f[n][n] != 0) {
		return;
	}
	for (int j = 1; j<=n/2;j++) {
		if (n-2*j >= j) {
			f[n][j] += get(n-2*j,j);
		}
	}
	if ((n/2)*2 == n)
		f[n][n/2] += 1;
	f[n][n]=1;
	for (int j=n-1; j>=1;j--) {
		f[n][j] += f[n][j+1];
	}
}



int main()
{
	for (int i =0 ; i< N; i++) {
		f[i] = new long long[i+1];
		for (int j=0;j<=i;j++) {
			f[i][j] = 0;
		}
	}

	int n;
	cin >> n;
	f[1][1]=1;
	while(n!=0)
	{
		compute(n);
		cout << n << " " << f[n][1] << endl;
		cin >> n;
	}
}

附录:

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

python 常见算法(1-50题) 常见的算法题 阅读详情

相关推荐

大模型面试题52:LLM如果在训练过程中loss值出现spike应该怎么办?

小白急救:停更→回滚→降LR→裁剪梯度,先保住训练不崩溃;进阶排查:查数据→查优化器→查硬件,找到spike的根因并解决;深层预防:预处理保守配置+训练中实时监控,从源头规避spike。先止损,再溯源,最后预防。

叫好与叫座虽然不是对立面,但想在同一个作品中达到双重效果很难。 1180

动态规划之UNIMODAL PALINDROMIC DECOMPOSITIONS

UNIMODAL PALINDROMIC DECOMPOSITIONS Description A sequence ofpositive integers is Palindromic if it reads the same forward and backward. Forexample:  23 11 15 1 37 37 1 15 11 23  1 1 2 3 4 7 7 10

hqf1992的专栏 733

【coze实战】基于Coze的公众号图文自动化生成工作流实战解析

【coze实战】基于Coze的公众号图文自动化生成工作流实战解析

大家好,我是码农老gou,一名专注于编程技术的程序员。在这个博客中,我将与大家分享我在工作和学习中的经验、见解以及一些实用的技术干货。 1万+

ZOJ 1353 Unimodal Palindromic Decompositions(简单计数dp)

Unimodal Palindromic Decompositions Time Limit: 2 Seconds      Memory Limit: 65536 KB A sequence of positive integers is Palindromic if it reads the same forward and backward. For example: 23 11 15...

满月照亮的路 356

大模型学习笔记四:LangChain开发框架解析

1、模型I/O封装LLMs:大语言模型Chat Models:一般基于LLMs,但按对话结构重新封装PromptTemple:提示词模板OutputParser:解析输出2、数据连接封装Document Loaders:各种格式文件的加载器Document transformers:对文档的常用操作,如:split,filter,translate,extract metadata,etcTextr Embedding Models:文本向量化表示,用于检索等操作。

谢白羽 4293

【大模型】一图看懂3D因果卷积!

本文将系统性地介绍从1D到3D的因果卷积原理,通过直观的动图演示帮助读者理解其工作机制,并重点探讨3D因果卷积在大规模视频生成中的独特价值和应用前景。

xian0710830114的专栏 2208

POJ 1221

UNIMODAL PALINDROMIC DECOMPOSITIONS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5903 Accepted: 2896 Description A sequence of positive integers is Palindromic if it reads the same for...

fengzhizi76506的博客 287

Lumerical 教程,包含作者个人理解及相关设计案例 (目前共计184篇,持续更新中)

4138。

u011699626的博客 1668

POJ1221 UNIMODAL PALINDROMIC DECOMPOSITIONS

                                                  UNIMODAL PALINDROMIC DECOMPOSITIONSTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2893 Accepted: 1335Desc

nextcy的专栏 616

Python爬虫教程:如何爬取多平台电商商品信息(价格、评价、库存)

本文介绍了如何编写一个跨平台电商商品爬虫,爬取多个平台的商品信息,包括商品名称、价格、评价等数据。通过Python的爬虫库(如requestsSelenium)和数据处理库(如pandasmatplotlib)的结合,我们可以高效地获取和分析电商平台上的商品信息。

2201_76125261的博客 922

Python爬虫实战:新浪财经股票金融数据全方位抓取教程

本教程系统介绍了新浪财经股票数据爬取技术,从静态网页解析、实时接口请求到异步爬取和数据存储,帮助你搭建了一个完整的金融数据采集流程。未来,可以结合深度学习模型进行行情预测,或者将数据接入量化交易系统,实现自动化投资。

2201_76125261的博客 2210

ACM 动态规划 POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS 题解

题目:   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 U...

untilyouydc 487

HTML粉色爱心(完整代码)

HTML语言实现粉色爱心的完整代码。

AIGC 1125

POJ1121 UNIMODAL PALINDROMIC DECOMPOSITIONS

本题大意是,对于某一个数,有多少回文的组合方式。第一反应就是使用DP,状态的定义:d(i,k)表示数i中,组合数大于k的个数。因此d(i,k)=∑d(i-2j,j),j=n/2的情况,使d(i,j)=1。此题特别需要注意的是要使用int64的数据类型,对应的输出要使用"%lld",否则会溢出。 #include #include typedef long long INT64; I

flyRP的专栏 757

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

POJ1221(整数划分)

UNIMODAL PALINDROMIC DECOMPOSITIONS Time Limit:1000MS Memory Limit:65536K Total Submissions:5430 Accepted:2641 Description A sequence of positive integers is Palind...

weixin_30530523的博客 181

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

acm pku 1221 Unimodal palindromic decompositions的逻辑推理过程

如果能够弄清楚这道题目的逻辑过程,则是一道简单的递推题目。

uestcshe的专栏 860

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

poj1221简单的动态规划

UNIMODAL PALINDROMIC DECOMPOSITIONS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4503   Accepted: 2211 Description A sequence of positive integers is Pali

he_fa的专栏 750

单峰分布(unimodal distribution)、双峰分布 (bimodal distribution)以及偏态分布(skewness distribution)

单峰分布(unimodal distribution)& 双峰分布 (bimodal distribution) 数据分布会有一个或者很多个峰值(peaks),数据分布中只有一个明显峰值的叫做单峰,有两个明显峰值的叫做双峰,同样,具有多个峰值的就是多峰。 偏态分布(skewness distribution) 在数据分布的图像展现形式中,偏态分布中一边的观测值要比另一边的观测值多,右...

敲代码的quant的博客 7万+

poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS 动态规划

UNIMODAL PALINDROMIC DECOMPOSITIONSTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2580 Accepted: 1200DescriptionA sequence of positive integers

yc0576的专栏 700

poj1221 UNIMODAL PALINDROMIC DECOMPOSITIONS

题意很简单,给你一个正整数数n,将n拆分成一个正整数数字序列,要求数字序列和为n且是个回文并且左半部分的序列非降。问n拆分成这样的序列有几种方案?设d(n,k)表示n左右两边是k的时候的方案数,那么递推式: 表示当前这一种方案(n,k)加上方案(n-2i,i)的方案数,注意一下边界。本题其实就是一个递推式,由于用到了记忆化所以就写得跟dp一样了。。。。#include<cstdio> #inc

Daniel 653

unimodal_palindromic——回文串dp动规

#include <iostream> using namespace std; const int N = 250; long long divide[N][N]; // 数字比较大,别人用的int——64?? void computer () // 计算i的所有分解中,最大的分解数小于j的分解方法数 { for (int i = 0; i < N; i++) ...

CSer 401

[母函数]POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS

传送门:UNIMODAL PALINDROMIC DECOMPOSITIONS UNIMODAL PALINDROMIC DECOMPOSITIONS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4741   Accepted: 2330 Descrip

jjaw2013 942

OpenJudge(1221)——UNIMODAL PALINDROMIC DECOMPOSITIONS

题意: 现在给你一个叫Unimodal Palindromic序列的东西,它符合是回文串并且它是峰型的,峰型意味着它就是先递增再递减。 思路: 一开始没思路,后来尝试着找思路,但是打表打出来之后发现有一些问题。因为没有限制条件,于是会发生有重复的现象。于是设dp[i][j]表示当和为i时,而且这个序列中的最小的那个数是大于等于j的时候所能够组成的情况数有几个 于是dp[i][j]=dp[i

ACM_hades的梦想之路 780

POJ Unimodal Palindromic Decompositions(DP)

223:UNIMODAL PALINDROMIC DECOMPOSITIONS 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 A sequence of positive integers is Palindromic if it reads the same forward and backward. For example: 23 11 15 1

hyqsblog的专栏 602

从单模态到多模态:AI原生应用的交互革命

当我们用Siri问天气时(语音单模态),用微信发文字消息时(文本单模态),或是用美图秀秀修图时(图像单模态),这些“单一感官输入-单一输出”的交互方式已伴随我们多年。但随着AI大模型技术的突破,一场“交互革命”正在发生:AI开始能同时理解文字、图像、语音、视频甚至触觉信息,并生成多模态响应——这就是“多模态交互”。本文将聚焦这场革命的技术本质、应用场景与未来趋势。

AI架构师小马 1384

poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS(母函数)

【题目大意】:给出一个数n,把它拆分成若干个数的和,要求最大的数在中间并向两边非递增。问拆法有多少种。 【解题思路】:母函数。枚举中间的那一个数,因为左右对称,所以只需要求左边部分的方案即可。注意,左右两部分的取数必须小于中间的数,中间的数是0的话则以n为最大取值。 【代码】: #include #include #include #include #includ

Wu is so Water 848
上一篇: 一道链表相关的笔试题
下一篇: POJ 1033-Defragment 简单搜索
kindlucy
博客等级 码龄19年 64粉丝 95原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值