Codeforces 1000D Yet Another Problem On a Subsequence 动态规划
2 seconds
256 megabytes
standard input
standard output
The sequence of integers a1,a2,…,aka1,a2,…,ak is called a good array if a1=k−1a1=k−1 and a1>0a1>0. For example, the sequences [3,−1,44,0],[1,−99][3,−1,44,0],[1,−99] are good arrays, and the sequences [3,7,8],[2,5,4,1],[0][3,7,8],[2,5,4,1],[0] — are not.
A sequence of integers is called good if it can be divided into a positive number of good arrays. Each good array should be a subsegment of sequence and each element of the sequence should belong to exactly one array. For example, the sequences [2,−3,0,1,4][2,−3,0,1,4], [1,2,3,−3,−9,4][1,2,3,−3,−9,4] are good, and the sequences [2,−3,0,1][2,−3,0,1], [1,2,3,−3−9,4,1][1,2,3,−3−9,4,1] — are not.
For a given sequence of numbers, count the number of its subsequences that are good sequences, and print the number of such subsequences modulo 998244353.
The first line contains the number n (1≤n≤103)n (1≤n≤103) — the length of the initial sequence. The following line contains nn integers a1,a2,…,an (−109≤ai≤109)a1,a2,…,an (−109≤ai≤109) — the sequence itself.
In the single line output one integer — the number of subsequences of the original sequence that are good sequences, taken modulo 998244353.
3 2 1 1
2
4 1 1 1 1
7
In the first test case, two good subsequences — [a1,a2,a3][a1,a2,a3] and [a2,a3][a2,a3].
In the second test case, seven good subsequences — [a1,a2,a3,a4],[a1,a2],[a1,a3],[a1,a4],[a2,a3],[a2,a4][a1,a2,a3,a4],[a1,a2],[a1,a3],[a1,a4],[a2,a3],[a2,a4] and [a3,a4][a3,a4]
今天是我做出来一道动态规划题大喜的日子,这个题开了一个一维数组f[i],表示从1开始到i结束的序列里有几个符合条件的序列。f[i]分两部分,一部分是f[i-1],另一部分是包含a[i]的序列。包含a[i]的序列又分两部分,一个good array,或者一个good array和前面的序列组合成的序列。
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;long long n;
long long tr[1005][1005],a[1005];
long long f[1010];const long long mod=998244353;int main()
{scanf("%lld",&n);tr[0][0]=1; tr[1][0]=1; tr[1][1]=1;for (int i=2; i<=n; i++){tr[i][0]=1;for (int j=1; j<n; j++){tr[i][j]=(tr[i-1][j-1]+tr[i-1][j])%mod;}tr[i][i]=1;}for (int i=1; i<=n; i++){scanf("%lld",&a[i]);}memset(f,0,sizeof(f));for (int i=2; i<=n; i++){f[i]=(f[i]+f[i-1])%mod;for (int j=i-1; j>=1; j--){int st=a[j]-1;if (st<0 || st>i-j-1) continue;f[i]=(f[i]+tr[i-j-1][st])%mod;f[i]=(f[i]+tr[i-j-1][st]*f[j-1])%mod;}}cout<<f[n]<<endl;return 0;
}
Codeforces 1000D Yet Another Problem On a Subsequence 动态规划相关推荐
- CodeForces - 1000D Yet Another Problem On a Subsequence(动态规划+组合数学)
题目链接:点击查看 题目大意:给出n个数字组成的序列,现在规定"好数组"指的是一个连续序列a1,a2,...ak的a1=k-1,再规定"好序列"是可以分为若干个 ...
- Codeforces 1000D Yet Another Problem On a Subsequence 【dp】【组合数学】
难点在于怎么想dp,我一开始想dp[i][j]代表前i个数挑j个能组成多少个good sebsequence,最后把dp[n][ 2到n ]加起来就行,但想不出来转移方程怎么做.后来想到我这么想是不对 ...
- Codeforces 1000D Yet Another Problem On a Subsequence
题目:点击打开链接 题意:定义一个数列是"好的":第一个数字a[0]为数列长度+1.定义一个数列的子序列是"好的":这个子序列能分割成几个"好的&qu ...
- CodeForces - 1000D Yet Another Problem On a Subsequence
题面在这里! 好智障的一个dp啊,一段开头的数字相当于下面要跟多少个数,直接滚动数组dp就行了... #include<bits/stdc++.h> #define ll long lon ...
- D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)
D - Yet Another Problem On a Subsequence CodeForces - 1000D The sequence of integers a1,a2,-,aka1,a2 ...
- CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数)
CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数) 题目大意:这题目啊,贼难理解- 定义一个数列是"好的&quo ...
- CodeForces - 1000D D. Yet Another Problem On a Subsequence 好题
D. Yet Another Problem On a Subsequence time limit per test 2 seconds memory limit per test 256 mega ...
- Codeforces 776D The Door Problem
题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...
- codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)
题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...
最新文章
- Annotations
- 什么是断点,为什么要设置断点?断点的作用是什么?
- wince6.0 编译报错:error C2220: warning treated as error - no 'object' file generated的解决办法...
- 区块链学习之区块链思想的诞生(一)
- php工厂模式和单例模式,php 设计模式之工厂模式、单例模式、注册树模式
- 卓越领导者的智慧(精华版)
- flutter版本的玩Android客户端
- BZOJ 4011 HNOI2015 落忆枫音
- 微信小程序与公众号推送消息
- PMBOK(第六版) 学习笔记 ——《第五章 项目范围管理》
- 【FFmpeg】在FFmpeg里添加自研编码器方法
- 佛祖释迦牟尼说的最经典的一句话
- Windows10与Ubuntu双系统安装记录
- 中公教育python教师_为什么中公教育、华图的老师不自己去考公务员?
- 微信jssdk图片上传给服务器,使用微信JSSDK进行图片选择和上传
- cat3速度 rj45_技术词语:4G网CAT.4和CAT.3那个快?
- 德国风力发电机发电数据集(13w多条数据)
- linux内核配置nfs,【参赛手记】开启Digilent提供的Linux内核的NFS支持
- 天地图卫星地图在OpenLayers中的应用示例源码
- Oracle数据库CPU占用过高