HDU 5288 OO’s Sequence

OO’s Sequence

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

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

 

/*
∑i=1-n ∑j=i-n f(i,j) 
其实是在求对于任一个i满足与其他成员互质的区间个数
则就可以采用类似线性筛的方法,将i的左右端点给记录下来
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int mod=1000000007;
#define N 100005
int a[N];
int Hasha[10005],Hashb[10005];
int positiona[N],positionb[N];
int main(){
    int x;
    int sum;
    int i,j,k;
    while(~scanf("%d",&x)){
        sum=0;
        memset(Hasha,0,sizeof(Hasha));
        for(i=1;i<=10000;i++) Hashb[i]=x+1;
        for(i=1;i<=x;i++) scanf("%d",&a[i]);
        for(i=1;i<=x;i++){
            positiona[i]=Hasha[a[i]];
            for(j=a[i];j<10005;j+=a[i]) Hasha[j]=i;
        }
        for(i=x;i>=1;i--){
            positionb[i]=Hashb[a[i]]-1;
            for(j=a[i];j<10005;j+=a[i]) Hashb[j]=i;
        }
        for(i=1;i<=x;i++){
            //printf("%lld %lld
",positiona[i],positionb[i]);
            sum=(sum+(i-positiona[i])*(positionb[i]+1-i))%mod;
        }
        printf("%d
",sum);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/Basasuya/p/8433773.html