HDU5086Revenge of Segment Tree(数论)

pid=5086" target="_blank" style="">题目链接

题目大意:给出长度为n的数组。然后要求累计里面的每一个子串的和。

解题思路:枚举起点和终点,推断每一个数属于多少条线段。那么这个数就要被加这么多次。能够得出每一个位置被加次数的公式: i
(n - i + 1);那么结果就是累计(arr[i] i) mod * (n - i + 1) % mod,注意两个数相乘可能会超出long long型的数。

代码:

#include <cstdio>
#include <cstring> typedef long long ll; const int maxn = 1e6;
const ll mod = 1e9 + 7; ll arr[maxn]; int main () { int T, n;
scanf ("%d", &T);
while (T--) { scanf ("%d", &n);
for (int i = 1; i <= n; i++)
scanf ("%I64d", &arr[i]); ll ans = 0;
for (int i = 1; i <= n; i++)
ans = (ans + (arr[i] * i % mod) * (n - i + 1) % mod) % mod;
printf ("%I64d\n", ans);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU5086Revenge of Segment Tree(数论)的更多相关文章

  1. HDU-5086-Revenge of Segment Tree

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5086 这题太不应该了,比赛时没做出来,本来呢,以现在的水平这题是能够做出来的,可就是题目理解错了,按题 ...

  2. BestCoder#16 A-Revenge of Segment Tree

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

  3. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  4. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  5. Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  6. Segment Tree Query I & II

    Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...

  7. Segment Tree Build I & II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

  8. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  9. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

随机推荐

  1. 《Struts2技术内幕》学习笔记

    第2.3章 基础 三种类模式:属性-行为模式.属性模式.行为模式. 其中属性模式有:PO(持久化对象).BO(业务对象).VO(值对象).DTO(传输数据对象).FromBean(页面对象)他们是对J ...

  2. java垃圾回收那点事(二)不同gc策略的heap分配

    在前面的文章中曾提到了在java虚拟机启动的时候会对G1,CMS, SerialGC定义不同的heap的类,并且定义不同的policy. CollectorPolicy CollectorPolicy ...

  3. 数据结构——bitmap

    近期在看<编程珠玑>这本书. 第1章中引入了bitmap(位图)的数据结构. 曾经没有接触过, 抽出时间研究了一下,记录下来. 书中描写叙述的情景: 1. 最多1000万个7位数电话号码( ...

  4. SICP 解题集 — SICP 解题集

    SICP 解题集 — SICP 解题集 SICP 解题集¶ 这个文档的目标是成为中文化的.完整的<计算机程序的构造和解释>一书的解题集. 这个解题集的特色是: 对于每道习题,除了习题答案之 ...

  5. 利用Gearman实现并发查询(Multi-Query)

    这个样例是想从数据库查询出几个结果集,一般的做法是,一个接一个的发送查询,然后汇总结果进行输出. 以下我们利用Gearman的gearman_client_run_tasks实现并发的查询,gearm ...

  6. ThinkPHP的连贯操作方法中field方法

    ThinkPHP的连贯操作方法中field方法有很多的使用技巧,field方法主要目的是标识要返回或者操作的字段,下面详细道来. 1.用于查询 在查询操作中field方法是使用最频繁的. $Model ...

  7. ACdream 1114(莫比乌斯反演)

    传送门:Number theory 题意:给n个数,n 和 每个数的范围都是 1---222222,求n个数中互质的对数. 分析:处理出每个数倍数的个数cnt[i],然后进行莫比乌斯反演,只不过这里的 ...

  8. Java程序猿之从菜鸟到职场高手的必看

    J2SE之入门引导            Java基础系列之初识JAVA                                           Java基础系列之Java语法       ...

  9. Jeecg社区wiki在开放,最终能够在线看文档啦!!!

    Jeecg社区wiki在开放,最终能够在线看文档啦! .! 2014-12-18 scott JEECG jeecg开源社区wiki正式启动了.方便大家看文档 訪问地址是: http://osbaba ...

  10. Java中WebService实例

    Web Services是由企业公布的完毕其特定商务需求的在线应用服务,其它公司或应用软件可以通过Internet来訪问并使用这项在线服务. Web Service的关键技术和规则: 1.XML:描写 ...