题目链接:

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. jQuery 效果 - toggle() 方法切换元素的可见状态。

    定义和用法 toggle() 方法切换元素的可见状态. 如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素. 语法 $(selector).toggle(speed,callback, ...

  2. 基于STM32的简易磁卡充值系统

    使用的是MFRC522射频模块,把磁卡放入感应区后,可以执行三种操作: 初始化磁卡金额 读取卡内金额 向卡内写入金额(充值) 本来想着回学校了能把洗浴卡的金额给改掉,实现帝皇般的尊贵洗浴享受(不花钱… ...

  3. R语言学习笔记—决策树分类

    一.简介 决策树分类算法(decision tree)通过树状结构对具有某特征属性的样本进行分类.其典型算法包括ID3算法.C4.5算法.C5.0算法.CART算法等.每一个决策树包括根节点(root ...

  4. hash环/consistent hashing一致性哈希算法

        一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的 ...

  5. 20155301 2016-2017-2 《Java程序设计》第1周学习总结

    20155301 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 Java SE的全称是Java Platform, Standard Edition,并用于 ...

  6. !important用法:

    如果不加important特性,则超链接的颜色为蓝色,但是加上important之后优先级提高了,显示颜色为绿色 <style type="text/css"> a{ ...

  7. RHCSA-考前准备

    1.考前准备 RHCSA classroom虚拟机和server虚拟机 将两台虚拟机切换到初始化快照 打开虚拟机电源,当出现提示时选择我已移动该虚拟机 系统密码: classroom: root As ...

  8. day 1类 对象 属性 方法

    1. 解决吃啤酒鸭的问题 第一种方式(面向过程): 1)养鸭子 2)鸭子长成 3)杀 4)作料 5)烹饪 6)吃 7)卒 第二种方式(面向对象): 1)找个卖啤酒鸭的人 2)给钱 交易 3)吃 4)胖 ...

  9. 【LG5018】[NOIP2018pj]对称的二叉树

    [LG5018][NOIP2018pj]对称的二叉树 题面 洛谷 题解 看到这一题全都是用\(O(nlogn)\)的算法过的 考场上写\(O(n)\)算法的我很不开心 然后就发了此篇题解... 首先我 ...

  10. sqoop 数据迁移

    sqoop 数据迁移 1 概述 sqoop是apache旗下一款“Hadoop和关系数据库服务器之间传送数据”的工具. 导入数据:MySQL,Oracle导入数据到Hadoop的HDFS.HIVE.H ...