hdu5496
2015-10-24 20:36
295 查看
Beauty of Sequence
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 668 Accepted Submission(s): 297
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
参考链接:
官方题解 http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html http://blog.csdn.net/one_piece_hmh/article/details/48897927
我猜到了开头,却没有猜到结尾。想到了可能是求每个数的贡献,但是却没有推出求每个数贡献的简便方法。还是做题少,不思考。
遇到很到的数列求和,或是什么的,有可能是通过求每个数的贡献来求解最后答案。
(首尔府年糕紫菜包饭的菜米饭好吃又便宜
)
已ac的代码:
#include<stdio.h> #include<string.h> #include<map> #include<iostream> using namespace std; #define N 100010 #define MOD 1000000007 int num ; map<int,int> mapex;//傻了吧,其实一维的就够了,把前面的以相同的数为结尾的都加起来,省时间又省空间。 long long int km(long long int x,long long int y){ long long int ans=1; while(y){ if(y%2){ ans=ans*x%MOD; } y=y/2; x=x*x%MOD; } return ans; } int main(){ int t; int n; long long int ans; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&num[i]); } mapex.clear();//记住,是clear,不是erase ans=0; for(int i=0;i<n;i++){ //printf("%d %lld %lld\n",mapex[num[i]],km(2,n-i-1),km(2,i)); long long int tempfro=((km(2,i)-mapex[num[i]])%MOD+MOD)%MOD; ans=(ans+tempfro*km(2,n-1-i)%MOD*num[i]%MOD)%MOD; mapex[num[i]]=(mapex[num[i]]+km(2,i))%MOD; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- php引用传值
- android 控件不同状态 selector----drawable
- 安卓软件第一次打开时引导界面以及打开后停留界面
- php.ini 核心配置选项说明
- 执行shell脚本方法总结
- C++有符号和无符号之间的转换
- 把eclipse大括号向VS一样上下对齐
- 学习笔记三:类和数据抽象
- 解析jQuery的链式编程
- ASP导出为Word或Excel的最简单方法
- Uva10474——Where is the Marble?
- Xcode开发中遇到的Bug
- Hough变换在直线提取中的改进
- 图画(txt等一下)实施开放的默认下载的默认浏览器,而不是(Java文本)
- 【noip2012提高组】国王游戏
- IOS开发-C基础2
- 倍增和LCA
- [c#][Windows Phone | Windows]FlipView循环滚动,图片轮播
- iOS NSUserDefaults 之最简单存取数据 小Demo
- linux 软件/库 命令集合