原题

RMQ st表棵题

要想让一个区间里的所有数都可以整除其中一个数,那么他一定是这个区间内的最小值,并且同时是这个区间的gcd。然后这个问题就转化成了RMQ问题。

维护两个st表,分别是最小值和gcd,然后二分最长区间长度,用st表check即可。

#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 300010
using namespace std;
int n,a[N],mn[N][20],gd[N][20],l,r,mid,cnt,ans[N],p; int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') fu=-1,j=getchar();
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
} int gcd(int x,int y)
{
return !y?x:gcd(y,x%y);
} int check(int x)
{
int p=log2(x),m,g,cct=0;
if (p<0) return 0;
for (int i=1;i+x<=n;i++)
{
m=min(mn[i][p],mn[i+x-(1<<p)+1][p]);
g=gcd(gd[i][p],gd[i+x-(1<<p)+1][p]);
if (m==g) ans[++cct]=i;
}
return cct;
} int main()
{
while (~scanf("%d",&n))
{
r=n;
l=0;
cnt=0;
for (int i=1;i<=n;i++) a[i]=read(),mn[i][0]=gd[i][0]=a[i];
for (int i=1;i<20;i++)
for (int j=1;j+(1<<(i-1))<=n;j++)
{
mn[j][i]=min(mn[j][i-1],mn[j+(1<<(i-1))][i-1]);
gd[j][i]=gcd(gd[j][i-1],gd[j+(1<<(i-1))][i-1]);
}
while (l!=r)
{
mid=(l+r+1)>>1;
p=check(mid);
if (p) l=mid,cnt=p;
else r=mid-1;
}
if (!l)
{
cnt=n;
for (int i=1;i<=n;i++)
ans[i]=i;
}
printf("%d %d\n",cnt,l);
for (int i=1;i<=cnt;i++)
printf("%d%c",ans[i]," \n"[i==cnt]);
}
return 0;
}

[codeforces] 359D Pair of Numbers的更多相关文章

  1. CodeForces 359D Pair of Numbers (暴力)

    题意:给定一个正整数数组,求最长的区间,使得该区间内存在一个元素,它能整除该区间的每个元素. 析:暴力每一个可能的区间,从数组的第一个元素开始考虑,向两边延伸,设延伸到的最左边的点为l, 最右边的点为 ...

  2. Codeforces 359D Pair of Numbers | 二分+ST表+gcd

    题面: 给一个序列,求最长的合法区间,合法被定义为这个序列的gcd=区间最小值 输出最长合法区间个数,r-l长度 接下来输出每个合法区间的左端点 题解: 由于区间gcd满足单调性,所以我们可以二分区间 ...

  3. Codeforces 395 D.Pair of Numbers

    D. Pair of Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #209 (Div. 2) D. Pair of Numbers (模拟)

    D. Pair of Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

  6. CodeForces 359D (数论+二分+ST算法)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...

  7. cf359D Pair of Numbers

    Simon has an array a1, a2, ..., an, consisting of n positive integers. Today Simon asked you to find ...

  8. CodeForces 628 D Magic Numbers 数位DP

    Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...

  9. CodeForces - 1245A Good ol' Numbers Coloring (思维)

    Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...

随机推荐

  1. 10^9以上素数判定,Miller_Rabin算法

    #include<iostream> #include<cstdio> #include<ctime> #include<string.h> #incl ...

  2. 爬虫学习(十五)——json解析

    json与jsonpath 对象{}:jsonobject 对象:对象在js中表现为{}括起来的内容,数据结构为{key:value,key:value...}键值对的结构,在面向对象的结构中,key ...

  3. Intellij IDEA 像eclipse那样给maven添加依赖,且Intellij idea里在pom.xml里添加Maven依赖,本地仓库下拉列表显示包很少的血的经验

    打开pom.xml,在它里面使用快捷键:ALT+Insert  ————>点击dependency 再输入想要添加的依赖关键字,比如:输个spring   出现下图: 根据需求选择版本,完成以后 ...

  4. pwn的一些环境搭建

    <1>pwntools库安装 pwntools是一个CTF框架和漏洞利用开发库,用Python开发,由rapid设计,旨在让使用者简单快速的编写exploit. 本文将基于KUbuntu ...

  5. java中的运算(2013-05-03-bd 写的日志迁移

    // ++自加 --自减 int a=9; a++; // a=a+1; System.out.println(a); // a=10 a--; // a=a-1 System.out.println ...

  6. Ado访问sqlserver 端口号非1433时 连接串的写法

    Provider=SQLOLEDB.;Persist Security Info=False;Data Source=hostName,Port //注意用 逗号分隔主机名与端口号

  7. python学习之控制流1

    配置环境:python 3.6 python编辑器:pycharm 代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- # 控制流: # 1.布尔值: ...

  8. Python解压ZIP、RAR等常用压缩格式的方法

    解压大杀器 首先祭出可以应对多种压缩包格式的python库:patool.如果平时只用基本的解压.打包等操作,也不想详细了解各种压缩格式对应的python库,patool应该是个不错的选择. pato ...

  9. ESP-01S-ESP8266入门教程

    1.模块实物 2.引脚说明 3.连接说明,按下图将引脚连接到PC的串口(VCC接3V,GND接地,TX接串口TX,RX接串口的RX,CH_PD接3V,否则wifi不工作) 4.通过该PC串口配置WiF ...

  10. zookeeper: web ui工具的安装

    zookeeper官方没有提供web用户界面,这使很多人在使用zookeeper(动物管理员)同时,并不是很容易的理解zookeeper的数据结构,还好淘宝有位大神用Nodejs写了一个web的ui工 ...