洛咕

题意:

  • 给定 \(n\) 和一个长度为 \(n\) 的数组 \(a\),求一个最长的区间 \(\left[l,r\right]\),使得存在 \(m\geq 2\) 和 \(k\),对于所有 \(l\leq i\leq r,a_i\equiv k\pmod{m}\)(即区间内所有数对 \(m\) 取模余数相等),输出最长区间长度(区间长度定义为 \(r-l+1\))。

分析:

  • 最大公约数也具有区间可并性。因此构建ST表,设\(f[i][j]\)表示\(i\) ~ \(i+\)\(2^j\)\(-1\)这段区间的最大公约数,预处理和维护都是模板,然后枚举长度可以用二分答案,时间复杂度约为\(nlog_2\)\(nlog_2\)\(n\).
#include<bits/stdc++.h>
#define ll long long
#define rg register
#define rep(i,j,k) for(int i=j;i<=k;++i)
using namespace std;
inline ll read(){
char ch=getchar();ll x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){x=1ll*x*10+ch-'0';ch=getchar();}
return x*f;
}
const int mod=100003;
const int N=2e5+5;
ll n,a[N],b[N],f[N][21],lg[N];
ll gcd(ll x,ll y){
if(!y)return x;
return gcd(y,x%y);
}
ll query(ll l,ll r){
ll x=lg[r-l+1];
return gcd(f[l][x],f[r-(1<<x)+1][x]);
}
bool check(ll mid){
for(int i=1;i+mid-1<=n;++i){
if(query(i,i+mid-1)>=2)return 1;
}
return 0;
}
int main(){
int T=read();
while(T--){
n=read();
for(int i=1;i<=n;++i)a[i]=read();
n--;
for(int i=1;i<=n;++i)b[i]=f[i][0]=abs(a[i]-a[i+1]);
for(int j=1;j<=20;++j){
for(int i=1;i+(1<<j)-1<=n;++i){
f[i][j]=gcd(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
lg[0]=-1;for(int i=1;i<=n;++i)lg[i]=lg[i>>1]+1;
int l=1,r=n,mid,ans=1;
while(l<=r){
mid=(l+r)>>1;
if(check(mid))l=mid+1,ans=mid+1;
else r=mid-1;
}
cout<<l<<endl;
}
return 0;
}

CF1548B Integers Have Friends的更多相关文章

  1. 2021record

    2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...

  2. [LeetCode] Sum of Two Integers 两数之和

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  3. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  4. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  5. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  6. LeetCode Sum of Two Integers

    原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...

  7. Nim Game,Reverse String,Sum of Two Integers

    下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...

  8. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  9. LeetCode 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  10. leetcode-【中等题】Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

随机推荐

  1. php 安装 自带扩展

    # 进入安装包解压目录 cd /usr/local/src/php-8.0.0/ # 进入扩展目录 cd ext/gd # 编译 phpize # 配置 ./configure --with-php- ...

  2. 【Direct3D 12】学习准备

    学习资料 微软官方文档地址: https://docs.microsoft.com/en-us/windows/win32/direct3d12/direct3d-12-graphics 左下角可以根 ...

  3. N63050 第十二周运维作业 ansible常用模块介绍未完成

    就业和全程班本周学习内容: 二十三.Mysql数据库四 1.MySQL主从复制实现和故障排错(50分钟) 2.MySQL级联复制和主主复制架构实现(51分钟) 3.MySQL的半同步复制和复制过滤器( ...

  4. Java——IO框架

    IO框架 流:内存与存储设备之间传输数据的通道 分类 流向 输入流:从硬盘等外设到内存的流 输出流:从内存到硬盘等外设的流 传输单位 字节流(抽象类InputStream,OutputStream): ...

  5. MC 末影人

    #include <iostream> #include "minecraft.h" using namespace std; int x=225,y=115,z=23 ...

  6. ts-基础

    1. 定义变量// 将b赋值为 hello,只能是 hello或者 wowrldlet b : "hello" | "world" // 设置变量c只能为num ...

  7. CRON表达式转换成中文

    目录 1,cron表达式生成器 2,cron表达式 一,结构 二.各字段的含义 三.常用表达式例子 3,转换成中文: 1,CronExpParserUtil 2,WeekEnum 3,测试: 4,测试 ...

  8. Servlet中使用request转发页面引发的500空指针异常

    多余的不说如果你出现了我下面这张图的异常错误,可以继续看下去: 上面的错误是因为我们使用的转发地址错误引起的,这样他转发的地址不存在,则会出现空指针异常. register.jsp在我的Tomcat的 ...

  9. 自定义注解获取请求Header中的值

    前言 这几天开发一个项目,为了方便,前台将当前登陆人的ID和名称放在每个请求的Header中(这里不考虑安全性之类的),这样后台只要需要用到,就直接从Header中get出来就可以了. 后台实现方法 ...

  10. RabbitMQ异常注意 reply-code=404, reply-text=NOT_FOUND - no exchange 'topic' in vhost '/', class-id=50

    1.问题排查: 第一次,一定要先启动Provider再启动Consumer!!! rabbitmq为初始状态没有队列信息,然后我又没有启动consumer,所以导致provider找不到queue和e ...