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

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


分析每个数字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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值