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>
#include <vector>
#include <string.h>
#define LL __int64
using namespace std;
int a[100010];
vector<int> mp[10010];
int l[100010],r[100010];//l:在第i个数的左边离a[i]最近的能整除a[i]的数的位置
int vis[10010];

const LL kmod=1e9+7;
void init(){
	int i,j;
	for(i=1;i<=10000;i++){
		mp[i].clear();
		for(j=1;j<=i;j++){
			if(i%j==0)
				mp[i].push_back(j);
		}
	}
}

int main(){
	int i,j,n;
	init();
	while(scanf("%d",&n)!=EOF){
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		for(i=1;i<=n;i++)  l[i]=1;
		memset(vis,0,sizeof vis);
		for(i=1;i<=n;i++){
			int sz=mp[a[i]].size();
			for(j=0;j<sz;j++){
				int tmp=mp[a[i]][j];
				if(vis[tmp]==0 || a[i]%tmp!=0)//因子未出现或者不是a[i]的因子
					continue;
				l[i]=max(l[i],vis[tmp]+1);//取一个最右边的点
			}
			vis[a[i]]=i;//记录a[i]的下标,
		}
		for(i=1;i<=n;i++)  r[i]=n;
		memset(vis,0,sizeof vis);
		for(i=n;i>=1;i--){
			int sz=mp[a[i]].size();
			for(j=0;j<sz;j++){
				int tmp=mp[a[i]][j];
				if(vis[tmp]==0 || a[i]%tmp!=0) //先判断tmp(其实就是a[i]中是否有等于tmp)是否存在,then判断是否能被整除
					continue;
				r[i]=min(r[i],vis[tmp]-1);
			}
			vis[a[i]]=i;
		}
		LL ans=0;
		for(i=1;i<=n;i++){
			LL tmp=(LL)(i-l[i]+1)*(r[i]-i+1);
			printf("%d %d = %d\n",l[i],r[i],tmp);
			ans=(ans+tmp)%kmod;
		}
		printf("%I64d\n",ans);
	}
	return 0;
}
/*
5
1 2 6 4 5
*/

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

时间: 2024-08-07 17:01:46

HDU 5288 OO’s Sequence (暴力枚举因子)的相关文章

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): 588    Accepted Submission(s): 209 Problem Description OO has got a array A of

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): 985    Accepted Submission(s): 375 Problem Description OO has got a array

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

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(数学啊 多校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(计数)

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