首页 > 其他分享 >hdu 5288 OO’s Sequence

hdu 5288 OO’s Sequence

时间:2023-03-04 11:32:56浏览次数:50  
标签:OO 5288 hdu pre int pos MAXN include tmp


题目链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=5288​

解法:
定义两个数组L[i],R[i],表示第i数左侧和右侧最接近它且值是a[i]因子的数字的位置,那么第i个数能贡献的答案就是(R[i]-i)*(i-L[i]),因此每个数字x都去枚举它的因子y,然后左右找到一个值是y且最接近x的数,然后用他的位置更新一下L,R数组。时间复杂度O(nsqrt(a))。

代码:

#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <assert.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")

using namespace std;

const int MAXN = 100010;
const int MOD = 1e9 + 7;

int n;
int a[MAXN];
int L[MAXN], R[MAXN];
int pre[MAXN];
vector<int> g[MAXN];

void init()
{
for (int i = 1; i <= 10000; i++)
{
g[i].clear();
for (int j = 1; j*j <= i; j++)
{
if (i%j == 0)
{
g[i].push_back(j);
if (j*j != i)
g[i].push_back(i / j);
}
}
}
}

int main()
{
init();
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; i++)
scanf("%d",&a[i]);

memset(L,0,sizeof(L));
memset(R,0,sizeof(R));
memset(pre,0,sizeof(pre));

for (int i = 1; i <= n; i++)
{
int tmp = a[i];
int pos = 0;
for (int j = 0; j < g[tmp].size(); j++)
{
int t2 = g[tmp][j];
pos = max(pre[t2],pos);
}
pre[tmp] = i;
L[i] = pos;
}
for (int i = 1; i <= 10000; i++) pre[i] = 100001;

for (int i = n; i >= 1;i--)
{
int tmp = a[i];
int pos = n + 1;
for (int j = 0; j < g[tmp].size(); j++)
{
int t2 = g[tmp][j];
pos = min(pre[t2], pos);
}
pre[tmp] = i;
R[i] = pos;
}
long long ans = 0;
for (int i = 1; i <= n; i++)
ans += (long long)(R[i] - i) * (i - L[i]) % MOD;
printf("%lld\n", ans%MOD);
}
return 0;
}


标签:OO,5288,hdu,pre,int,pos,MAXN,include,tmp
From: https://blog.51cto.com/u_15990681/6099885

相关文章

  • hdu 5339 Untitled【搜索】
    题目链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=5339​​题意:一个整数a和一个数组b,问你是否能在b中取出r个元素排列组成c数组满足a%c1%c1%…..%cr==0。输出最小......
  • springboot接入nacos注册中心
    1,引入依赖,所有模块都要用到,多模块里可以将依赖放到父模块或者公用模块<dependency><groupId>com.alibaba.cloud</groupId><artifactId......
  • #yyds干货盘点#nginx的root 与 alias
    nginx指定文件路径有两种方式root和alias,主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。它们的使用方法和作......
  • Spring Boot应用如何快速接入Prometheus监控
    1.Micrometer简介Micrometer为Java平台上的性能数据收集提供了一个通用的API,它提供了多种度量指标类型(Timers、Guauges、Counters等),同时支持接入不同的监控系统,例如Influ......
  • prometheus + grafana对 springboot 项目进行监控
    1.prometheus接入springbootprometheus安装后,在安装目录有一个默认的配置文件prometheus.yml#myglobalconfigglobal:scrape_interval:15s#Setthescrapeinte......
  • 【总结】2023-03-03 Rook Path
    RookPath题意有一个\(n\)行\(m\)列的矩阵,有一只乌鸦在\((x_1,y_1)\)上,它想要去\((x_2,y_2)\)。乌鸦可以飞\(k\)次:假设乌鸦现在在\((x,y)\),它可以选择以下......
  • Spring Boot如何自定义监控指标
    1.创建项目pom.xml引入相关依赖<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="htt......
  • mybatis动态标签——choose、when、otherwise
    <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><m......
  • SpringBoot中使用Kaptcha实现验证码
    1.首先,我们在pom.xml文件中引入kaptcha的maven依赖。1<dependency>2<groupId>com.github.penggle</groupId>3<artifactId>kaptch......
  • MacBookPro安装Windows10单系统的4个小坑
    MacbookPro安装Windows10 从入门到精通0、前言:一个朋友买了MacBookPro一直用Mac系统,因为工作原因经常涉及到Office的一些功能,使用起来多有不便于是让我帮忙看看......