HDU5086Revenge of Segment Tree(数论)
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(数论)的更多相关文章
- HDU-5086-Revenge of Segment Tree
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5086 这题太不应该了,比赛时没做出来,本来呢,以现在的水平这题是能够做出来的,可就是题目理解错了,按题 ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- 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), ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
随机推荐
- 线段树菜鸟一题+归并排序【求逆序数】POJ2299
题目链接:http://poj.org/problem?id=2299 归并排序解法链接:http://blog.csdn.net/lyy289065406/article/details/66473 ...
- <一年成为Emacs高手>更新到20130706版
这次更新比较多,加了第三方精品插件推荐,添加了我认为不错的Emacs社区. 见 原文
- Repeater
- Bootstrapping (compilers) - Wikipedia, the free encyclopedia
Bootstrapping (compilers) - Wikipedia, the free encyclopedia Bootstrapping (compilers)
- Android源代码分析-资源载入机制
转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/23387079 (来自singwhatiwanna的csdn博客) 前言 我们 ...
- 使用Python在2M内存中排序一百万个32位整数
译言网 | 使用Python在2M内存中排序一百万个32位整数 使用Python在2M内存中排序一百万个32位整数 译者:小鼠 发表时间:2008-11-13浏览量:6757评论数:2挑错数:0 作者 ...
- discuz清空session,导致session保存机制失败,session无法更新与解决
<?php function userErrorHandler() { $e = func_get_args(); echo '<pre style="color:red;&qu ...
- 关于NSArray的几种排序:
#利用数组的sortedArrayUsingComparator调用 NSComparator 当中NSComparator事实上就是一个返回NSComparisonResult的block. ty ...
- mysql 数据库备份ubuntu
安装 1 sudo apt-get update 2. sudo apt-get install mysql-server 3 sudo apt-get install mysql-client 4 ...
- UVA796- Critical Links(无向图中的桥梁)
题目链接 题意: 给出一个无向图,按顺序输出桥 思路:求出全部的桥,然后按顺序输出就可以 代码: #include <iostream> #include <cstdio> # ...