OO’s Sequence(多校第一场A题)

171 篇文章 0 订阅


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): 1698    Accepted Submission(s): 605


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 a i mod a j=0,now OO want to know
i=1nj=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
  
  
5 1 2 3 4 5
 

Sample Output
  
  
23
 

Author
FZUACM
 

Source
 

解析:题解见官方网址 :   点击打开链接


AC code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define LL long long
#define MAXN 1000010
using namespace std;
int n,l,r,i,j;
int a[MAXN];
LL ans;
const int MOD=1e9+7;
vector<int>vec[MAXN];
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<=10010;i++)
		{
			vec[i].clear();
		}
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			vec[a[i]].push_back(i);
		}
		for(i=1;i<=n;i++)
		{
			sort(vec[a[i]].begin(),vec[a[i]].end());
		}
		ans=0;
		for(i=1;i<=n;i++)
		{
			int m=sqrt(a[i])+1;
			l=0;
			r=n+1;
			for(j=1;j<=m;j++)
			{
				if(a[i]%j==0)
				{
					if(vec[j].size()>0)
					{
						int pos1=lower_bound(vec[j].begin(),vec[j].end(),i)-vec[j].begin();
						int pos2=upper_bound(vec[j].begin(),vec[j].end(),i)-vec[j].begin();
						if(pos1>0)
						{
							l=max(l,vec[j][pos1-1]);
						}
						if(pos2<vec[j].size())
						{
							r=min(r,vec[j][pos2]);
						}
					}
					int t=a[i]/j;
					if(vec[t].size()>0)
					{
						int pos1=lower_bound(vec[t].begin(),vec[t].end(),i)-vec[t].begin();
						int pos2=upper_bound(vec[t].begin(),vec[t].end(),i)-vec[t].begin();
						if(pos1>0)
						{
							l=max(l,vec[t][pos1-1]);
						}
						if(pos2<vec[t].size())
						{
							r=min(r,vec[t][pos2]);
						}
					}
				}
			}
			if(i>=l&&i<=r)
				ans=(ans+(i-l)*(r-i))%MOD;
		}
		printf("%I64d\n",ans);
	}
	return 0;
 } 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值