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
  1. 3
    2 1 1
Output
  1. 2
Input
  1. 4
    1 1 1 1
Output
  1. 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)

代码示例:

  1. ll n;
  2. ll a[1005];
  3. ll c[1005][1005];
  4.  
  5. void init(){
  6. for(ll i = 1; i <= 1000; i++){
  7. c[i][0] = c[i][i] = 1;
  8. for(ll j = 1; j < i; j++){
  9. c[i][j] = (c[i-1][j]+c[i-1][j-1])%mod;
  10. }
  11. }
  12. }
  13. ll dp[1005];
  14.  
  15. int main() {
  16. //freopen("in.txt", "r", stdin);
  17. //freopen("out.txt", "w", stdout);
  18. init();
  19.  
  20. cin >> n;
  21. for(ll i = 1; i <= n; i++){
  22. scanf("%lld", &a[i]);
  23. }
  24. dp[n+1] = 1;
  25.  
  26. ll ans = 0;
  27. for(ll i = n-1; i >= 1; i--){
  28. ll p = i+a[i];
  29. if (a[i] <= 0) continue;
  30. for(ll j = p; j <= n; j++){
  31. dp[i] += (c[j-i][a[i]]*dp[j+1])%mod;
  32. dp[i] %= mod;
  33. }
  34. ans += dp[i];
  35. ans %= mod;
  36. }
  37. printf("%lld\n", ans);
  38.  
  39. return 0;
  40. }

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. java 利用反射创建对象

    创建对象: 1.使用Class对象的newInstance()方法创建该Class对象的实例,此时该Class对象必须要有无参数的构造方法. 2.使用Class对象获取指定的Constructor对象 ...

  2. H3C FTP双TCP连接方式

  3. centos7 安装R和RstudioServer版

    参考: http://www.cnblogs.com/inspursu/p/4275701.html http://blog.csdn.net/u010022051/article/details/5 ...

  4. yum安装错误:CRITICAL:yum.cli:Config Error: Error accessing file for config file:///home/linux/+

    上网搜了一下然后自己总结一份 : 出现原因:yum可能没有,或者损坏 解决: 第一步:下载   wget  http://yum.baseurl.org/download/3.2/yum-3.2.28 ...

  5. spring的几个面试题

    Spring 是一种轻量级开发框架,旨在提高开发人员的开发效率以及系统的可维护性.Spring 官网:https://spring.io/. 我们一般说 Spring 框架指的都是 Spring Fr ...

  6. k8s的持久化存储

    本例使用nfs 创建pv [root@k8s-master data]# vi pv.yaml apiVersion: v1kind: PersistentVolumemetadata: name: ...

  7. 纵我不往,知识不来--学习Java第一周心得

    暑假第一周,也是开始学习java的第一周. 本周的主要时间花在了小学期的任务上,但也草草开始了java的学习.首先安装好了所需要的软件,然后在网上下载了一份<Java基础笔记>,看了前五章 ...

  8. 一些实战中总结的 javascript 开发经验

    Javascript 的很多扩展的特性是的它变得更加的犀利, 同时也给予程序员机会创建更漂亮并且更让用户喜欢的网站. 尽管很多的开发人员都乐于颂扬 javascript,但是仍旧有人看到它的阴暗面. ...

  9. Linux 2>&1的意思

    2>&1的意思是将标准错误(2)也定向到标准输出(1)的输出文件中. 我们来具体了解下:Linux 中三种标准输入输出,分别是STDIN,STDOUT,STDERR,对应的数字是0,1, ...

  10. 它来了,它来了,centos 8 的时代到来了

    简介 Centos 8 已经在2019年9月24日正式发布.由于这是从Red Hat Enterprise Linux(RHEL)派生的Linux发行版,因此CentOS团队必须构建基础结构来支持新引 ...