题目链接:

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代码:

 #include<iostream>
#include<cstdio>
#include<map>
using namespace std;
typedef long long LL; const int maxn = 1e5 + ;
const int mod = 1e9 + ; int a[maxn];
int n; int bin[maxn];
map<int, int> mymap; void table() {
bin[] = ;
for (int i = ; i < maxn; i++) bin[i] = bin[i - ] * % mod;
} void init() {
mymap.clear();
} int main() {
int tc;
table();
scanf("%d", &tc);
while (tc--) {
init();
scanf("%d", &n);
LL ans = ;
for (int i = ; i <= n; i++) {
scanf("%d", a + i);
//mymap[a[i]]表示在i之前,所有以a[i]结尾的子序列的数目
//bin[n-1]表示所有包含i的子序列,而mymap[a[i]]*bin[n-i]代表的就是不符合条件的子序列了。
LL tmp = ((bin[n - ] - (LL)bin[n - i] * mymap[a[i]])%mod +mod) % mod;
ans = (ans + a[i] * tmp) % mod;
mymap[a[i]] += bin[i - ];
mymap[a[i]] %= mod;
}
printf("%lld\n", ans);
}
return ;
}

HDU 5496 Beauty of Sequence的更多相关文章

  1. Hdu 5496 Beauty of Sequence (组合数)

    题目链接: Hdu 5496 Beauty of Sequence 题目描述: 一个整数序列,除去连续的相同数字(保留一个)后,序列的和成为完美序列和.问:一个整数序列的所有子序列的完美序列和? 解题 ...

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

    Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. HDU 5783 Divide the Sequence(数列划分)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  4. hdu-5496 Beauty of Sequence(递推)

    题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  5. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

  6. 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区间上的所以数变成 ...

  7. Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) (C++,Java)

    Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) Hdu 5806 题意:给出一个数组,求区间第k大的数大于等于m的区间个数 #include<queue> # ...

  8. HDU 5063 Operation the Sequence(暴力)

    HDU 5063 Operation the Sequence 题目链接 把操作存下来.因为仅仅有50个操作,所以每次把操作逆回去执行一遍,就能求出在原来的数列中的位置.输出就可以 代码: #incl ...

  9. HDU 5288 OO’s Sequence [数学]

     HDU 5288 OO’s Sequence http://acm.hdu.edu.cn/showproblem.php?pid=5288 OO has got a array A of size ...

随机推荐

  1. Redis之Redis持久化

    Redis(Remote Dictionary Server)是一个可持久化的内存.Key-Value数据库. 作为内存数据库,为了防止因服务器断电或系统宕机而引起的数据丢失问题,Redis自带了持久 ...

  2. 微信小程序页面3秒后自动跳转

    setTimeout() 是属于 window 的方法,该方法用于在指定的毫秒数后调用函数或计算表达式. 语法格式可以是以下两种:   setTimeout(function () { // wx.r ...

  3. php计算上个月是几月份

    PHP计算上个月的时间, $date = date("Y-m-d"); $arr = explode('-',$date); foreach ($arr as $key=>$ ...

  4. S3 Zeta使用python和opencv

    一.为SDK的Buildroot打上下面的补丁 diff --git a/package/opencv/Config.in b/package/opencv/Config.in index c046b ...

  5. Go黑帽子

    使用go语言来实现python黑帽子和绝技的代码 1.unix密码破解器 package main import( "bufio" "flag" "i ...

  6. 格式化输出%02hhx

    每次看到人家的十六进制输出,对齐的很好,ff就显示了,而我的总是0xffffffff.如果是"%02x",是以0补齐2位数,如果超过2位就显示实际的数:"%hhx&quo ...

  7. python的MetaClass的代码分析。基于廖雪峰博客代码

    # 一张表一个类,表内每一行就是一个实例 ''' 一个单独的元类使用的程序分析. ''' class Field(object): def __init__(self, name, column_ty ...

  8. Nginx入门篇(五)之LNMP环境应用

    一.LNMP组合工作原理 (1)用户通过浏览器输入域名请求Nginx web服务: (2)Nginx对请求的资源进行判断,如果是静态资源,则由Nginx返回给用户:如果是动态请求(.php文件),那么 ...

  9. Java 中的正则(Pattern)

    /**String 中 replaceAll(),matches(),split() 等方法,都是调用Pattern中的方法.学习了,瞬间觉得Pattern强大了 public String repl ...

  10. python-模块详解

    模块: 模块的分类: 第三方模块/扩展模块:没在安装python解释器的时候安装的那些功能 自定义模块:你写的功能如果是一个通用的功能,那你就把它当做一个模块 内置模块:安装python解释器的时候跟 ...