CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数)
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.
Input
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.
Output
In the single line output one integer — the number of subsequences of the original sequence that are good sequences, taken modulo 998244353.
Examples
3
2 1 1
2
4
1 1 1 1
7
Note
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].
题意:给定序列,问有多少子序列(不一定连续),满足可以划分为若干个组,给个组的第一个等于区间长度-1;
思路:因为关键在于区间的第一个,我们从后向前考虑,dp[i]表示以i为开头,满足题意的数量;sum[i]表示i后面可能的情况数量。
对于i:还要取a[i]个,我们假设最后一个数在j位置,那么dp[i]+=C(j-i-1,a[i]-1)*(1+sum[j+1]);
复杂度为O(N^2);
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int Mod=;
const int maxn=;
int a[maxn],dp[maxn],sum[maxn];
int c[maxn][maxn],ans;
int main()
{
int N,i,j;
scanf("%d",&N);
for(i=;i<=N;i++) c[i][]=,c[i][]=i,c[i][i]=;
for(i=;i<=N;i++)
for(j=;j<=N;j++)
c[i][j]=(c[i-][j]+c[i-][j-])%Mod;
for(i=;i<=N;i++) scanf("%d",&a[i]);
for(i=N;i>=;i--){
if(a[i]>&&i+a[i]<=N){
for(j=i+a[i];j<=N;j++){
(dp[i]+=(ll)c[j-i-][a[i]-]*(+sum[j+])%Mod)%=Mod;
}
}
sum[i]=(sum[i+]+dp[i])%Mod;
}
printf("%d\n",sum[]);
return ;
}
CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数)的更多相关文章
- CodeForces - 1000D Yet Another Problem On a Subsequence
题面在这里! 好智障的一个dp啊,一段开头的数字相当于下面要跟多少个数,直接滚动数组dp就行了... #include<bits/stdc++.h> #define ll long lon ...
- Codeforces 713C Sonya and Problem Wihtout a Legend DP
C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- cf1000D Yet Another Problem On a Subsequence (dp)
设f[i]是以i为开头的好子序列的个数 那么有$f[i]=\sum\limits_{j=i+a[i]+1}^{N+1}{f[j]*C_{j-i-1}^{a[i]}}$(设f[N+1]=1)就是以i为开 ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (DP)
题意:给你一个长度为\(n\)的序列,求一个最长的\({x,x+1,x+2,.....,x+k-1}\)的序列,输出它的长度以及每个数在原序列的位置. 题解:因为这题有个限定条件,最长序列是公差为\( ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- 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 163A Substring and Subsequence dp
A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...
- Educational Codeforces Round 9 D. Longest Subsequence dp
D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...
- 【codeforces 750E】New Year and Old Subsequence
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- uboot之bootm以及go命令的实现
本文档简单介绍了uboot中用于引导内核的命令bootm的实现,同时分析了uImage文件的格式,还简单看了一下uboot下go命令的实现 作者: 彭东林 邮箱: pengdonglin137@163 ...
- kfaka windows安装
1 官网下载 解压到D:\developTools\kfaka\kafka_2.10-0.9.0.0 2 windows cmd启动 新开cmd命令:cd /d D:\developTools\kfa ...
- Android中怎样控制LogCat的自己定义输出
在Android开发中,LogCat是一个非常重要的调试工具,能够输出非常多关于项目或者手机的信息. 可是正是因为LogCat功能的过于强大,输出的信息量也是极为庞大的,那么我们就须要通过一定的方式依 ...
- EasyPlayerPro windows播放器之多窗口播放音量控制方法
EasyPlayerPro-win基础版本的音频播放为单一通道播放,即同一时间仅允许一个通道播放声音,现应客户需求,在基础版本上实现独立的音频播放,即每个通道可同时播放视频和音频; 设计思路 将音频播 ...
- with(nolock) 与 with(readpast) 与不加此2个的区别
调试窗口一: 或者查询窗口一: 总之:事务没有结束 查询窗口二:
- AWS:5.公有云编程
主要内容 1.AWS接口 2.使用AWS命令行 3.使用python sdk编程 AWS接口 Console:web控制台 登录amazon后在"我的账户" -> AWS管理 ...
- 流畅的python学习笔记第八章:深拷贝,浅拷贝,可变参数
首先来看赋值,浅拷贝,深拷贝. 一赋值: a=['word',2,3] b=a print id(a),id(b) print [id(x) for x in a] print [id(x) for ...
- 用c++后缀自动机实现最大公共字符串算法,并封装成Python库
后缀自动机的C++代码转自https://e-maxx.ru/algo/suffix_automata,其余封装为自写. 在C++文件同级目录建立setup.py文件,代码如下: # !/usr/bi ...
- linux复制和移动
复制: -f 强制覆盖同名文件 -r 按递归方式保留原目录结构复制文件 cp -Rf /home/user1/* /root/temp/ 将/home/user1目录下的所有东西拷到/root ...
- vuex原理笔记
本文总结自: https://tech.meituan.com/vuex-code-analysis.html, 将要点提炼为笔记,以便不时之需,安不忘危. 核心可分为两部分: 1.vue.use(V ...