HUD 5086 Revenge of Segment Tree(递推)
http://acm.hdu.edu.cn/showproblem.php?pid=5086
题目大意:
给定一个序列,求这个序列的子序列的和,再求所有子序列总和,这些子序列是连续的。去题目给的第二组数据的
3
1 2 3
这个序列的子序列有 [1]、[2]、[3]、[1、2]、[2、3]、[1、2、3],这些子序列的和是分别是1、2、3、3、5、6。再将这些和加起来
1+2+3+3+5+6=20这个就是最终的答案。
解题思路:
我们假设n等于5。序列为1、2、3、4、5。然后我们将它们如下排列,每行表示一个序列
我们从中会发现序列中的a[i](表示序列第i个数),不管在那堆里面,a[i]有i个。总共有几个a[i]*i呢,可以看出有n-i+1个。
所以推出公式为∑a[i]*i*(n-i+1)就是正确的答案了
为什么我们要推公式,是因为我们暴力做的话时间复杂度是O(n^2),根据题目给的数据,肯定会超时。
推出的公式的时间复杂度是O(n),题目给的数据,是不会超时的。
AC代码:
include<stdio.h>
#define MOD 1000000007
typedef __int64 LL;
int main(){
int t;
LL sum, num, n;
scanf("%d", &t);
while(t--){
scanf("%I64d", &n);
sum = ;
for(LL i = ; i <= n; ++i){
scanf("%I64d", &num);
sum = (sum + num * i % MOD * (n - i + ) % MOD) % MOD;
}
printf("%I64d\n", sum);
}
return ;
}
HUD 5086 Revenge of Segment Tree(递推)的更多相关文章
- hdu 5086 Revenge of Segment Tree(BestCoder Round #16)
Revenge of Segment Tree Time Limit: 4000/20 ...
- [ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- HDU5086——Revenge of Segment Tree(BestCoder Round #16)
Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...
- hdu5086——Revenge of Segment Tree
Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- UVALive - 6577 Binary Tree 递推+找规律
题目链接: http://acm.hust.edu.cn/vjudge/problem/48421 Binary Tree Time Limit: 3000MS 问题描述 Binary Tree is ...
- HDU5086:Revenge of Segment Tree(规律题)
http://acm.hdu.edu.cn/showproblem.php?pid=5086 #include <iostream> #include <stdio.h> #i ...
- hdu 5086(递推)
Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
随机推荐
- software glue Middleware
https://en.wikipedia.org/wiki/Middleware https://zh.wikipedia.org/wiki/中间件 Middleware is computer so ...
- java CyclicBarrier
import java.io.IOException; import java.util.Random; import java.util.concurrent.BrokenBarrierExcept ...
- 蓝牙—GAP(Generic Access Profile)
1.简介 下图可见GAP在蓝牙协议中的位置和关系 LE中GAP共有四个角色: <1> Boradcaster:发送advertising 事件的设备 <2>Observer:接 ...
- 三种查看SqlServer中数据物理pge页的方法
1.根据数据记录查看当前记录所在的文件编号.page页.以及在页中的插槽. 示例如下: SELECT top %%physloc%%, sys.fn_physlocFormatter (%%physl ...
- lua环境安装 转
curl -R -O http://www.lua.org/ftp/lua-5.2.2.tar.gz tar zxf lua-5.2.2.tar.gz cd lua-5.2.2 make linux ...
- Redis学习笔记(7)-事务
package cn.com; import java.util.List; import redis.clients.jedis.Jedis; import redis.clients.jedis. ...
- 自动将每日的日志增量导入到hive中
一:大纲介绍 1.导入方式 load data local inpath 'local_file_path' into table tbname partition (date='',hour='') ...
- SIP学习(实例详解)
本文摘自:http://blog.chinaunix.net/uid-20655530-id-1589483.html 学习 SIP 协议最快捷的方法是通过范例来学习, 找到了一个完整的呼叫流程,le ...
- Swift-01 UIWebView加载网页
UIWebView在swift里面的语法,和OC不太一样,但是,使用方法什么的,都是从OC演变过来的.比如,都得有init方法,都有loadRequest方法,所以,有了OC这个基础,学习swift是 ...
- DMV to track the temp file usage for SQLServer
There are three DMVs you can use to track tempdb usage: sys.dm_db_task_space_usagesys.dm_db_session_ ...