HDU 5288

//枚举因子,查找和i最近的左右是i因子的点即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define LL long long
using namespace std;

const int MAX=100010;
const LL mod=1e9+7;
int l_next[10010];
int r_next[10010];
int num[MAX];
int l[MAX],r[MAX];

int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		for(int i=1;i<=n;i++){
			scanf("%d",&num[i]);
			l[i]=0;r[i]=MAX;
		}
		for(int i=0;i<10010;i++){
			l_next[i]=0; r_next[i]=MAX;
		}
		for(int i=1;i<=n;i++){
			int lmax=0;
			for(int k=1;k*k<=num[i];k++){
				if(num[i]%k==0){
					lmax=max(lmax,l_next[k]);
			//		if(k!=1)
					lmax=max(lmax,l_next[num[i]/k]);
				}
			}
			l[i]=lmax;
			l_next[num[i]]=i;
		}
	/*	for(int i=1;i<=n;i++)
		cout<<l[i]<<" ";
		cout <<endl;*/
		for(int i=n;i>=1;i--){
			int rmin=n+1;
			for(int k=1;k*k<=num[i];k++){
				if(num[i]%k==0){
					rmin=min(rmin,r_next[k]);
				//	if(k!=1)
					rmin=min(rmin,r_next[num[i]/k]);
				}
			}
			r[i]=rmin;
			r_next[num[i]]=i;
		}
	/*	for(int i=1;i<=n;i++)
		cout<<r[i]<<" ";
		cout <<endl;
		*/
		LL ans=0;
		for(int i=1;i<=n;i++){
			ans+=(LL)(i-l[i])*(LL)(r[i]-i);
			ans%=mod;
		}
		printf("%lld\n",ans);

	}
	return 0;
}

  

时间: 2024-07-30 04:13:24

HDU 5288的相关文章

hdu 5288||2015多校联合第一场1001题

http://acm.hdu.edu.cn/showproblem.php?pid=5288 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

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 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 分解因子

链接: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(数学啊 多校2015)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 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 kn

hdu 5288 OO’s Sequence(2015多校第一场第1题)枚举因子

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 题意:在闭区间[l,r]内有一个数a[i],a[i]不能整除 除去自身以外的其他的数,f(l,r)表示在这区间内a[i]这样的数的个数,,现给你n个数,求所有区间的f(l,r)的和. 思路:对于每个数a[i]求出他的左右侧最靠近他的且是他的因子的位置L.R,并记录,那么对于每个数a[i]都有了他的L,R,而对于每个a[i]在f(l,r)有价值的次数之和就是(i-L+1)*(R-i+1) 代码:

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(计数)

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 mul

hdu 5288 (预处理+暴力) OO’s Sequence

题意: 见题面. 思路 预处理出每个a[i]左边和右边第一个能整除它的位置L[i]和R[i],然后计算这个值对于答案的贡献的个数. 贡献就是左右区间长度相乘 参考code: /* #pragma warning (disable: 4786) #pragma comment (linker, "/STACK:0x800000") */ #include <cassert> #include <cctype> #include <cmath> #inc