51nod 子序列的个数(动态规划)
给定一个正整数序列,序列中元素的个数和元素值大小都不超过105, 求其所有子序列的个数。注意相同的只算一次:例如 {1,2,1}有子序列{1} {2} {1,2} {2,1}和{1,2,1}。最后结果对10^9 + 7取余数。
第1行:一个数N,表示序列的长度(1 <= N <= 100000)
第2 - N + 1行:序列中的元素(1 <= a[i] <= 100000)
输出a的不同子序列的数量Mod 10^9 + 7。
4
1
2
3
2
13
dp[i] = dp[i – 1] * 2 – dp[j – 1],如果a[i]最近在j的位置出现过。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define mod 1000000007
typedef long long ll;
using namespace std;
ll dp[],have[],a[];
int n,cnt,m,h;
int main()
{
cin>>n;
for(int i=;i<=n;i++)cin>>a[i];
dp[]=;
memset(have,,sizeof(have));
for(int i=;i<=n;i++)
{
if(have[a[i]]==){dp[i]=(dp[i-]*)%mod;}
else {dp[i]=(dp[i-]*-dp[have[a[i]]-]+mod)%mod;}
have[a[i]]=i; }
cout<<(dp[n]-)%mod<<endl;
return ;
}
51nod 子序列的个数(动态规划)的更多相关文章
- 51nod 子序列的个数 (动规分析方法)
这道题的分析方法我很需要学习学习. 一开始我想的是f[i][j]表示前i个数子序列长度为j的个数 然后发现新加入一个数的时候会和前面的重复,这个时候不知道该怎么处理这种重复. 其实我再继续往下想就可以 ...
- FZU 2129 子序列个数 (动态规划)
题意:子序列的定义:对于一个序列a=a[1],a[2],......a[n].则非空序列a'=a[p1],a[p2]......a[pm]为a的一个子序列,其中1<=p1<p2<.. ...
- Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划
Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...
- 动态规划精讲(一)LC 最长递增子序列的个数
最长递增子序列的个数 给定一个未排序的整数数组,找到最长递增子序列的个数. 示例 1: 输入: [1,3,5,4,7]输出: 2解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- leetcode 940. 不同的子序列 II (动态规划 ,字符串, hash,好题)
题目链接 https://leetcode-cn.com/problems/distinct-subsequences-ii/ 题意: 给定一个字符串,判断里面不相同的子串的总个数 思路: 非常巧妙的 ...
- rqnoj-217-拦截导弹-最长不上升子序列以及不上升子序列的个数
最长上升子序列的O(n*log(n))算法. 不上升子序列的个数等于最长上升子序列的长度. #include<string.h> #include<stdio.h> #incl ...
- 【最长下降子序列】【动态规划】【二分】XMU 1041 Sequence
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1041 题目大意: 一个二维平面,上面n(n<=1 000 000)个点.问至少选 ...
随机推荐
- 【题解】APIO2007动物园
首先一眼感受到这题特别的性质……5个?这么小的,感觉就像是状压.脑补了一下,如果没有环的话应该很好做吧……有环怎么办?5真的很小的,随便乱搞肯定也可以.那就放在外面暴力枚举吧.然后正解就出来了. 然而 ...
- [CF409F]000001
题目大意:输入一个数,输出一个数(愚人节系列) 题解:$OEIS$的$A000001$(原来我不想写的,但是洛谷的智能推荐推荐我做这个...是不是我太菜了) 卡点:无 C++ Code: #inclu ...
- linux kernel 关于RSS/RPS/RFS/XPS的介绍
Introduction============ This document describes a set of complementary techniques in the Linuxnetwo ...
- 工具——代码中自动生成SVN版本号
本节和大家讨论一下程序集版本最后一位使用SVN版本号的自动生成方法,这里就向大家简单介绍一下.在进行自动部署的时候,经常需要用脚本获取程序的最新版本号.现在我们定义每个程序集的版本信息的最末段表示SV ...
- Intellij Idea debug 远程部署的的tomcat项目
web项目部署到tomcat上之后,有时需要打断点单步调试,如果用的是Intellij idea,可以通过如下方法实现: 开启debug端口,启动tomcat 以tomcat7.0.75为例,打开bi ...
- UVA 11995 STL 使用
There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...
- 停止ambari上服务的顺序
Before performing any upgrades or uninstalling software, stop all of the Hadoop services in the foll ...
- 使用HTML实现对汉字拼音的支持
<!DOCTYPE HTML><html> <head> <meta charset="utf-8"> <title>无 ...
- Hibernate中inverse、cascade的说明
一: 前沿:刚刚学习hibernate时,对于inverse很是纠结,不知道什么时候该用什么时候不该用,在网上找了一些资料,说的也很含糊,我都不知道如果写了"inverse=true&quo ...
- 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法
2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 第二次世界大战时期,英国皇家空军从沦陷国 ...