HDU 5496 Beauty of Sequence

来源:互联网 发布:linux gzip解压命令 编辑:程序博客网 时间:2024/05/22 12:51
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
351 2 3 4 541 2 1 353 3 2 1 2
Sample Output
24054

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