您的位置:首页 > 产品设计 > UI/UE

hdoj 5496 Beauty of Sequence

2016-03-02 17:32 357 查看

Beauty of Sequence

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

Total Submission(s): 767 Accepted Submission(s): 348



[align=left]Problem Description[/align]
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}.

[align=left]Input[/align]
There are multiple test cases. The first line of input contains an integer
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.

[align=left]Output[/align]
For each test case, print the answer modulo
109+7
in a single line.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

240
54
144


给出由n个数组成的序列,求出所有去重后的子序列的和;

子序列的末尾元素有n种,a[1],a[2],a[3]...a


设f[i]为前i个元素组成序列的所有子序列的和(去重后);

设add表示以元素a[i]结尾的子序列的和(去重后);

则f[i]=f[i-1]+add;

设sum表示以元素a[i]结尾的子序列的和(没有去重);

则sum=F[i-1](不管怎样F[i-1]一定会有)+2^(i-1)*a[i](我们贪心的认为前面所有方案都成立,然后去重)。

设重复包含a[i]的序列有same[a[i]]个,那么add=sum-same[a[i]]*a[i]。

可以选择用map建立映射<LL, LL>same计算same[[ai]];

same[a[i]]=same[a[i]]+2^(i-1);

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<map>
#define LL long long
#define MAXN 100000+10
#define MOD 1000000007
LL a[MAXN];
LL f[MAXN];//f[i]为前i个元素组成序列的所有子序列的和(去重后)
map<LL,LL>same;
LL pow_mod(LL a,LL b)//a的b次方
{
LL ans=1;
while(b)
{
if(b&1)
ans=ans*a%MOD;
a=a*a%MOD;
b=b/2;
}
return ans;
}
int main()
{
int T,n,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
same.clear();
f[0]=0;
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
LL sum=(f[i-1]+pow_mod(2,i-1)*a[i]%MOD)%MOD;
//sum表示以元素a[i]结尾的子序列的和(没有去重)
LL add=(sum+MOD-same[a[i]]*a[i]%MOD)%MOD;
//add表示以元素a[i]结尾的子序列的和(去重后)
f[i]=(f[i-1]+add)%MOD;
same[a[i]]=(same[a[i]]+pow_mod(2,i-1))%MOD;
//重复包含a[i]的序列有same[a[i]]个
}
printf("%lld\n",f
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: