D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)
D - Yet Another Problem On a Subsequence
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,组合数学)的更多相关文章
- Yet Another Problem On a Subsequence CodeForces - 1000D (组合计数)
大意:定义一个长为$k>1$且首项为$k-1$的区间为好区间. 定义一个能划分为若干个好区间的序列为好序列. 给定序列$a$, 求有多少个子序列为好序列. 刚开始一直没想出来怎么避免重复计数, ...
- Consecutive Subsequence CodeForces - 977F(dp)
Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...
- Sonya and Problem Wihtout a Legend CodeForces - 714E (dp)
大意: 给定序列, 每次操作可以任选一个数+1/-1, 求最少操作数使序列严格递增. 序列全-i后转化为求最少操作数使序列非降, 那么贪心可以知道最后$a_i$一定是修改为某一个$a_j$了, 暴力d ...
- New Year and Old Subsequence CodeForces - 750E (dp矩阵优化)
大意: 给定字符串, 每次询问区间[l,r]有子序列2017, 无子序列2016所需要删除的最小字符数 转移用矩阵优化一下, 要注意$(\mathbb{Z},min,+)$的幺元主对角线全0, 其余全 ...
- D. Yet Another Problem On a Subsequence 解析(DP)
Codeforce 1000 D. Yet Another Problem On a Subsequence 解析(DP) 今天我們來看看CF1000D 題目連結 題目 略,請直接看原題 前言 這題提 ...
- HDU 1159 Common Subsequence 公共子序列 DP 水题重温
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- hdu 1159(Common Subsequence)简单dp,求出最大的公共的字符数
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159.Common Subsequence【动态规划DP】
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...
- Problem D: 勤奋的涟漪2 dp + 求导
http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=3 dp[i][state]表示处理了前i个,然后当前状态是state的时候的 ...
随机推荐
- eNSP路由器输出 '#' 无法启动的一种解决方法
试过网上的改virtulboxIP.关防火墙.改兼容性等方法,都不成功.后来发现通过改变选择路由器的型号可以启动. 如果对路由器没有特殊需求,可以选择型号数字较小的试一下(最低可以选择Router).
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
- 【并行计算-CUDA开发】CUDA ---- Warp解析
Warp 逻辑上,所有thread是并行的,但是,从硬件的角度来说,实际上并不是所有的thread能够在同一时刻执行,接下来我们将解释有关warp的一些本质. Warps and Thread Blo ...
- 【VS开发】程序员对内存的理解
程序员对内存的理解 在C和C++语言开发中,指针.内存一直是学习的重点.因为C语言作为一种偏底层的中低级语言,提供了大量的内存直接操作的方法,这一方面使程序的灵活度最大化,同时也为bug埋下很多隐患. ...
- table中td内容过长自动换行
table { table-layout:fixed; WORD-BREAK:break-all;}
- 结束占用端口号进程(pid)
- Infix to Prefix conversion using two stacks
Infix : An expression is called the Infix expression if the operator appears in between the operands ...
- spring-boot 使用servlet2.5(四)
环境 jdk 6 tomcat 6.0.53 sts 4.4.2 maven 3.2.5 背景 由于环境限制,还在使用 servlet 2.5,所以需要将 spring boot 进行配置,支持 se ...
- Spring中 如果该Service有多个实现类,它怎么知道该注入哪个ServiceImpl类?
方法一:Controller中注入service的时候使用@Autowired自动注入,@Qualifier("beanId") 来指定注入哪一个. 方法二:Controller中 ...
- 方格取数--状压DP or 网络流
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1565 取不相邻的点是权值最大. 这题可以网络流做,暂时先DP一下,网络流明天学一下~~ #define I ...