Problem Description
OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) , that there's no j(l<=j<=r,j<>i) satisfy ai mod aj=0,now OO want to know

∑i=1n∑j=inf(i,j) mod (109+7).
 
Input
There are multiple test cases. Please process till EOF.
In each test case: 
First line: an integer n(n<=10^5) indicating the size of array
Second line:contain n numbers ai(0<ai<=10000)
 

Output

For each tests: ouput a line contain a number ans.
 

Sample Input

5
1 2 3 4 5
 
Sample Output
23
 
Author
FZUACM
 
Source
 
一考思维,就想GG,搞了好长时间
这道题目的解法是: 通过预处理,记录 a[i] 的左右边界(所谓的左右边界时 在从 a[i] 当前位置往左往右找,找到左边第一个和右边第一个能够整除 a[i] 的数,这两个数就是a[i]的左右边界)然后记录到 l[] & r[] 中, 这样 a[i] 对 ans 的贡献是 (i - l[i]) * (r[i] - i);
在预处理 l[] 数组时,用pre[j]标记一下 j (表示 j 最后一次出现的位置),如果 j 在之前已经遇到过且右边界没有被更新过,就将 pre[j] 的边界更新到当前 i 所在的位置。
 
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define LL long long
#define MAXN 100010
using namespace std;
const LL MOD = 1e9 + ;
int a[MAXN];
LL l[MAXN], r[MAXN], pre[MAXN], last[MAXN];
int main() {
int n;
while(~scanf("%d", &n)) {
for(int i = ; i <= n; ++i) {
scanf("%d", a + i);
l[i] = , r[i] = n+;
}
memset(pre, , sizeof(pre));
memset(last, , sizeof(last)); for(int i = ; i <= n; ++i) {
for(int j = a[i]; j <= ; j += a[i]) {
           //通过这个循环,如果a[i]是之前一个数的因子,一定会在后边遇到
if(pre[j] != && r[pre[j]] == n+) {
              //这个数字 j (j = x * a[i])之前已经出现,且右边界是最右端
r[pre[j]] = i;   
                  //这时更新pre[j]的右端, pre[j] 表示的是 j 最后出现的位置
}
}
pre[a[i]] = i; //当前pre[a[i]] 最后出现的位置是 i
} for(int i = n; i > ; --i) {
for(int j = a[i]; j <= ; j += a[i]) {
if(last[j] != && l[last[j]] == ) {
l[last[j]] = i;
}
}
last[a[i]] = i;
}
LL ans = ;
for(int i = ; i <= n; ++i) {
ans = (LL) (ans % MOD + (LL)(((i-l[i])*(r[i]-i)%MOD)%MOD));
ans %= MOD;
}
cout << ans << endl;
}
return ;
}

HDU5288 OO’s Sequence的更多相关文章

  1. hdu5288 OO’s Sequence 二分 多校联合第一场

    OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  2. 解题报告 之 HDU5288 OO&#39; s Sequence

    解题报告 之 HDU5288 OO' s Sequence Description OO has got a array A of size n ,defined a function f(l,r) ...

  3. 2015 Multi-University Training Contest 1 - 1001 OO’s Sequence

    OO’s Sequence Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5288 Mean: 给定一个数列,让你求所有区间上满足 ...

  4. HDU 5288 OO‘s sequence (技巧)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5288 题面: OO's Sequence Time Limit: 4000/2000 MS (Jav ...

  5. HDOJ 5288 OO’s Sequence 水

    预处理出每一个数字的左右两边能够整除它的近期的数的位置 OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 13 ...

  6. HDU 5288 OO’s Sequence 水题

    OO's Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5288 Description OO has got a array A ...

  7. HDU 5288——OO’s Sequence——————【技巧题】

    OO’s Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  8. Hdu 5288 OO’s Sequence 2015多小联赛A题

    OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  9. hdu 5288 OO’s Sequence(2015 Multi-University Training Contest 1)

    OO's Sequence                                                          Time Limit: 4000/2000 MS (Jav ...

随机推荐

  1. 使用spring注解@Controller @Service @Repository简化配置

    前言:在web项目中引入spring框架中的配置文件,我们给每一个java bean进行相关配置可以非常安全,便捷的管理我们的bean.那么,问题来了,如果一个项目中所涉及到的java bean十分庞 ...

  2. thinkphp上传

    上传代码 // 缩略图上传 $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = ;// 设置附件上传大小 $upload-> ...

  3. css3 动画效果 总结 不断完善~~

    1.transition 动画过程改变某个css属性的效果 (比如宽高 颜色) 语法 transition:    all  所有元素                                + ...

  4. sublime 编译运行C程序

    { "cmd": ["gcc", "${file}", "-o","${file_path}/${file_b ...

  5. MongoDB C Driver使用教程

    MongoDB C Driver使用教程 转载请注明出处http://www.cnblogs.com/oloroso/ 本指南提供简介 MongoDB C 驱动程序. 在 C API 的详细信息,请参 ...

  6. css3弹性盒模型

    一.简介 css3引入了新的盒模型——弹性盒模型,该模型决定一个盒子在其他盒子中的分布方式以及如何处理可用的空间.使用该模型,可以很轻松的创建自适应浏览器窗口的流动布局或自适应字体大小的弹性布局. 目 ...

  7. asp.net中membership使用oracle数据库(一)

    第一步 数据库的准备 使用 oracle 11g的数据库 需要安装好,安装过程中先决条件检查失败的处理:确认server服务已运行 cmd->net share c$=c: 就可以通过 orac ...

  8. sql之left join、right join、inner join的区别

    sql之left join.right join.inner join的区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括 ...

  9. javaWeb开发中的中文编码问题

    常规解决乱码问题的方法是: a.把所有的jsp页面的charset设置为UTF-8.   b.添加过滤器,在filter内调用request.setCharacterEncoding("ut ...

  10. cocoaPods 的使用

    打开 终端 1. 移除系统自带的 因为该rub已经被和谐了 使用ruby.taobao.org MCJ:~ MCJ$ sudo gem sources -l *** CURRENT SOURCES * ...