Forehead! I am going to learn dp, I will update it from time to time
Really much to learn
Post today's first question
number of subsequences
Definition of subsequence: For a sequence a=a[1],a[2],......a[n]. Then the non-empty sequence a'=a[p1],a[p2]......a[pm] is a subsequence of a, where 1<=p1<p2<.....<pm<= n.
For example 4,14,2,3 and 14,1,2,3 are both subsequences of 4,13,14,1,2,3.
For a given sequence a, output the number of distinct subsequences. (Since the answer is relatively large, please mod the answer to 1000000007)
The input contains multiple sets of data. The first line of each set of data is an integer n (1<=n<=1,000,000), which represents the number of sequence elements.
The second line contains n integers a[i] (0<=a[i]<=1,000,000) representing each element in the sequence.
4 1 2 3 2
13
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #define mod 1000000007 /** In fact, it's a matter of finding rules Let f(n) be the substring of the string whose main string is n greatest amount, Available if a[n] appears in the string f(n)=f(n-1)*2-f(flag[a[n]]) else f(n)=f(n-1)*2+1 **/ long long math[1000005]; long long dp[1000005]; long long flag[1000005]; int main() { int n; while(~scanf("%d",&n)) { memset(flag,0,sizeof(flag)); memset(dp,0,sizeof(dp)); memset(math,0,sizeof(math)); for(int i=1; i<=n; i++) scanf("%lld",&math[i]); dp[0]=0; dp[1]=1; for(int i=1; i<=n; i++) { if(flag[math[i]]!=0) { dp[i]=(dp[i-1]*2-dp[flag[math[i]]-1]+mod)%mod; } else if(flag[math[i]]==0) { dp[i]=(dp[i-1]*2+1)%mod; } flag[math[i]]=i; } printf("%lld\n",dp[n]); } return 0; }