[codeforces] 359D Pair of Numbers
原题
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的更多相关文章
- CodeForces 359D Pair of Numbers (暴力)
题意:给定一个正整数数组,求最长的区间,使得该区间内存在一个元素,它能整除该区间的每个元素. 析:暴力每一个可能的区间,从数组的第一个元素开始考虑,向两边延伸,设延伸到的最左边的点为l, 最右边的点为 ...
- Codeforces 359D Pair of Numbers | 二分+ST表+gcd
题面: 给一个序列,求最长的合法区间,合法被定义为这个序列的gcd=区间最小值 输出最长合法区间个数,r-l长度 接下来输出每个合法区间的左端点 题解: 由于区间gcd满足单调性,所以我们可以二分区间 ...
- Codeforces 395 D.Pair of Numbers
D. Pair of Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- [codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...
- CodeForces 359D (数论+二分+ST算法)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...
- cf359D Pair of Numbers
Simon has an array a1, a2, ..., an, consisting of n positive integers. Today Simon asked you to find ...
- CodeForces 628 D Magic Numbers 数位DP
Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...
- CodeForces - 1245A Good ol' Numbers Coloring (思维)
Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...
随机推荐
- Ajax跨域请求以及乱码解决
Ajax跨域请求2种解决方法 1 ) 什么叫跨域请求,协议,域名,端口号,其中一样不同都称跨域; 第一种:使用script标签发送请求; //创建一个script标签; var v_element=d ...
- Yii2 设计模式
一. 单例模式 顾名思义, 单例模式就是只实例一次,通过一个接口去实现多处需要的同一类对象的需求. 例子: public function __construct($config = []) { Yi ...
- Python全栈day 04
Python全栈day 04 一.解释器/编译器 补充:编译型语言和解释型语言? # 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行. c c++,c# ,java # 解释型: ...
- kivy学习一:安装kivy模块
现在是看脸的时代,一个程序没有一个漂亮的UI,就像一个深闺中的美女没人欣赏. 当然作为一个小小.............白,没有那么高的要求,当前要先有脸是不? 首选python自家的模块tkinte ...
- ospf几种lsa
ospf网络类型 1.点到点 点到点网段 2.广播网络 transit网段(至少有两台路由器的广播型网段) 3.NBMA transit网段 4.点到多点 ospf网段的类型(网段的类型只与网络的类型 ...
- gcc常用语法
1. gcc -E source_file.c-E,只执行到预编译.直接输出预编译结果. 2. gcc -S source_file.c -S,只执行到源代码到汇编代码的转换,输出汇编代码. 3. g ...
- 11.2,nginx负载均衡实验
Nginx负载均衡概述 Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现 ...
- Hibernate---开发环境搭建
下载安装Hibernate 在官网http://hibernate.org/tools/上下载eclipse安装插件的文件/安装插件的地址.点击download选择JBoss Tools,选择Arti ...
- 关于RTKLIB资料整理和学习
最近要做一个关于rtk的项目,采用rtklib源码基础上进行移植,由于我在嵌入式方面和rtk方面都是小白,所以无论是嵌入式通信还是rtk都得从头学起.嵌入式方面打算用stm32进行移植,现在已经基本掌 ...
- 公布一些常用的WebServices
天气预报Web服务,数据来源于中国气象局 Endpoint Disco WSDL IP地址来源搜索 WEB 服务(是目前最完整的IP地址数据) Endpoint Disco WSD ...