HDU 5496 Beauty of Sequence

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 AA of nn integers {a_1,a_2,...,a_n}{a1,a2,...,an}. You need find the summation of the beauty of all the sub-sequence of AA. As the answer may be very large, print it modulo 10^9+7109+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}{1,3,2} is a sub-sequence of {1, 4, 3, 5, 2, 1}{1,4,3,5,2,1}.

Input

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1 \le n \le 10^5)(1n105), indicating the size of the sequence. The following line contains nn integers a_1,a_2,...,a_na1,a2,...,an, denoting the sequence (1 \le a_i \le 10^9)(1ai109).

The sum of values nn for all the test cases does not exceed 20000002000000.

Output

For each test case, print the answer modulo 10^9+7109+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

找到递推方法然后就好了。。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
const int maxn = 300005;
const LL base = 1e9 + 7;
int T, n, m, a[maxn], b[maxn], c[maxn];
LL now, bef, f[maxn], sum[maxn], tot;

int main()
{
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &n);    tot = 0;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			b[i] = a[i];
			sum[i] = f[i] = 0;
		}
		sort(b + 1, b + n + 1);
		m = unique(b + 1, b + n + 1) - b;
		now = 1;
		for (int i = 1; i <= n; i++) c[i] = lower_bound(b + 1, b + m, a[i]) - b;
		for (int i = 1; i <= n; i++)
		{
			bef = (now - f[c[i]] + base) % base*a[i] % base;
			(f[c[i]] += now) %= base;
			(sum[c[i]] += tot + bef) %= base;

			((tot <<= 1) += bef) %= base;
			(now <<= 1) %= base;
		}
		printf("%I64d\n", tot);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值