D - Yet Another Problem On a Subsequence

CodeForces - 1000D

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

32 1 1

Output

2

Input

41 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].

题意:

定义个good array 是这个数组的长度为len时,a[1]=len-1

good sequence的本质就是多个good array相连,

现在给你一个含有n个数的数组,问你the number of subsequences of the original sequence that are good sequences,

思路:

定义dp[i] 表示从i到n,由i开头的good subsequence个数

这样dp[i]里每个情况都是由i开头的一个good array后面连good sequence。我们枚举good sequence可以接的位置是 j = i+a[i]+1 到 n,转移方程就是dp[i] = C(j-i-1,a[i] ) * d p [j ]

最后考虑如果一个good array后面不接sequence的情况,那就是c[ n-i ][ a[i] ]个情况,我们可以把j放宽到n+1,并把dp[n+1]设成1来解决这个问题。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ const ll mod = 998244353ll;
ll dp[maxn];
ll C[maxn][maxn];
ll a[maxn];
int n;
void init()
{
repd(i, 0, n) {
C[i][1] = i;
C[i][0] = 1ll;
C[i][i] = 1ll;
}
repd(i, 1, n) {
repd(j, 1, n) {
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
} }
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin >> n;
repd(i, 1, n) {
cin >> a[i];
}
init();
dp[n + 1] = 1ll;
for (int i = n; i >= 1; --i) {
int j = i + a[i] + 1;
if (a[i] <= 0 || j > n + 1) {
continue;
}
for (j; j <= n + 1; ++j) {
dp[i] = (dp[i] + C[j - i - 1][a[i]] * dp[j]) % mod;
}
}
for (int i = n - 1; i >= 1; --i) {
dp[i] = (dp[i] + dp[i + 1]) % mod;
}
cout << dp[1] << endl;
return 0;
} inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

最新文章

  1. Android开发学习——画横线竖线
  2. html5相关知识点的总结(有一些错误或者不足的地方)
  3. extjs学习之Ext.selection.CheckboxModel
  4. Nancy总结(一)Nancy一个轻量的MVC框架
  5. P1311 选择客栈
  6. js获取上一页、当前页及域名url方法,JS反回上一页的方法
  7. javascript组件开发
  8. Newtonsoft.Json同时对多个时间字段以不同的格式序列化
  9. python string 连接test
  10. art.dialog 与 ajax 异步请求
  11. 如何利用百度音乐播放器的API接口来获取高音质歌曲
  12. storm从入门到放弃(一),storm介绍
  13. 使用腾讯云“自定义监控”监控GPU使用率
  14. Windows Server 2012 R2 安装密钥(只适用安装,不支持激活)
  15. python 模拟豆瓣登录(豆瓣6.0)
  16. 2018-2019-2 20175311 实验一《Java开发环境的熟悉》实验报告
  17. spring cloud 下载依赖慢解决方案
  18. Windows----Github环境搭建
  19. Apache Common Math Stat
  20. WPF EventTrigger,BeginStoryboard

热门文章

  1. vue如何监听键盘事件中的按键?
  2. 【Abode Air程序开发】打包并导出
  3. 企业场景-网站目录安全权限深度讲解及umask知识
  4. [校内模拟赛T3]火花灿灿_二分答案_组合数学_贪心
  5. SQL Server 数据备份存储过程
  6. n*n矩阵 每行每列XOR为0(思维)
  7. 1-N(1的总数)找规律
  8. 超级实用的 Java 工具类
  9. 两个链表的第一个公共结点——牛客offer
  10. 修改hosts文件 解决coursera可以登录但无法播放视频的问题