「 LuoguT37042」 求子序列个数

Description


给定序列 A, 求出 A 中本质不同的子序列 (包含空的子序列) 个数模 10^9+ 7 的结果.

一个序列 B 是 A 的子序列需要满足 A 删掉某些元素后能够得到 B.

两个子序列中对应位置的数都相同我们就认为这两个子序列本质相同

Input


第一行包含一个整数 N , 代表序列的长度.

接下来一行 N 个整数, 第 i 个数代表 Ai.

Output


输出一个整数代表答案.

Sample Input


5
2 3 1 3 2

Sample Output


27

Hint


对于 20% 的数据, N<=10.

对于 40% 的数据, N<=20

对于 70% 的数据, N<=100000; 1<=Ai<=100

题解


很奇怪的思路。

看代码应该挺好懂的。

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 #define R register
 5 #define ULL unsigned long long
 6 ULL a[1000007];
 7 ULL f[1000007];
 8 ULL s[1000007];
 9 ULL las[1000007];
10 int main()
11 {
12     ULL n;
13     cin>>n;
14     for(R int i=1;i<=n;++i)
15     cin>>a[i];
16     f[0]=1;
17     s[0]=1;
18     f[1]=1;
19     s[1]=2;
20     las[a[1]]=1;
21     ULL mod=1e9+7;
22     for(R int i=2;i<=n;++i)
23     {
24         f[i]=(s[i-1]-s[las[a[i]]]+f[las[a[i]]]+mod)%mod;
25         las[a[i]]=i;
26         s[i]=(s[i-1]+f[i])%mod;
27     }
28     cout<<s[n];
29     return 0;
30 }
原文地址:https://www.cnblogs.com/qwerta/p/9379739.html