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. springmvc web.xml和application.xml配置详情(附:完整版pom.xml)

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...

  2. Array数组的常用方法

    1.concat()链接数组 var a = [1,2,3]; console.log(a.concat(4,5));//[1,2,3,4,5] 2.join()分隔符链接数组变字符串,如果省略该参数 ...

  3. 2018.11.16 浪在ACM 集训队第五次测试赛

    2018.11.16 浪在ACM 集训队第五次测试赛 整理人:李继朋 Problem A : 参考博客:[1]朱远迪 Problem B : 参考博客: Problem C : 参考博客:[1]马鸿儒 ...

  4. 【转载】VS Code 中的代码自动补全和自动导入包

    原文连接:https://maiyang.me/post/2018-09-14-tips-vscode/ VSCode 必须安装以下插件: 首先你必须安装 Golang 插件,然后再给 Go 安装工具 ...

  5. Libra和中国央行数字货币(DCEP)的对比

    最近偶然和朋友讨论起Libra,对Libra和央行的数字货币方案很感兴趣.梳理了阅读资料(参考见文末)和自己的思考,发知乎留个记录. Libra 是什么? 无国界货币 + 为全球数十亿人服务的金融基础 ...

  6. easypermissions拒绝权限后闪退。 java.lang.NoSuchMethodError: No virtual method isStateSaved()Z in class Landroid/support/v4/app/FragmentManager

    Process: com.tazan.cd.streetlight, PID: 18825 java.lang.NoSuchMethodError: No virtual method isState ...

  7. AbstactFactory模式

    AbstractFactory模式就是用来解决这类问题的:要创建一组相关或者相互依赖的对象. AbstractFactory模式关键就是将这一组对象的创建封装到一个用于创建对象的类(ConcreteF ...

  8. 使用Gson中的JsonElement所遇到的坑

    使用Gson中的JsonElement所遇到的坑 原文链接: https://blog.csdn.net/weixin_30326515/article/details/98196013 声明:此博客 ...

  9. C# 初识接口 Interface

    什么是接口? 接口(interface)用来定义一种程序的协定.实现接口的类或者结构要与接口的定义严格一致.有了这个协定,就可以抛开编程语言的限制(理论上).C#接口可以从多个基接口继承,而类或结构可 ...

  10. nodejs-websocket+ssl证书

    1.nodejs配置微信小程序本地服务器(二):利用ws模块创建基于ssl证书的WebSocket服务器:https://segmentfault.com/a/1190000013956534 2.n ...