tokitsukaze
>
Codeforces edu#46 D.Yet Another Problem On a Subsequence (dp)
转载请注明出处:http://tokitsukaze.live/
题目链接:http://codeforces.com/problemset/problem/1000/D
题意:给一个序列。定义good子序列:满足可以划分为若干个组,每个组的第一个数=这个组的长度-1。求有多少个good子序列。
题解:
dp[i]表示以i开头的good子序列的个数。直接倒着转移即可。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") #define mem(a,b) memset((a),(b),sizeof(a)) #define MP make_pair #define pb push_back #define fi first #define se second #define sz(x) (int)x.size() #define all(x) x.begin(),x.end() using namespace std; #define _GLIBCXX_PERMIT_BACKWARD_HASH #include <ext/hash_map> using namespace __gnu_cxx; struct str_hash{size_t operator()(const string& str)const{return __stl_hash_string(str.c_str());}}; typedef long long ll; typedef unsigned long long ull; #define PII pair<int,int> #define PLL pair<ll,ll> #define PDD pair<double,double> #define VI vector<int> #define VL vector<ll> const int INF=0x3f3f3f3f; const ll LLINF=0x3f3f3f3f3f3f3f3f; const double PI=acos(-1.0); const double eps=1e-4; const int MAX=5e5+10; const ll mod=998244353; ll C[1010][1010]; void init(int n) { int i,j; for(i=(C[0][0]=1);i<=n;i++) { for(j=(C[i][0]=1);j<=n;j++) { C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod; } } } int main() { int n,i,j,a[MAX]; ll dp[1010]; init(1000); while(~scanf("%d",&n)) { for(i=1;i<=n;i++) scanf("%d",&a[i]); mem(dp,0); dp[n+1]=1; for(i=n;i;i--) { if(a[i]<=0) continue; for(j=n;j-a[i]>=i;j--) { (dp[i]+=dp[j+1]*C[j-i][a[i]]%mod)%=mod; } } for(i=2;i<=n;i++) (dp[1]+=dp[i])%=mod; printf("%lld\n",dp[1]); } return 0; }
|