Gym - 100952H--H. Special Palindrome--dp整数划分(模板)

题目地址
A sequence of positive and non-zero integers called palindromic if it can be read the same forward and backward, for example:

15 2 6 4 6 2 15

20 3 1 1 3 20

We have a special kind of palindromic sequences, let’s call it a special palindrome.

A palindromic sequence is a special palindrome if its values don’t decrease up to the middle value, and of course they don’t increase from the middle to the end.

The sequences above is NOT special, while the following sequences are:

1 2 3 3 7 8 7 3 3 2 1

2 10 2

1 4 13 13 4 1

Let’s define the function F(N), which represents the number of special sequences that the sum of their values is N.

For example F(7) = 5 which are : (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)

Your job is to write a program that compute the Value F(N) for given N’s.

Input
The Input consists of a sequence of lines, each line contains a positive none zero integer N less than or equal to 250. The last line contains 0 which indicates the end of the input.

Output
Print one line for each given number N, which it the value F(N).

Examples
Input
1
3
7
10
0
Output
1
2
5
17

题目大意:F(n) 就是 数字总数等于n,为回文字符串,前面的子串递增,后面的递减的字符串的数量(看一下题目应该能懂)。
思路:由于是回文字符串,所有我们只要c处理一边就行了,数字是对半的。如果字符串是长度是偶数,那n的数值必须是偶数,因为两边的总数要相等。我们先dp,预处理出整数划分,f[i][j]表示只从1~i中选,且总和等于j的方案数。然后,我们分字符长度为偶数和奇数处理就行了。

代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define re register
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(a) ((a)&-(a))
#define ios std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);
#define fi first
#define rep(i,n) for(int i=0;(i)<(n);i++)
#define rep1(i,n) for(int i=1;(i)<=(n);i++)
#define se secondusing namespace std;
typedef long long  ll;
typedef unsigned long long  ull;
typedef pair<int,int > pii;
const ll mod=10001;
const ll N =1e6+10;
const double eps = 1e-4;
const double pi=acos(-1);
ll gcd(int a,int b){return !b?a:gcd(b,a%b);}
int dx[4]={-1,0,1,0} , dy[4] = {0,1,0,-1};
ll f[351][351];
void solve()
{f[1][1] = 1;f[0][0] = 1;for (int i=2;i<=251;i++)for (int j=1;j<=i;j++)f[i][j]=(f[i-1][j-1]+f[i-j][j]);int n;while(cin>>n&&n){ll ans=0;if(n%2==0)//长度为偶数的时候,一边的总数必定是n/2;{for(int i=1;i<=n/2;i++) ans+=f[n/2][i];}for(int i=n;i>=1;i-=2)//奇数时,枚举中间那个数的值{for(int j=0;j<=i;j++) //因为中间的数是最大的,不得超过中间的数{//cout<<f[(n-i)/2][j]<<endl;ans+=f[(n-i)/2][j];}}cout<<ans<<endl;}
}
int main()
{iosint T;//cin>>T;T=1;while(T--){solve();}
}

整数划分模板
状态转移方程:
f[i][j] = f[i - 1][j] + f[i][j - i];

const int N = 1010;//一维int n;
int f[N];int main()
{cin >> n;f[0] = 1;for (int i = 1; i <= n; i ++ )for (int j = i; j <= n; j ++ )f[j] = (f[j] + f[j - i]) ;cout << f[n] << endl;return 0;
}
const int N = 1010;// 二维int n;
int f[N][N];int main()
{cin >> n;f[1][1] = 1;for (int i = 2; i <= n; i ++ )for (int j = 1; j <= i; j ++ )f[i][j] = (f[i - 1][j - 1] + f[i - j][j]) ;int res = 0;for (int i = 1; i <= n; i ++ ) res = (res + f[n][i]);cout << res << endl;return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/315422.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AtCoder Beginner Contest 194 F - Digits Paradise in Hexadecimal 数位dp

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 给一个161616进制的串NNN&#xff0c;让你求1−N1-N1−N中有多少个数有kkk个不同的数且没有前导零。 思路&#xff1a; NNN很大&#xff0c;有2e52e52e5了&#xff0c;那么就比较明显是个数位dpdpdp了。首先…

C#并发编程之异步编程(二)

写在前面前面一篇文章介绍了异步编程的基本内容&#xff0c;同时也简要说明了async和await的一些用法。本篇文章将对async和await这两个关键字进行深入探讨&#xff0c;研究其中的运行机制&#xff0c;实现编码效率与运行效率的提升。异步方法描述&#xff1a;使用async修饰符来…

2020牛客暑期多校训练营(第四场)H.Harder Gcd Problem(把1到n分为不互质的数对,找最多的对数)

题目大意&#xff1a;把1到n分为不互质的数对&#xff0c;找最多的对数 思路&#xff1a;先从最大的质因数开始找&#xff0c;因为小的比大的更容易匹配&#xff0c;所以贪心的从大的开始找。 首先要预处理出所以数的最大质因数。 然后根据质因数从大往小找&#xff0c;当质因…

Palindromic Numbers LightOJ - 1205 数位dp 求回文数

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 求[l,r][l,r][l,r]中有多少个回文数。 思路&#xff1a; 裸的数位dpdpdp啦&#xff0c;记dp[pos][pre][state]dp[pos][pre][state]dp[pos][pre][state]表示到了第pospospos位&#xff0c;回文是从第prepre…

C#规范整理·语言要素

如有不理解&#xff0c;请留言&#xff0c;开始!1. 正确操作字符串拼接字符串一定要考虑使用 StringBuilder ,默认长度为16,实际看情况设置。StringBuilder本质&#xff1a; 是以非托管方式分配内存。同时StringFormat方法 内部也是使用StringBuilder进行字符串格式化。2. 使用…

Educational Codeforces Round 94 (Rated for Div. 2) D(思维)

题目&#xff1a; You are given an array a1,a2…an. Calculate the number of tuples (i,j,k,l) such that: 1≤i<j<k<l≤n; aiak and ajal; Input The first line contains a single integer t (1≤t≤100) — the number of test cases. The first line of each…

2019 ICPC Asia Nanchang Regional K.Tree 树上启发式合并 + 动态开点线段树

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 给你一棵树&#xff0c;每个点都有一个权值valvalval&#xff0c;求满足以下条件 (1)x!yx!yx!y (2)xxx和yyy不互为祖先 (3)val[lca(x,y)]∗2val[x]val[y]val[lca(x,y)]*2val[x]val[y]val[lca(x,y)]∗2val[x]…

NetCore服务虚拟化01(集群组件Sodao.Core.Grpc)

一. 起始去年.NetCore2.0的发布&#xff0c;公司决定新项目采用.NetCore开发&#xff0c;当作试验。但是问题在于当前公司内部使用的RPC服务为Thrift v0.9 zookeeper版本&#xff0c;经过个性化定制&#xff0c;支持了异步&#xff0c;但也因为如此&#xff0c;这么多年来一直…

2019 ICPC Asia Nanchang Regional And and Pair 组合数学

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 给一个长度为nnn的二进制&#xff0c;求满足如下条件的j,ij,ij,i对数&#xff1a; (1)0<j<i<n(1)0<j<i<n(1)0<j<i<n (2)i&ni(2)i\And ni(2)i&ni (3)i&j0(3)i\And j0…

Oracle 发布基于 VS Code 的开发者工具,轻松连接 Oracle 数据库

在之前的文章中&#xff0c;我们提到了亚马逊、谷歌、IBM 等大厂都上了 Visual Studio Code 的船。今天&#xff08;北京时间 2019 年 6 月 20 日&#xff09;&#xff0c;甲骨文也上了 VS Code 的船&#xff0c;发布了基于 VS Code 的开发者工具&#xff0c;让开发者能轻松连接…

CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 树启 + 状压

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 思路&#xff1a; 据说是树启的压轴题。 先观察题意&#xff0c;字符有1−221-221−22中&#xff0c;为什么不是1−261-261−26个&#xff1f;显然他就是让你状压的。我们考虑将每条路径上字符状压成statest…

线段树——思维(Codeforces 339D Xenia and Bit Operations/Billboard HDU - 2795)

Codeforces 339D Xenia and Bit Operations vj地址 题意&#xff1a;给出2的n次方个数&#xff0c;每次将现在这个序列中相邻的两个数运算后合并为一个数&#xff0c;得到一个新的序列&#xff0c;这个新序列的长度是上一个序列长度-1&#xff0c;当新序列长度为1时停止运算&am…

误删50节点K8s集群为何3小时才能复原?Spotify揭自家事故幕后经验

误删50节点K8s集群为何3小时才能复原&#xff1f;Spotify揭自家事故幕后经验线上音乐串流服务Spotify一位基础架构工程师David Xia&#xff0c;在今年欧洲KubeCon大会上分享了自家Kubernetes集群一次意外事件。拥有上亿用户的Spotify&#xff0c;旗下开发者高达1千人&#xff0…

Codeforces Round #610 (Div. 2) D. Enchanted Artifact 交互 + 思维

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 思路&#xff1a; 首先我们发现如果知道了字符串的长度&#xff0c;我们就可以O(n1)O(n1)O(n1)次询问求解出来。比如当前长度为nnn&#xff0c;那么我们就可以构造出一个长度为nnn的全′a′a′a′字符串&…

P1005 矩阵取数游戏(__int128模板/简单dp)

转跳P1005 题目描述 帅帅经常跟同学玩一个矩阵取数游戏&#xff1a;对于一个给定的 n \times mnm 的矩阵&#xff0c;矩阵中的每个元素 a_{i,j}a i,j ​ 均为非负整数。游戏规则如下&#xff1a; 每次取数时须从每行各取走一个元素&#xff0c;共 nn 个。经过 mm 次后取完矩…

Docker+ Kubernetes已成为云计算的主流(二十六)

前言 最近正在抽时间编写k8s的相关教程&#xff0c;很是费时&#xff0c;等相关内容初步完成后&#xff0c;再和大家分享。对于k8s&#xff0c;还是上云更为简单、稳定并且节省成本&#xff0c;因此我们需要对主流云服务的容器服务进行了解&#xff0c;以便更好地…

P1020 导弹拦截(n*log n时间的最长上升子序列思想)

题目描述 某国为了防御敌国的导弹袭击&#xff0c;发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷&#xff1a;虽然它的第一发炮弹能够到达任意的高度&#xff0c;但是以后每一发炮弹都不能高于前一发的高度。某天&#xff0c;雷达捕捉到敌国的导弹来袭。由于该系统还…

P4137 Rmq Problem / mex 主席树求mex

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 思路&#xff1a; 按照值建线段树&#xff0c;每个位置维护值出现的最后位置&#xff0c;让后可持久化一下&#xff0c;当查询[l,r][l,r][l,r]的时候&#xff0c;我们只需要在[1,r][1,r][1,r]中找最后出现位…

《刷新》:拥抱同理心,建立成长型思维

“ 不刷新即死亡”2018年&#xff0c;很多朋友包括博客园里的很多园友都在阅读微软第三任CEO萨提亚纳德拉的这本《刷新》并且发布了很多读后感&#xff0c;但我却一直没有来得及阅读。刚好最近订阅了喜马拉雅的VIP会员&#xff0c;每天上下班时间开始了听书之旅&#xff0c;这里…

P1833 樱花——混合背包 二进制优化成01背包

P1833樱花 题目大意&#xff1a;有n颗樱花树&#xff0c;你的总时间为T&#xff0c;现在n课树&#xff0c;每次观看要花费w时间&#xff0c;能获取v点价值&#xff0c;最多能参观s次&#xff0c;如果s等于0&#xff0c;则可以观看无限次&#xff0c;问你在T时间内 获得的最大价…