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当时卡在了二叉树. ...
随机推荐
- PDO操作
1.创建实例与取结果集 <? $db = new PDO('mysql:host=localhost;dbname=test', $user, $pass); $rs = $db->que ...
- Aliasing 走样
Computer Science An Overview _J. Glenn Brookshear _11th Edition Have you ever noticed the weird &quo ...
- C++ - 扩展欧几里德算法非递归实现
#include <iostream> using namespace std; int x, y; void get_x_y(int a, int b){ int q, r[3], s[ ...
- 【php学习】array_map,array_walk,array_filter的区别
array_map(function($v){return $v+1;}, $array); array_walk($array, function($v, $k){...}); array_filt ...
- scandir 使用示例
int filter_fn(const struct dirent * ent) { if (ent->d_type != DT_REG) return 0; r ...
- xftp的使用
1.xftp 一个基于 MS windows 平台的功能强大的SFTP.FTP 文件传输软件 2.下载安装 *3.在linux上安装服务 sudo yum install vsftp
- Servlet Threading Model
Servlet Threading Model The scalability issues of Java servlets are caused mainly by the server thre ...
- javaScript没有块级作用域
1.如下,变量i,j,k 的作用域是相同的. function test(obj){ var i= 0; if(typeof obj == "object"){ var j = 0 ...
- DependencyProperty深入浅出
写这篇心得之前,看到博友一句话:需求是推动发展的原动力. 说得好,说的棒,说到了点子上,说到了心里去: 好我们开始 最初的世界是简单的,甚至比单细胞动物还简单: 普通属性定义 public class ...
- php--纯静态和伪静态的区别与关系
先前说了什么是纯静态和伪静态,现在介绍一下他们的区别? 首先肯定的是纯静态和伪静态都是SEO的产物,但纯静态和伪静态还是有很大区别的.纯静态是生成真实的HTML页面保存到服务器端,用户访问时直接访问这 ...