The sequence of integers a1,a2,…,ak is called a good array if a1=k−1 and a1>0. For example, the sequences [3,−1,44,0],[1,−99] are good arrays, and the sequences [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]

, [1,2,3,−3,−9,4] are good, and the sequences [2,−3,0,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)

— the length of the initial sequence. The following line contains n integers 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]

and [a2,a3]

.

In the second test case, seven good subsequences — [a1,a2,a3,a4],[a1,a2],[a1,a3],[a1,a4],[a2,a3],[a2,a4]

and [a3,a4].

题意 : 给你一串数字,并按照题目叙述,给出一个好序列的定义,并且任意个好序列之间可以合并起来,问最终好序列的个数。

思路分析:

  dp[i] 表示以i位置开始的序列的最优解,倒着推一下就可以了, dp[i] += dp[j-i][i]*dp[j+1] (i+a[i] <= j <= n)

代码示例:

ll n;
ll a[1005];
ll c[1005][1005]; void init(){
for(ll i = 1; i <= 1000; i++){
c[i][0] = c[i][i] = 1;
for(ll j = 1; j < i; j++){
c[i][j] = (c[i-1][j]+c[i-1][j-1])%mod;
}
}
}
ll dp[1005]; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
init(); cin >> n;
for(ll i = 1; i <= n; i++){
scanf("%lld", &a[i]);
}
dp[n+1] = 1; ll ans = 0;
for(ll i = n-1; i >= 1; i--){
ll p = i+a[i];
if (a[i] <= 0) continue;
for(ll j = p; j <= n; j++){
dp[i] += (c[j-i][a[i]]*dp[j+1])%mod;
dp[i] %= mod;
}
ans += dp[i];
ans %= mod;
}
printf("%lld\n", ans); return 0;
}

dp - 求符合题意的序列的个数的更多相关文章

  1. [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  2. [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  3. dp求顺序hdu1160

    题意是仅仅求一次的顺序.先依照速度从大到小排序,速度想等到按体重增序排列. 然后基本就变成了求已定顺序序列的最长递增序列递增,跟那个求一致最大序列和的基本一致. dp[i]里存储的是到当前i最大的递增 ...

  4. JDOJ 1946 求最长不下降子序列个数

    Description 设有一个整数的序列:b1,b2,…,bn,对于下标i1<i2<…<im,若有bi1≤bi2≤…≤bim 则称存在一个长度为m的不下降序列. 现在有n个数,请你 ...

  5. HDU3853-LOOPS(概率DP求期望)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  6. ACdream 1113 The Arrow (概率dp求期望)

    E - The Arrow Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit  ...

  7. hdu 3641 数论 二分求符合条件的最小值数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...

  8. Help Hanzo lightof 1197 求一段区间内素数个数,[l,r] 在 [1,1e9] 范围内。r-l<=1e5; 采用和平常筛素数的方法。平移区间即可。

    /** 题目:Help Hanzo lightof 1197 链接:https://vjudge.net/contest/154246#problem/M 题意:求一段区间内素数个数,[l,r] 在 ...

  9. Poj 2096 (dp求期望 入门)

    / dp求期望的题. 题意:一个软件有s个子系统,会产生n种bug. 某人一天发现一个bug,这个bug属于某种bug,发生在某个子系统中. 求找到所有的n种bug,且每个子系统都找到bug,这样所要 ...

随机推荐

  1. css3图片展示方式

    <view class='img_block' id='mjltest'> <view class='text_view'> <view class='{{cell_cl ...

  2. 4-4 Selector有一个方法可以获取Selector中的文本内容---extract()

    ####### 例如: response.xpath('//div[@class ="entry-header"]/h1/text()').extract()

  3. 报错:org.springframework.beans.factory.BeanCreationException

    报错码为以下内容,把自己走的坑贴出来,免得大家如同样的坑.以下解决方法仅供参考. ERROR [RMI TCP Connection(3)-127.0.0.1] - Context initializ ...

  4. jekyll 添加 Valine 评论

    本文告诉大家如何在自己搭建的静态博客添加 Valine 评论.在这前,我基本都是使用 多说,但是多说gg啦,所以就在找一个可以替换的评论 本来 Disqus是很好的,但是在国内很难打开,所以我就需要一 ...

  5. win10 uwp 使用 Azure DevOps 自动构建

    通过 Azure DevOps 可以做到自动构建程序,覆盖计划.创建.编程.测试.部署.发布.托管.共享等各个环节,适用于大多数的语言.平台. 本文继续使用图床为例告诉大家如何使用 Azure Dev ...

  6. HDU - 6333 Problem B. Harvest of Apples (莫队)

    There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm a ...

  7. PDF.JS 读取文件流前端展示 C#

    最近再搞PDF得展示问题,因为aspose.pdf成本太高,只能使用pdf.js这个开源强大的前端东东了. 在百度了很久后 网上大都是node,java,php的事例,有位大哥的是C#的后台代码按他写 ...

  8. Qt中动态链接库的使用

    转自: http://www.qtcn.org/bbs/read.php?tid=14719 现在有些软件有自动升级功能,有些就是下载新的DLL文件,替换原来的动态链接库.MFC好象也有类似机制 Qt ...

  9. [wireshark] ip filter

    查ip 时,使用 ip==10.224.37.18 发现无效 使用 ip.dst, 查到了 Match destination: ip.dst == x.x.x.x Match source: ip. ...

  10. 深入谷歌和甲骨文旷日持久的版权战争,趣味科普当前最火的编程语言JAVA的前世今生

    这篇文章是我在B站上投稿的一个科普java的视频文案,内容如标题,感兴趣的码农朋友可以移步观看https://www.bilibili.com/video/av81171108/ 在过去短短几十年间, ...