FZU 2129 子序列个数 (递推dp)

时间:2023-03-09 04:16:03
FZU 2129 子序列个数 (递推dp)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2129

dp[i]表示前i个数的子序列个数

当a[i]在i以前出现过,dp[i] = dp[i - 1]*2 - dp[pre - 1],pre表示a[i]在i之前的位置

当a[i]在i以前没有出现过,dp[i] = dp[i - 1] *2 + 1

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e6 + ;
LL dp[N], mod = 1e9 + , pre[N]; int main()
{
int n, num;
while(~scanf("%d", &n)) {
memset(pre, , sizeof(pre));
for(int i = ; i <= n; ++i) {
scanf("%d", &num);
if(pre[num]) {
dp[i] = ((dp[i - ]* - dp[pre[num] - ]) % mod + mod) % mod;
} else {
dp[i] = (dp[i - ]* + ) % mod;
}
pre[num] = i;
}
printf("%lld\n", dp[n]);
}
return ;
}