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';
}
}
}

D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)的更多相关文章

  1. Yet Another Problem On a Subsequence CodeForces - 1000D (组合计数)

    大意:定义一个长为$k>1$且首项为$k-1$的区间为好区间. 定义一个能划分为若干个好区间的序列为好序列. 给定序列$a$, 求有多少个子序列为好序列. 刚开始一直没想出来怎么避免重复计数, ...

  2. Consecutive Subsequence CodeForces - 977F(dp)

    Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...

  3. Sonya and Problem Wihtout a Legend CodeForces - 714E (dp)

    大意: 给定序列, 每次操作可以任选一个数+1/-1, 求最少操作数使序列严格递增. 序列全-i后转化为求最少操作数使序列非降, 那么贪心可以知道最后$a_i$一定是修改为某一个$a_j$了, 暴力d ...

  4. New Year and Old Subsequence CodeForces - 750E (dp矩阵优化)

    大意: 给定字符串, 每次询问区间[l,r]有子序列2017, 无子序列2016所需要删除的最小字符数 转移用矩阵优化一下, 要注意$(\mathbb{Z},min,+)$的幺元主对角线全0, 其余全 ...

  5. D. Yet Another Problem On a Subsequence 解析(DP)

    Codeforce 1000 D. Yet Another Problem On a Subsequence 解析(DP) 今天我們來看看CF1000D 題目連結 題目 略,請直接看原題 前言 這題提 ...

  6. HDU 1159 Common Subsequence 公共子序列 DP 水题重温

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  7. hdu 1159(Common Subsequence)简单dp,求出最大的公共的字符数

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU 1159.Common Subsequence【动态规划DP】

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  9. Problem D: 勤奋的涟漪2 dp + 求导

    http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=3 dp[i][state]表示处理了前i个,然后当前状态是state的时候的 ...

随机推荐

  1. PHP非对称加密

    加密的类型: 在日常设计及开发中,为确保数据传输和数据存储的安全,可通过特定的算法,将数据明文加密成复杂的密文.目前主流加密手段大致可分为单向加密和双向加密. 单向加密:通过对数据进行摘要计算生成密文 ...

  2. AFNetworking网址中有中文崩溃的问题

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSe ...

  3. 【VS开发】C++ opencv Mat基础

    OpenCV2:Mat 1.Mat基础 在计算机内存中,数字图像是已矩阵的形式保存的.OpenCV2中,数据结构Mat是保存图像像素信息的矩阵,它主要包含两部分:矩阵头和一个指向像素数据的矩阵指针. ...

  4. win10下查看进程,杀死进程

    参考链接:https://blog.csdn.net/qq_36819098/article/details/80262482

  5. 第12课.经典问题解析(const;指针和引用)

    问题1:const什么时候为只读变量?什么时候是常量? const常量的判别准则: a.只有用字面量初始化的const常量才会进入符号表(直接初始化过的const为常量) b.被使用其他变量初始化的c ...

  6. python urllib2 实现大文件下载

    使用urllib2下载并分块copy: # from urllib2 import urlopen # Python 2 from urllib.request import urlopen # Py ...

  7. 博客C语言I作业11

    一.本周教学内容&目标 第5章 函数 要求学生掌握各种类型函数的定义.调用和申明,熟悉变量的作用域.生存周期和存储类型. 二.本周作业头 这个作业属于哪个课程 c语言程序设计II 这个作业要求 ...

  8. gdb移植(交叉版本)

    Gdb下载地址: http://ftp.gnu.org/gnu/gdb/ termcap下载地址:http://ftp.gnu.org/gnu/termcap/tar -zxvf termcap-1. ...

  9. kettle转换设置变量,校验输出新变量

    背景:有很多小的转换需要串联起来,如果前一个执行成功,后面继续接着执行,如果执行等待中,就让程序等一会再次获取数据分析,如果失败就中止,成功就进行下一个转换,以此类推.... 需求:通过job把参数传 ...

  10. 「java.util.concurrent并发包」之 CAS

    一  引言 在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度延时,引起性能 ...