problem 2129 Number of sub-sequencesaccept:147 submit:432
Time limit:2000 mSec Memory limit:32768 KB problem Description
Definition of subsequence: For a sequence a=a[1],a[2],...... a[n]. The non-empty sequence a ' =a[p1],a[p2]......a[pm] is a sub-sequence of a, where 1<=p1<p2<.....<pm<=n.
For example, 4,14,2,3 and 14,1,2,3 are all sub-sequences of 4,13,14,1,2,3.
For the given sequence A, output the number of different sub-sequences. (Because the answer is larger, please mod 1000000007)
Input
The input contains multiple sets of data. The first behavior of each set of data is an integer n (1<=n<=1,000,000) that represents the number of sequence elements.
The second line contains n integers a[i] (0<=a[i]<=1,000,000) to represent each element in the sequence.
Output outputs an integer for one row, which is the number of different sub-sequences being asked. Because the answer is large, please mod 1000000007. Sample INPUT41 2 3 2 sample Output13 hint 40% data points 1<=n<=1000. Source
Tenth session of Fuzhou University program design Competition
#include <stdio.h>const __int64 mod=1000000007;__int64 dp[1000005],k[1000005];bool have[1000005];int main () { int n,a; while (scanf ("%d", &n) >0) {for (int i=0; i<=1000001; i++) { dp[i]=k[i]=0; have[i]=0; } for (int i=1; i<=n; i++) { scanf ("%d", &a); int flag=0; if (!have[a]) have[a]=1,flag=1; dp[i]= (Dp[i-1]*2-k[a]+flag+mod)%mod; k[a]=dp[i-1];//for the next occurrence of a number in the range of 1~ (i-1), because the number of 1~ (I-1) in the range of the next occurrence is equivalent to the current a combo } printf ("%i64d\n", dp[ n]);} }
Fzuproblem 2129 Sub-sequence number (DP)