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

题意:

找出所有区间中没有因子的数的个数

分析:

就是找到该数在某个区间没有因子的情况下的最长的区间,就是要找到该区间的边界,与要用两个数组维护边界,数值最大为10000,所以只需要更新该数因子的最新位置就行

代码:

#include<bits/stdc++.h>
using namespace std;
int a[100005],L[100005],R[100005],pos[100005];
#define mod 1000000007
int main()
{
    int i,j,n,len;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        memset(pos,0,sizeof(pos));
        for(i=1;i<=n;i++)
        {
            len=sqrt(a[i]);
            L[i]=0;
            for(j=1;j<=len;j++)
            {
                if(a[i]%j==0)
                {
                    if(pos[j]){
                        L[i]=max(L[i],pos[j]);
                    }
                    if(pos[a[i]/j])
                    {
                        L[i]=max(L[i],pos[a[i]/j]);
                    }
                }
            }
            pos[a[i]]=i;
        }
        memset(pos,0,sizeof(pos));
        for(i=n;i>=1;i--)
        {
            len=sqrt(a[i]);
            R[i]=n+1;
            for(j=1;j<=len;j++)
            {
                if(a[i]%j==0)
                {
                    if(pos[j]){
                        R[i]=min(R[i],pos[j]);
                    }
                    if(pos[a[i]/j])
                    {
                        R[i]=min(R[i],pos[a[i]/j]);
                    }
                }
            }
            pos[a[i]]=i;
        }
        int ans;
        ans=0;
        for(i=1;i<=n;i++)
        {
            ans=(ans+((i-L[i])*(R[i]-i)%mod))%mod;
        }
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/lml11111/article/details/81071134