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

The sequence of integers a1,a2,,aka1,a2,…,ak is called a good array if a1=k1a1=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.c++

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,39,4,1][1,2,3,−3−9,4,1] — are not.ide

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.spa

Inputcode

The first line contains the number n (1n103)n (1≤n≤103) — the length of the initial sequence. The following line contains nn integers a1,a2,,an (109ai109)a1,a2,…,an (−109≤ai≤109) — the sequence itself.blog

Outputelement

In the single line output one integer — the number of subsequences of the original sequence that are good sequences, taken modulo 998244353.input

Examplesit

Input
3
2 1 1
Output
2
Input
4
1 1 1 1
Output
7

Noteclass

In the first test case, two good subsequences — [a1,a2,a3][a1,a2,a3] and [a2,a3][a2,a3].test

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; }
相關文章
相關標籤/搜索