子序列的个数

给定一个正整数序列,序列中元素的个数和元素值大小都不超过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
请选取你熟悉的语言,并在下面的代码框中完成你的程序,注意数据范围,最终结果会造成Int32溢出,这样会输出错误的答案。
不同语言如何处理输入输出,请查看下面的语言说明。
【分析】
dp[i] = dp[i – 1] * 2  如果a[i]不在之前出现
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 子序列的个数(动态规划)的更多相关文章

  1. 51nod 子序列的个数 (动规分析方法)

    这道题的分析方法我很需要学习学习. 一开始我想的是f[i][j]表示前i个数子序列长度为j的个数 然后发现新加入一个数的时候会和前面的重复,这个时候不知道该怎么处理这种重复. 其实我再继续往下想就可以 ...

  2. FZU 2129 子序列个数 (动态规划)

    题意:子序列的定义:对于一个序列a=a[1],a[2],......a[n].则非空序列a'=a[p1],a[p2]......a[pm]为a的一个子序列,其中1<=p1<p2<.. ...

  3. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

    Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...

  4. 动态规划精讲(一)LC 最长递增子序列的个数

    最长递增子序列的个数 给定一个未排序的整数数组,找到最长递增子序列的个数. 示例 1: 输入: [1,3,5,4,7]输出: 2解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, ...

  5. [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  6. [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  7. leetcode 940. 不同的子序列 II (动态规划 ,字符串, hash,好题)

    题目链接 https://leetcode-cn.com/problems/distinct-subsequences-ii/ 题意: 给定一个字符串,判断里面不相同的子串的总个数 思路: 非常巧妙的 ...

  8. rqnoj-217-拦截导弹-最长不上升子序列以及不上升子序列的个数

    最长上升子序列的O(n*log(n))算法. 不上升子序列的个数等于最长上升子序列的长度. #include<string.h> #include<stdio.h> #incl ...

  9. 【最长下降子序列】【动态规划】【二分】XMU 1041 Sequence

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1041 题目大意: 一个二维平面,上面n(n<=1 000 000)个点.问至少选 ...

随机推荐

  1. 解决echarts中X轴文字过长的问题。【转】

    axisLabel: { interval: , formatter:function(value) { debugger var ret = "";//拼接加\n返回的类目项 ; ...

  2. [USACO08DEC] 秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

  3. codeforces 1077F1

    题目:https://codeforces.com/contest/1077/problem/F1 题意: 你有n幅画,第i幅画的好看程度为ai,再给你两个数字k,x 表示你要从中选出刚好x幅画,并且 ...

  4. spring4.3注解

     Spring4.3中引进了 {@GetMapping.@PostMapping.@PutMapping.@DeleteMapping.@PatchMapping},分别对应这个查询,插入,更新,删除 ...

  5. shell监控进程是否存在

    1.直接给代码:我这个是两个程序.多半要写成函数 [root@java1 src]# cat checkprocess.sh #!/bin/bashcheckprocess(){ps -ef|grep ...

  6. nginx对指定目录做代理

    环境介绍 web1,作为前端端服务器,访问地址是http://192.168.1.1,要将http://192.168.1.1/bbs的请求交给web2.在web1的网站根目录下并没有bbs目录 we ...

  7. Document base D:\devTools\apache-tomcat-6.0.51\webapps\AppService does not exist or is not a readable directory

    tomcat通过eclipse发布项目到webapp后  手动删除在webapp目录下的文件,启动tomcat时,会报出异常找不到那个删除的项目. 解决方法是(1)重新发布项目到webapp (2)在 ...

  8. matlab求矩阵、向量的模

    求矩阵的模: function count = juZhenDeMo(a,b) [r,c] = size(a);%求a的行列 [r1,c1] = size(b);%求b的行列 count = 0; f ...

  9. 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法

    2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 第二次世界大战时期,英国皇家空军从沦陷国 ...

  10. POJ2154 Color

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10322   Accepted: 3360 Description Bead ...