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 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
 

题目大意: 给出n个数,求在任意区间内,i<>j 并且i%j!=0 的i的个数 

思        路: 利用pre数组存储每个数出现的序号
                    借用此从左到右,便利出每个数据的因子,找到在数据范围最右面的那个因子所在的序号
                   同理可知道左面的。
                  
 具体看代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int mod = 1e9+7;
#define ll long long
#define N 100005
ll left[N],right[N];
int n,num[N],pre[N];

int main()
{
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&num[i]);
            left[i]=1;
            right[i]=n;
        }
          
        memset(pre,0,sizeof(pre));
        for(int i=1;i<=n;i++){
            for(int j=num[i];j<=10000;j+=num[i]){
                if(pre[j]&&right[pre[j]]==n)
                    right[pre[j]]=i-1;
            }
            pre[num[i]]=i;
        }
        memset(pre,0,sizeof(pre));
        for(int i=n;i>=1;i--){
            for(int j=num[i];j<=10000;j+=num[i]){
                if(pre[j]&&left[pre[j]]==1)
                    left[pre[j]]=i+1;
            }
            pre[num[i]]=i;
        }

        long long ans=0;
        for(int i=1;i<=n;i++)
            ans = (ans%mod+(long long)(i-left[i]+1)*(right[i]-i+1)%mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值