HDU 5288 OO’s Sequence 水题

解决HDU序列问题的高效算法
本文介绍了解决HDU 5288问题的高效算法,通过详细解析问题背景,提供了一种直接暴力分解的方法来计算指定区间内满足特定条件的元素数量,并最终求得所有满足条件区间的累加值。此算法复杂度为O(n^2),适用于处理大小不超过10^5的数组。通过实例输入和输出,展示了如何应用该算法并得出正确答案。

OO’s Sequence

题目连接:

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

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=1n∑j=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

Hint

题意

f(l,r)表示[l,r]区间中有多少个i满足在这个区间中找不到其他j使得ai%aj=0

然后让你输出所有f(l,r)的累加。

题解:

对于每一个位置,我算贡献就好了。

直接暴力分解a[i]就好了。

复杂度nsqrtn的。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+6;
const int mod = 1e9+7;
int a[maxn],n;
int L[maxn],R[maxn];
int p[maxn];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(L,0,sizeof(L));
        memset(R,0,sizeof(R));
        memset(p,0,sizeof(p));
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=sqrt(a[i]);j++)
                if(a[i]%j==0)L[i]=max(L[i],p[j]+1),L[i]=max(L[i],p[a[i]/j]+1);
            p[a[i]]=i;
        }
        reverse(a+1,a+1+n);
        memset(p,0,sizeof(p));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=sqrt(a[i]);j++)
                if(a[i]%j==0)R[i]=max(R[i],p[j]+1),R[i]=max(R[i],p[a[i]/j]+1);
            p[a[i]]=i;
        }
        long long ans = 0;
        for(int i=1;i<=n;i++)
        {
            ans += 1ll*(i-L[i]+1)*(n-R[n-i+1]+1-i+1);
            ans%=mod;
            //cout<<L[i]<<" "<<n-R[i]+1<<endl;
        }
        cout<<ans<<endl;
    }
}

转载于:https://www.cnblogs.com/qscqesze/p/5269559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值