dp - 求符合题意的序列的个数
The sequence of integers a1,a2,…,ak is called a good array if a1=k−1 and a1>0. For example, the sequences [3,−1,44,0],[1,−99] are good arrays, and the sequences [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]
, [1,2,3,−3,−9,4] are good, and the sequences [2,−3,0,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)
— the length of the initial sequence. The following line contains n integers 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
3
2 1 1
2
4
1 1 1 1
7
Note
In the first test case, two good subsequences — [a1,a2,a3]
and [a2,a3]
.
In the second test case, seven good subsequences — [a1,a2,a3,a4],[a1,a2],[a1,a3],[a1,a4],[a2,a3],[a2,a4]
and [a3,a4].
题意 : 给你一串数字,并按照题目叙述,给出一个好序列的定义,并且任意个好序列之间可以合并起来,问最终好序列的个数。
思路分析:
dp[i] 表示以i位置开始的序列的最优解,倒着推一下就可以了, dp[i] += dp[j-i][i]*dp[j+1] (i+a[i] <= j <= n)
代码示例:
ll n;
ll a[1005];
ll c[1005][1005]; void init(){
for(ll i = 1; i <= 1000; i++){
c[i][0] = c[i][i] = 1;
for(ll j = 1; j < i; j++){
c[i][j] = (c[i-1][j]+c[i-1][j-1])%mod;
}
}
}
ll dp[1005]; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
init(); cin >> n;
for(ll i = 1; i <= n; i++){
scanf("%lld", &a[i]);
}
dp[n+1] = 1; ll ans = 0;
for(ll i = n-1; i >= 1; i--){
ll p = i+a[i];
if (a[i] <= 0) continue;
for(ll j = p; j <= n; j++){
dp[i] += (c[j-i][a[i]]*dp[j+1])%mod;
dp[i] %= mod;
}
ans += dp[i];
ans %= mod;
}
printf("%lld\n", ans); return 0;
}
dp - 求符合题意的序列的个数的更多相关文章
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- dp求顺序hdu1160
题意是仅仅求一次的顺序.先依照速度从大到小排序,速度想等到按体重增序排列. 然后基本就变成了求已定顺序序列的最长递增序列递增,跟那个求一致最大序列和的基本一致. dp[i]里存储的是到当前i最大的递增 ...
- JDOJ 1946 求最长不下降子序列个数
Description 设有一个整数的序列:b1,b2,…,bn,对于下标i1<i2<…<im,若有bi1≤bi2≤…≤bim 则称存在一个长度为m的不下降序列. 现在有n个数,请你 ...
- HDU3853-LOOPS(概率DP求期望)
LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Total Su ...
- ACdream 1113 The Arrow (概率dp求期望)
E - The Arrow Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit ...
- hdu 3641 数论 二分求符合条件的最小值数学杂题
http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...
- Help Hanzo lightof 1197 求一段区间内素数个数,[l,r] 在 [1,1e9] 范围内。r-l<=1e5; 采用和平常筛素数的方法。平移区间即可。
/** 题目:Help Hanzo lightof 1197 链接:https://vjudge.net/contest/154246#problem/M 题意:求一段区间内素数个数,[l,r] 在 ...
- Poj 2096 (dp求期望 入门)
/ dp求期望的题. 题意:一个软件有s个子系统,会产生n种bug. 某人一天发现一个bug,这个bug属于某种bug,发生在某个子系统中. 求找到所有的n种bug,且每个子系统都找到bug,这样所要 ...
随机推荐
- BoundsChecker下载
首先,单独的BoundsChecker已经没了,被收购了,整合进了DevPartner 其次,DevPartner是收费软件,属于Borland的.官方地址:http://www.borland.co ...
- spring security自定义指南
序 本文主要研究一下几种自定义spring security的方式 主要方式 自定义UserDetailsService 自定义passwordEncoder 自定义filter 自定义Authent ...
- mysql导入文件出现Data truncated for column 'xxx' at row 1的原因
mysql导入文件的时候很容易出现"Data truncated for column 'xxx' at row x",其中字符串里的xxx和x是指具体的列和行数. 有时候,这是因 ...
- linux 手动睡眠
在 Linux 内核的之前的版本, 正式的睡眠要求程序员手动处理所有上面的步骤. 它是一 个繁琐的过程, 包含相当多的易出错的样板式的代码. 程序员如果愿意还是可能用那种方 式手动睡眠; <li ...
- 51nod 范德蒙矩阵
思路: 根据矩阵乘法的定义,G中的第i行第j列的元素 ai,j ,对答案的贡献为 ai,j∗ T中第j行的所有元素之和. 因此我们可以将T中根据每行的和进行排序.第i行的和可以通过公式 (ai^n−1 ...
- vue-cli3 使用 svg-sprite-loader 的坑
chainWebpack: config => { config.module.rules.delete("svg"); //重点:删除默认配置中处理svg, //const ...
- linux下mysql5.7忘记root密码修改
朋友最近开始学服务器,mysql密码忘了又不会弄,让我帮忙解决一下.重置或修改mysql的root密码这种事平时很少做,还是得google辅助一下,于是弄完了写篇博客记录一下,方便若干月后又有人遇到这 ...
- Educational Codeforces Round 61
Educational Codeforces Round 61 今早刚刚说我适合打pikmike出的EDU 然后我就挂了 A 不管 B 不管 C 这道题到快结束了才调出来 大概就是\(n^2\)枚举不 ...
- [板子]用线段树解决ST表问题
ST表可以参考:http://blog.csdn.net/whistlena/article/details/52191463 简单说就是区间RMQ最值问题. 对解决这种问题,线段树不用用啥啊. 扔一 ...
- windows系统锁屏及修改密码项目开发经验记录
改造windows开机.锁屏登录流程需要使用微软停供的Credential Providers工程,编译出来是dll,安装在C:\windows\system32目录下,然后注册注册表(运行工程生成的 ...