[HDU 5288] OO's Sequence (序列DP)

本文介绍了一种解决HDU-5288题目的算法,该题目要求计算所有区间内不被其他数整除的数的个数之和。通过计算每个数对最终答案的贡献来解决此问题,利用数的因子及其出现位置信息确定最大距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HDU - 5288

定义 f(l,r)为区间内不被其他数整除的数的个数
求所有区间f(l,r)的和


可以计算第 i个数对于答案的贡献
在某个区间 [iL,iR]内,第 i个数不被其他数整除
设这个向左延伸的最大距离 L,向右最大距离 R
则答案就为 Ni=1LiRi
以计算 L为例,维护一下数 a 最晚出现的位置,然后枚举第 i个数的因子
在所有因子最晚出现的位置中取 max,就能得到 L
计算 R是一样的,只要把数列翻转一下即可
时间复杂度 O(Na)

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))

const int maxn=1e5+10, maxa=1e4+10, MOD=1e9+7;
int N;
int inpt[maxn];
int L[maxa];
int res[2][maxn];

void solve(int*);

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    #endif

    while(~scanf("%d", &N))
    {
        for(int i=1; i<=N; i++) scanf("%d", &inpt[i]);
        solve(res[0]);
        reverse(inpt+1,inpt+1+N); 
        solve(res[1]);
        reverse(res[1]+1, res[1]+1+N);
        int ans=0;
        for(int i=1; i<=N; i++)
        {
            ans=((LL)ans+(LL)res[0][i]*res[1][i])%MOD;
        }
        printf("%d\n", ans);
    }
    return 0;
}

void solve(int res[])
{
    CLR(L);
    for(int i=1; i<=N; i++)
    {
        int Lim=sqrt(inpt[i]), l=0;
        for(int j=1; j<=Lim; j++)
        {
            if(inpt[i]%j) continue;
            l=max(l, L[j]);
            l=max(l, L[inpt[i]/j]);
        }
        res[i]= i-l;
        L[ inpt[i] ] = i;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值