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

转载于:https://www.cnblogs.com/qieqiemin/p/11512085.html

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. Codeforces 1000D dp

    注意对题意的理解,对于给出的序列,我们需要找出他满足要求的子序列,然后对于每个子序列,再把它分成几个满足要求的子串 . 对于任意一个元素i,我们需要至少往后面走a[i]位,然后从i+a[i]+1开始直 ...

  3. CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数)

    CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数) 题目大意:这题目啊,贼难理解- 定义一个数列是"好的&quo ...

  4. Codeforces 1000D Yet Another Problem On a Subsequence 动态规划

    D. Yet Another Problem On a Subsequence time limit per test 2 seconds memory limit per test 256 mega ...

  5. CodeForces - 1000D D. Yet Another Problem On a Subsequence 好题

    D. Yet Another Problem On a Subsequence time limit per test 2 seconds memory limit per test 256 mega ...

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

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

  7. Codeforces Round #555 (Div. 3), problem: (C2) Increasing Subsequence (hard version)【贪心+撞到南墙也不回头】

    题目链接 题目大意 复杂版大意是我们可以从左右两端每次拿走一个数,一直拿,不过要满足一个条件,每次拿的数要保证严格递增(即从小到大然后不会有相同的情况) 复杂版的话是会有相同的数字出现 在题解中正式说 ...

  8. Codeforces 1000D Yet Another Problem On a Subsequence 【dp】【组合数学】

    难点在于怎么想dp,我一开始想dp[i][j]代表前i个数挑j个能组成多少个good sebsequence,最后把dp[n][ 2到n ]加起来就行,但想不出来转移方程怎么做.后来想到我这么想是不对 ...

  9. CodeForces - 1000D Yet Another Problem On a Subsequence(动态规划+组合数学)

    题目链接:点击查看 题目大意:给出n个数字组成的序列,现在规定"好数组"指的是一个连续序列a1,a2,...ak的a1=k-1,再规定"好序列"是可以分为若干个 ...

最新文章

  1. opencv 二值化 python_Python OpenCV 图像二值化-阈值分割
  2. TiDB Ecosystem Tools 原理解读系列(二)TiDB-Lightning Toolset 介绍
  3. python3.5和pip3安装路径不匹配问题
  4. 如何听节拍器_我是如何开垮一家琴行的!
  5. mac中rabbitmq的安装
  6. 【英语学习】【WOTD】horticulture 释义/词源/示例
  7. hibernate 的第一个工程
  8. sql语句中case_SQL中的CASE语句
  9. 错乱的 Windows 10
  10. Git版本管理工具Tower for Mac
  11. 信捷XC系列PLC-编程
  12. 一种基于频域滤波法消除干扰项与角谱法重构技术的数字全息显微台阶形貌测量实例分析
  13. 人生有三重境界:看山是山,看水是水;看山不是山,看水不是水;看山还是山,看水还是水(转载)
  14. 语音识别(ASR)论文优选:A comparison of streaming models and data augmentation methods for robust speech recog
  15. Android(小米miui)如何判断当前应用是否允许NFC权限
  16. eap-peap/mschapv2
  17. 视频裁剪的软件哪个好用?推荐几个视频裁剪软件给你
  18. 在pcb放置坐标标注_PCB设计定位基准符号和尺寸
  19. 传智播客网络营销课程大升级,改变从“薪”开始
  20. 与Zeynep Tufekci讨论社交媒体驱动的抗议的未来

热门文章

  1. ABAP string函数一览
  2. 能量平衡_能量平衡原则
  3. java getscale_Java MajorType.getScale方法代碼示例
  4. express bodyparser_nodejs库express是如何接收inbound json请求的
  5. crontab 每分钟一次_Celery实现定时任务crontab
  6. UE4学习-第三人称游戏的AI巡逻
  7. log4j2.xml 文件
  8. java 管理员命令模式_java中命令模式详解和使用方法
  9. java 无法继承抽象类_java抽象类可以被继承吗?
  10. GB2312 UTF8 UCS2汉字编码对应表