HDU 5288 OO’s Sequence

http://acm.hdu.edu.cn/showproblem.php?pid=5288 
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 a i mod a j=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 a i(0 < a i <= 10000)  
Output 
For each tests: ouput a line contain a number ans. 
Sample Input 

1 2 3 4 5 
Sample Output 
23

这题的题意是后来看了题解才懂的,意思是给定一个序列,对于每一个数,包括这个数的区间里的其他的数都不是这个数的因数,求对于每一个数的区间的个数的总和。 
也就是说对于任何一个数,往左找到第一个它的因数的位置记作l,往右找到它的第一个因数的位置记作r,那么这个数对应的区间的个数是(i-l)*(r-i),然后对所有的数求一个和就可以了。 
因为这里数的范围只有1e4,所以一开始预处理1e4以内的数的因数存在vector[N]里。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
using namespace std;
typedef long long ll; const int maxn = 1e5+;
const int N = 1e4+;
const int mol = 1e9+;
int arr[maxn],l[maxn],r[maxn],vis[N];
vector <int> vi[N]; int main()
{
for(int i=;i<N;i++)
for(int j=;j<=sqrt(i);j++)
if(i%j == )
{
vi[i].push_back(j);
if(j*j != i) vi[i].push_back(i/j);
}
int n;
while(~scanf("%d",&n))
{
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(vis,,sizeof(vis));
ll ans = ;
for(int i=;i<=n;i++)
scanf("%d",&arr[i]);
for(int i=;i<=n;i++)
{
int tp = ;
for(int j=;j<vi[arr[i]].size();j++)
tp = max(tp,vis[vi[arr[i]][j]]);
l[i] = tp;
vis[arr[i]] = i;
}
for(int i=;i<N;i++) vis[i] = n+;
for(int i=n;i>;i--)
{
int tp = n+;
for(int j=;j<vi[arr[i]].size();j++)
tp = min(tp,vis[vi[arr[i]][j]]);
r[i] = tp;
vis[arr[i]] = i;
}
for(int i=;i<=n;i++)
ans = (ans + 1LL*(i-l[i])*(r[i]-i) % mol) % mol;
printf("%lld\n",ans);
}
}

最新文章

  1. centos中基于随机数,再加入班级学生姓名
  2. 《BI项目笔记》报到信息分析Cube
  3. 一步一步写miscdevice的驱动模块
  4. java-Filter
  5. SQL数据库约束行为---防止数据乱填(即数据规范化)
  6. 初学Ajax(一)
  7. 数据搬运工DSS~介绍
  8. 折腾iPhone的生活——运营商信号显示数据化
  9. CodeForces 221(div 2)
  10. li里的a标签浮动了,为什么li本身也浮动了?
  11. [UWP]实用的Shape指南
  12. 调试JDK源码时,不能查看变量的值
  13. mvc一对多模型表单的快速构建
  14. 恶补web之八:jQuery(1)
  15. 委托(3).net 2.0中的委托
  16. [转帖]Docker的数据管理(volume/bind mount/tmpfs)
  17. Django——博客项目
  18. 18. 4Sum(双指针)
  19. c++ cmakelist 详解
  20. TensorFlow --- 01初识

热门文章

  1. 网络流入门——EK算法
  2. Nginx学习(1)--- 介绍与安装
  3. 面试准备专题——JVM,类编译,类加载,内存错误
  4. google浏览器中 查看记住的账号和密码
  5. JavaScript(DOM编程一)
  6. CodeForcesGym 100641D Generalized Roman Numerals
  7. SimpleDateFormat 格式化 解析
  8. hdu 1868 水
  9. elasticsearch(七)java 搜索功能Search Request的介绍与使用
  10. D - Constructing Roads