HDU 5496 Beauty of Sequence

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=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 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

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.

Output

For each test case, print the answer modulo 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

题意分析:

  题目 是让我们求所有子序和的总和,并且在一个子序列中相邻且相等的数不重复累加(相当于当成一个数)。

题解:

  当你一个问题想不通的时候,可以换一个角度来思考。

  一开始直接想统计结果,但是明显统计量是天文数字,于是觉得是不是有什么规律,也没想出来,于是就想从反面思考这个问题,既然不能直接求和,那么能不能转而去求每个点对最后的ans的贡献呢。小试了一下,发现可行。

  另外一个问题,在一个子序列中相邻相同点只考虑一次。可以选择在这样的子序列中只计算第一个点的贡献值。也就是,考虑一个点的贡献值,只要考虑那些包含它且在它前面没有与它相等的点的所有子序列,我们可以换个角度去求这样的子序列个数,统计所有包含改节点的子序列,然后减去不符合条件的子序列(具体看代码注释)

ac代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<map>
 4 using namespace std;
 5 typedef long long LL;
 6
 7 const int maxn = 1e5 + 10;
 8 const int mod = 1e9 + 7;
 9
10 int a[maxn];
11 int n;
12
13 int bin[maxn];
14 map<int, int> mymap;
15
16 void table() {
17     bin[0] = 1;
18     for (int i = 1; i < maxn; i++) bin[i] = bin[i - 1] * 2 % mod;
19 }
20
21 void init() {
22     mymap.clear();
23 }
24
25 int main() {
26     int tc;
27     table();
28     scanf("%d", &tc);
29     while (tc--) {
30         init();
31         scanf("%d", &n);
32         LL ans = 0;
33         for (int i = 1; i <= n; i++) {
34             scanf("%d", a + i);
35             //mymap[a[i]]表示在i之前,所有以a[i]结尾的子序列的数目
36             //bin[n-1]表示所有包含i的子序列,而mymap[a[i]]*bin[n-i]代表的就是不符合条件的子序列了。
37             LL tmp = ((bin[n - 1] - (LL)bin[n - i] * mymap[a[i]])%mod +mod) % mod;
38             ans = (ans + a[i] * tmp) % mod;
39             mymap[a[i]] += bin[i - 1];
40             mymap[a[i]] %= mod;
41         }
42         printf("%lld\n", ans);
43     }
44     return 0;
45 }

时间: 2024-10-05 04:33:20

HDU 5496 Beauty of Sequence的相关文章

HDU 5496——Beauty of Sequence——————【考虑局部】

Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 384    Accepted Submission(s): 168 Problem Description Sequence is beautiful and the beauty of an integer sequence is defined a

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 5063 Operation the Sequence(暴力)

HDU 5063 Operation the Sequence 题目链接 把操作存下来,由于只有50个操作,所以每次把操作逆回去运行一遍,就能求出在原来的数列中的位置,输出即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 100005; const ll MOD = 100000

hdu 4893 Wow! Such Sequence!(线段树)

题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 间l到r区间上的所以数变成最近的斐波那契数,相等的话取向下取. 解题思路:线段树,对于每个节点新增一个bool表示该节点以下的位置是否都是斐波那契数. #include <cstdio> #include <cstring> #include <cstdlib> #include <algor

HDU Wow! 4893 Such Sequence!(线段树)

HDU 4893 Wow! Such Sequence! 题目链接 题意:给定一个序列,3种操作,单点添加值,查询区间和,把区间和变成最接近的婓波那契数 思路:线段树,就是第三个操作麻烦,就在结点添加一个值,标记它区间是不是都是婓波那契数了,然后修改区间的时候,如果区间是了就不用修改,如果不是就继续往后一层推即可 代码: #include <cstdio> #include <cstring> #include <cstdlib> #define lson(x) ((x

HDU 4893 Wow! Such Sequence! 水线段树

思路: 线段树走起.. 写完这题就退役T^T 单点更新的时候直接找到这个点的最近fib,然后维护当前和 和 fib的和 #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<algorithm> #include<queue> #include<map> #include<set> #include&l

hdu 4893 Wow! Such Sequence!(线段树功能:单点更新,区间更新相邻较小斐波那契数)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4893 --------------------------------------------------------------------------------------------------------------------------------------------

hdu-5496 Beauty of Sequence(递推)

题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 813    Accepted Submission(s): 379 Problem Description Sequence is beautiful and the beauty of an integer sequence is def

HDU 4893 Wow! Such Sequence! (线段树)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 838    Accepted Submission(s): 245 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot