HDOJ 5288 OO’s Sequence 水

预处理出每个数字的左右两边可以整除它的最近的数的位置

OO’s Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 1880    Accepted Submission(s): 672

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

2015 Multi-University Training Contest 1

/* ***********************************************
Author        :CKboss
Created Time  :2015年07月24日 星期五 08时12分15秒
File Name     :HDOJ5288.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

const int maxn=100100;
const LL MOD=(LL)(1e9+7);

int n;
int a[maxn];
int Left[maxn],Right[maxn];
vector<int> pos[10010];

void init()
{
	for(int i=0;i<=10010;i++)
	{
		pos[i].clear();
	}
	for(int i=0;i<n;i++)
	{
		Left[i]=0; Right[i]=n-1;
	}
}

void pre()
{
	for(int i=0;i<n;i++)
	{
		int x=a[i];
		for(int j=x;j<=10000;j+=x)
		{
			for(int k=0,sz=pos[j].size();k<sz;k++)
			{
				int z=pos[j][k];
				if(z==i) continue;
				else if(z<i)
				{
					Right[z]=min(Right[z],i-1);
				}
				else if(z>i)
				{
					Left[z]=max(Left[z],i+1);
				}
			}
		}
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	while(scanf("%d",&n)!=EOF)
	{
		init();
		for(int i=0;i<n;i++)
		{
			scanf("%d",a+i);
			pos[a[i]].push_back(i);
		}
		pre();
		LL ans=0;
		for(int i=0;i<n;i++)
		{
			LL L=i-Left[i]+1LL;
			LL R=Right[i]-i+1LL;
			ans=(ans+(L*R)%MOD)%MOD;
		}
		cout<<ans<<endl;
	}

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-08 12:22:07

HDOJ 5288 OO’s Sequence 水的相关文章

hdoj 5288 OO’s Sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 1 //*************头文件区************* 2 #include<iostream> 3 #include<cstdio> 4 #include<vector> 5 #define N 100010 6 #define P 1000000007 7 using namespace std; 8 int n,tmp,i,j; 9 int r[N

hdu 5288 OO’s Sequence 分解因子

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 588    Accepted Submission(s): 209 Problem Description OO has got a array A of

HDU 5288 OO’s Sequence (暴力枚举因子)

题目链接:HDU 5288 OO's Sequence 题意:给出一个n,表示n个数的序列,函数f(l,r)定义,在l,r区间中存在多少个数,不能被其他数整除.求累加所有子区间的函数值 思路:从ai的小范围入手 1.a不能被b整除,即a的所有因子中不存在b,所以打表枚举所有的数的因子. 2.找到一个数(位置为i)满足条件时最左端l和最右端r,(i-l)*(r-i)就是对答案的贡献. AC代码: #include <stdio.h> #include <algorithm> #inc

HDU 5288 OO&#39;s sequence (2015多校第一场 二分查找)

OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 955    Accepted Submission(s): 358 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the nu

HDU 5288 OO‘s sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 题面: OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 985    Accepted Submission(s): 375 Problem Description OO has got a array

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 ai mod aj=0,now OO want to know ∑(i=1->n)∑(j=i

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

OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1751    Accepted Submission(s): 632 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the n

HDU 5288 OO’s Sequence(多校第一场1001)

OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 900    Accepted Submission(s): 339 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the nu

【HDOJ】5288 OO’s Sequence

二分寻找对于指定pos的最左因数点和最右因数点. 1 /* 5288 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <