HDU5288 OO’s Sequence(序列的整除对数计数) 多校赛1最水题

来源:互联网 发布:node pdf导出 编辑:程序博客网 时间:2024/05/18 05:00

http://acm.hdu.edu.cn/showproblem.php?pid=5288


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
51 2 3 4 5
 

Sample Output
23


分析每个数字x对结果的共享:

x能与多少个数字不发生整除关系呢,a1,a2,a3,a4,a5,x,a7,a8,a9,a10,可以发现,如果x(a6)能整除a3,则x不能存在于a3之前的序列,所以它能贡献6-3=3点贡献值

x能贡献多少次呢?向有看,如果x可以整除a9,即它无法对a9以后的序列贡献,所以它能贡献9-6=3次

做法:

从左向右扫描,实事更新每个数字的最右位置。(数字只有从1-10000)。我用了一个L数组去存

然后找它的左边靠的最近的约数,它到约数的距离存进ansL

再就是从右向左扫描,更新最左边的位置R数字,与约数的距离存进ansR

最终答案就是所有的数字的ansL*ansR的和

#include<cstdio>#include<cstring>#include<vector>using namespace std;typedef long long LL;const int mod = 1000000007;vector<int>yue[10005];void init()//打一个约数表,每个数x,x的约数记录进yue[x]{    for(int i=1;i<=10000;i++)        for(int j=i;j<=10000;j+=i) yue[j].push_back(i);}int a[100005],L[10005],R[10005],ansL[100005],ansR[100005];void solve(){    init();    int n;    while(scanf("%d",&n)!=EOF){        memset(L,0,sizeof(L));//L数组初始化领所有数一开始最右边的位置为0        for(int i=0;i<=10000;i++) R[i]=n+1;//R数组初始化领所有数一开始最左边的位置为n+1        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        LL ans=0;        for(int i=1;i<=n;i++){//从左到右            int x=a[i],ml=0;            for(int j=0;j<yue[x].size();j++)                if(ml<L[yue[x][j]])ml=L[yue[x][j]];            ansL[i]=i-ml;//记录它与左边约数的最近距离            L[x]=i;//更新每个数字最右位置        }        for(int i=n;i>=1;i--){//从右到左            int x=a[i],mr=n+1;            for(int j=0;j<yue[x].size();j++)                if(mr>R[yue[x][j]])mr=R[yue[x][j]];            ansR[i]=mr-i;//记录它与右边约数的最近距离            R[x]=i;//更新每个数字最左位置        }        for(int i=1;i<=n;i++) ans=(ans+(LL)ansL[i]*(LL)ansR[i])%mod;//每个数字的共享之和        printf("%lld\n",ans);    }}int main(){    solve();    return 0;}


0 0
原创粉丝点击