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(递推)的更多相关文章

  1. hdu 5086 Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree                                                          Time Limit: 4000/20 ...

  2. [ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  3. HDU5086——Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...

  4. hdu5086——Revenge of Segment Tree

    Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  5. UVALive - 6577 Binary Tree 递推+找规律

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48421 Binary Tree Time Limit: 3000MS 问题描述 Binary Tree is ...

  6. HDU5086:Revenge of Segment Tree(规律题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5086 #include <iostream> #include <stdio.h> #i ...

  7. hdu 5086(递推)

    Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  8. BestCoder#16 A-Revenge of Segment Tree

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  9. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

随机推荐

  1. regardless of how many processors are devoted to a parallelized execution of this program

    https://en.wikipedia.org/wiki/Amdah's_law Amdahl's law is often used in parallel computing to predic ...

  2. (转)JAVA 调用matlab

    本文仅用于学习. 原文地址链接:http://blog.csdn.net/wannshan/article/details/5907877 前段时间摸索了java调用matlab东西,不说学的有多深, ...

  3. php面向对象之__toString()

    似曾相识,在php面向对象编程之魔术方法__set,曾经介绍了什么是魔术方法,这一章又介绍一个魔术方法__tostring(). __toString()是快速获取对象的字符串信息的便捷方式,似乎魔术 ...

  4. Java Blocking Queue

    //Listing 8-1. The Blocking Queue Equivalent of Chapter 3’s PC Application import java.util.concurre ...

  5. the differences between function and procedure

    一.自定义函数: 1. 可以返回表变量 2. 限制颇多,包括 不能使用output参数: 不能用临时表: 函数内部的操作不能影响到外部环境: 不能通过select返回结果集: 不能update,del ...

  6. 在Windows上一键编译各种版本的Protobuf

    所需工具 : cmake  for  windows 和  git for windows 原理:protobuf 是google的一个开源项目,其源代码在github上可以下载到,并且源码都采用cm ...

  7. C# 字符串的截取和替换

    1.取字符串的前n个字符 (1)string str1=str.Substring(0,n); (2)string str1=str.Remove(i,str.Length-n); 2.去掉字符串的前 ...

  8. 安装Postman

    原文地址:http://blog.csdn.net/ouyang111222/article/details/45743831 ** (一)安装篇 ** Postman是一款功能强大的网页调试与发送网 ...

  9. 第二条 一个类如果有多个参数,考虑用Builder构造者模式

    1. @Data public class Student { //体检用 private String name; private int age; private int height; priv ...

  10. http://blog.csdn.net/yangyuhan156/article/details/48899439

    http://blog.csdn.net/yangyuhan156/article/details/48899439