题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=5086

这题太不应该了,比赛时没做出来,本来呢,以现在的水平这题是能够做出来的,可就是题目理解错了,按题目的意四的话很好做,直接找规律,

题意

给定一个序列,求这个序列的子序列的和,再求所有子序列总和,这些子序列是连续的。

例如

1 2 4

{1 }    { 2 }     { 4 }    { 1  2 }   {2  4}  {1  2  4}

1+2+4+1+2+2+4+1+2+4=23

找规律

我们假设n等于5。序列为1、2、3、4、5。然后我们将它们如下排列,每行表示一个序列

1

2
1 2

3
2 3
1 2 3

4
3 4
2 3 4
1 2 3 4

5
4 5
3 4 5
2 3 4 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),题目给的数据,是不会超时的。

代码

#include<stdio.h>

const int mod=1000000007;

int main(void)
{
int t;
__int64 n,num,sum,i;
scanf("%d",&t);
while(t--)
{
sum=0;
scanf("%I64d",&n);
for(i=1;i<=n;i++)
{
scanf("%I64d",&num);
sum=(sum+(num*i)%mod*(n-i+1)%mod)%mod;
}
printf("%I64d\n",sum);
}
return 0;
}

错误的地方在我理解的是:a[i]数的连续和

如按我的理解

1 2 4

的结果是

{1}  {2}  {4}  {1 2}

答案是10 而不是23 也不是那么好做了。

HDU-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. HUD 5086 Revenge of Segment Tree(递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5086 题目大意: 给定一个序列,求这个序列的子序列的和,再求所有子序列总和,这些子序列是连续的.去题目给的第二组 ...

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

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

  5. hdu5086——Revenge of Segment Tree

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

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

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

  7. BestCoder#16 A-Revenge of Segment Tree

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

  8. hdu 5086(递推)

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

  9. HDU 1542/POJ 1151 Atlantis (scaning line + segment tree)

    A template of discretization + scaning line + segment tree. It's easy to understand, but a little di ...

  10. HDU5086Revenge of Segment Tree(数论)

    HDU5086Revenge of Segment Tree(数论) pid=5086" target="_blank" style="">题目 ...

随机推荐

  1. HTML5的兼容问题以及调用js文件的方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. HDU 4287 Intelligent IME(字典树)

    在我没用hash之前,一直TLE,字符串处理时间过长,用了hash之后一直CE,(请看下图)我自从经历我的字典树G++MLE,C++AC以后,一直天真的用C++,后来的CE就是因为这个,G++才支持这 ...

  3. C#使用FFmpeg 将视频格式转换成Gif图片示例

    根据EFmpeg封装的视频转换gif工具:https://my.oschina.net/tianma3798/blog/825317 一.本次使用参数说明 /* * 参数说明: * -i 源文件位置 ...

  4. 两列布局,读《css那些事儿》

    两列布局: 1.两列定宽: 要点:float.width固定. :after清除浮动. 前提:两列的盒模型宽度相加不能大于父元素的宽度,否则会出现错位现象. <!DOCTYPE html> ...

  5. C#调用SCL2008 动态库SCL_API_cdecl.dll or SCL_API_Stdcall.dll,提示找不到指定模块

    在公司用的联想 ThinkPad T430i,原装配置及系统,开机巨慢.按下开机键到登陆界面超过1分钟,再到桌面这个时间就难说了,多时有超过3分钟,进入桌面还要等待几分钟才能正常操作.新年过来向公司领 ...

  6. STL中map的一个知识点

    问题背景 在做USACO Section 1.1 Greedy Gift Givers的时候,我最初的想法是直接用一个map来进行数据处理.但是后来产生一个让我感到疑问的地方,后来我经过测试,发现了这 ...

  7. html精确定位

    1.offsetwidth.offsetHeight是指包含border的元素宽高. 2.clientWidth.clientHeight是指不包含border的元素宽高. 3.scrollWidth ...

  8. JAVA语法题

    import java.util.*; public class Birthdays { public static void main(String[] args){ Map<Friends, ...

  9. Android MulticastSocket IP组播

    MulticastSocket是对等的关系,也就是他们通过IP多点组播保留的地址来相互接收在这个组的Socket发送的消息 我写了一个例子,MultiSocketB是一个service,用于先接收再发 ...

  10. iOS之NSDictionary和NSArray以及NSMutableDictionary和NSMutableArray:将不再是问题

    字典的key就相当于数组的下标,怎样操作数组你就学会了怎样才做字典:来感受一把 需要注意的是读取文件的类型要集合.plist文件的rooty:否则将不能读入文件 // 1.全国省市:得到的是省和市 / ...