Beauty of Sequence

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 576    Accepted Submission(s): 258



Problem Description

Sequence is beautiful and the beauty of an integer sequence is defined as follows: removes all but the first element from every consecutive group of equivalent elements of the sequence (i.e. unique function in C++ STL) and the summation of rest integers is the beauty of the sequence.

Now you are given a sequence  A of  n integers  {a1,a2,...,an}. You need find the summation of the beauty of all the sub-sequence of  A. As the answer may be very large, print it modulo  109+7.

Note: In mathematics, a sub-sequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example  {1,3,2} is a sub-sequence of  {1,4,3,5,2,1}.

 


Input

T, indicating the number of test cases. For each test case:

The first line contains an integer  n  (1≤n≤105), indicating the size of the sequence. The following line contains  n integers  a1,a2,...,an, denoting the sequence (1≤ai≤109).

The sum of values  n for all the test cases does not exceed  2000000.

 


Output

109+7 in a single line.

 


Sample Input

3 5 1 2 3 4 5 4 1 2 1 3 5 3 3 2 1 2

 


Sample Output

240 54 144

 


Source

​BestCoder Round #58 (div.2)​

 


Recommend

hujie   |   We have carefully selected several similar problems for you:   ​​5498​​​  ​​​5497​​​  ​​​5496​​​  ​​​5493​​​  ​​​5492​​ 

 

解析:摘自​​zimpha​​ (BC官方题解)

BestCoder Round #58  Beauty of Sequence 即hdu5496 (递推)_hdu5496

          具体的思路就是这样,不太懂的话,就只有靠自己多想想了。

代码:


#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<map>
using namespace std;

typedef long long LL;
const int maxn=1e5;
const int mod=1e9+7;
map<int, LL> f;
LL b[maxn+10];

int getin()
{
int ans=0;char tmp;
while(!isdigit(tmp=getchar()));
do ans=(ans<<3)+(ans<<1)+tmp-'0';
while(isdigit(tmp=getchar()));
return ans;
}

int main()
{
freopen("1.in","r",stdin);
int t,i,n,x; LL ans,tmp;
for(b[0]=i=1;i<=maxn;i++)b[i]=(b[i-1]<<1)%mod;
for(t=getin();t;t--)
{
n=getin(),ans=0,f.clear();
for(i=1;i<=n;i++)
{
tmp=(b[i-1]-f[(x=getin())]+mod)%mod;
//这里的这个+mod一定要添上,因为b[i]有
//取余操作,所以b[i]-f[x]是有可能小于0的
ans+=tmp*b[n-i]%mod*x,ans%=mod;
f[x]+=b[i-1],f[x]%=mod;
}
printf("%I64d\n",ans);
}
return 0;
}