您的位置:首页 > 产品设计 > UI/UE

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

2018-06-28 15:17 766 查看
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
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][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].

题意:给定序列,问有多少子序列(不一定连续),满足可以划分为若干个组,给个组的第一个等于区间长度-1;

思路:因为关键在于区间的第一个,我们从后向前考虑,dp[i]表示以i为开头,满足题意的数量;sum[i]表示i后面可能的情况数量。

对于i:还要取a[i]个,我们假设最后一个数在j位置,那么dp[i]+=C(j-i-1,a[i]-1)*(1+sum[j+1]);

复杂度为O(N^2);

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int Mod=998244353;
const int maxn=1010;
int a[maxn],dp[maxn],sum[maxn];
int c[maxn][maxn],ans;
int main()
{
int N,i,j;
scanf("%d",&N);
for(i=0;i<=N;i++) c[i][0]=1,c[i][1]=i,c[i][i]=1;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
c[i][j]=(c[i-1][j]+c[i-1][j-1])%Mod;
for(i=1;i<=N;i++) scanf("%d",&a[i]);
for(i=N;i>=1;i--){
if(a[i]>0&&i+a[i]<=N){
for(j=i+a[i];j<=N;j++){
(dp[i]+=(ll)c[j-i-1][a[i]-1]*(1+sum[j+1])%Mod)%=Mod;
}
}
sum[i]=(sum[i+1]+dp[i])%Mod;
}
printf("%d\n",sum[1]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: