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

Input
3
2 1 1
Output
2
Input
4
1 1 1 1
Output
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+组合数)的更多相关文章

  1. CodeForces - 1000D Yet Another Problem On a Subsequence

    题面在这里! 好智障的一个dp啊,一段开头的数字相当于下面要跟多少个数,直接滚动数组dp就行了... #include<bits/stdc++.h> #define ll long lon ...

  2. 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 ...

  3. 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为开 ...

  4. Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (DP)

    题意:给你一个长度为\(n\)的序列,求一个最长的\({x,x+1,x+2,.....,x+k-1}\)的序列,输出它的长度以及每个数在原序列的位置. 题解:因为这题有个限定条件,最长序列是公差为\( ...

  5. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  6. 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 ...

  7. CodeForces 163A Substring and Subsequence dp

    A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...

  8. Educational Codeforces Round 9 D. Longest Subsequence dp

    D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...

  9. 【codeforces 750E】New Year and Old Subsequence

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  10. 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. apt-mirror 校验错误文件处理

    apt-mirror是一个用来将Debian或Ubuntu的软件源镜像到本地的工具,这个工具工作得非常好,不过有的时候由于网络问题,会有一些文件的校验是失败的,但apt-mirror并不能发现,等到最 ...

  2. 小程序WePY入门(一)

    全局安装或更新WePY命令行工具 npm install wepy-cli -g 在开发目录中生成Demo开发项目 wepy new myproject 切换至项目目录 cd myproject 开启 ...

  3. poj 3071 Football <DP>

    链接:http://poj.org/problem?id=3071 题意: 有 2^n 支足球队,编号 1~2^n,现在给出每支球队打败其他球队的概率,问哪只球队取得冠军的概率最大? 思路: 设dp[ ...

  4. 绿色版Tomcat的配置

    在环境变量中不配置JAVA_HOME或者JRE_HONE的情况下(正确配置java的路径)不影响java的使用 可以正常使用 java -version .... 但是这种情况下 无法在Tomcat的 ...

  5. Java反射机制简单学习

    java中除了基本数据类型,几乎都为对象.例如 Person p=new Person(); 这句语句表明了p是Person类的一个实例对象.但其实,Person也是一个实例对象,它是Class类的实 ...

  6. Flask的Debug功能非常酷

    Flask是一个Python开发框架.在试用的过程中发现它的debug功能非常cool.如下图所示,在出错的页面每条栈新的右边都有一个按钮,点击之后我们可以执行Python代码,而且非常重要的一点是通 ...

  7. ZookeeperclientAPI之创建会话(六)

    Zookeeper对外提供了一套Java的clientAPI. 本篇博客主要讲一下创建会话. 创建项目 首选,创建一个基于maven管理的简单javaproject.在pom文件里引入zookeepe ...

  8. 推荐20个非常有帮助的web前端开发教程

    1. CSS Vocabulary 一个伟大的指向和点击的小应用程序,让你加高速度掌握 CSS 语法的各个不同部分,学习各个属性的正确的名称. 2. Liquidapsive 一个简单的信息化布局.通 ...

  9. Eclipse使用方法和技巧二十六:浅谈快捷键

            网络上到处都是eclipse有哪些经常使用的快捷键,当中还有非常多讲得着实不错.这里就不再狗尾续貂而是谈谈别的这段时间的一些思考.近期增加了开发团队.代码量突突的上去了.同一时候也发现 ...

  10. 使用 Spring 容器管理 Filter

    当我们用Filter时,往往需要使用一些辅助的service,在普通的java中,只要声明(set,get方法)后在spring-application配置文件中配置就可以了,但是由于Filter与L ...