OO’s Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3612    Accepted Submission(s): 1330


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
 

【题意】给一个序列,求共有多少个找不到任意两个不同数是整除关系的连续子序列,结果mod 1e9+7

【解题方法】维护一下每一个a[i]向左和向右可以到达的最大距离l[i],r[i],那么答案就是对于每一个i,对(i-l[i])*(r[i]-i)求和就可以了。

【AC 代码】

//
//Created by just_sort 2016/9/12 16:50
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e5+7;
const LL mod = 1e9+7;
int a[maxn],vis[maxn],l[maxn],r[maxn];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        int maxx = 0;
        for(int i=1; i<=n; i++) l[i]=0,r[i]=n+1;
        for(int i=1; i<=n; i++) scanf("%d",&a[i]);
        for(int i=1; i<=n; i++){
            maxx = max(maxx,a[i]);
            for(int j=a[i]; j<=maxx; j+=a[i]){
                if(vis[j] && r[vis[j]]>i) r[vis[j]] = i;
            }
            vis[a[i]] = i;
        }
        maxx = 0;
        memset(vis,0,sizeof(vis));
        for(int i=n; i>=1; i--){
            maxx = max(maxx,a[i]);
            for(int j=a[i]; j<=maxx; j+=a[i]){
                if(vis[j] && l[vis[j]]<i) l[vis[j]] = i;
            }
            vis[a[i]] = i;
        }
//        for(int i=1; i<=n; i++){
//            cout<<l[i]<<" "<<r[i]<<endl;
//        }
        LL ans = 0;
        for(int i=1; i<=n; i++){
            ans = (ans+1LL*(i-l[i])*(r[i]-i)%mod)%mod;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}