hdu5288 OO’s Sequence 二分 多校联合第一场

OO’s Sequence

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


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

  对每一个区间这样的数的个数求和,这样的数满足在这个区间内没有数能被它整除。不包含它本身。

  由于a最大10000。我的做法是用vector记录下每一个数出现的位置,再枚举每一个数,看它在多少个区间内满足条件,枚举的过程是求这个数全部的因子,二分这个因子出如今这个数左边最右的位置和这个数右边最左的位置,最后得到这个数能延伸到的最远的左右区间。这个区间内没有数能被这个数整除。假设这个数的位置是i,左区间是l。右区间是r,则对于这个数共同拥有(i-l)*(r-i)种区间满足。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

typedef long long LL;

const LL INF=0x3f3f3f3f;
const LL MAXN=100010;
const LL MOD=1e9+7;

LL N;
LL a[MAXN];
vector<LL> V[10010];

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%I64d",&N)!=EOF){
        for(LL i=0;i<=10000;i++) V[i].clear();
        for(LL i=1;i<=N;i++){
            scanf("%I64d",&a[i]);
            V[a[i]].push_back(i);
        }
        for(LL i=0;i<=10000;i++) sort(V[i].begin(),V[i].end());
        LL ans=0;
        for(LL i=1;i<=N;i++){
            LL n=a[i];
            LL m=sqrt(n)+1;
            LL l=0,r=N+1;
            for(LL j=1;j<=m;j++){
                if(n%j==0){
                    if(V[j].size()>0){
                        LL pos=lower_bound(V[j].begin(),V[j].end(),i)-V[j].begin(),pos2=upper_bound(V[j].begin(),V[j].end(),i)-V[j].begin();
                        if(pos>0) l=max(l,V[j][pos-1]);
                        if(pos2<V[j].size()) r=min(r,V[j][pos2]);
                    }
                    LL t=n/j;
                    if(V[t].size()>0){
                        LL t=n/j;
                        LL pos=lower_bound(V[t].begin(),V[t].end(),i)-V[t].begin(),pos2=upper_bound(V[t].begin(),V[t].end(),i)-V[t].begin();
                        if(pos>0) l=max(l,V[t][pos-1]);
                        if(pos2<V[t].size()) r=min(r,V[t][pos2]);
                    }
                }
            }
            if(l<i&&r>i) ans=(ans+(r-i)*(i-l))%MOD;
        }
        printf("%I64d
",ans);
    }
    return 0;
}



原文地址:https://www.cnblogs.com/llguanli/p/7079940.html