洛咕

题意:

  • 给定 \(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. vivado工具ila抓取的波形读取方法

    保存ila文件 file-->export-->export ila_data.可以保存为ila格式或者vcd格式 (可以在modelism下转化为wlf文件后打开查看波形.) 打开保存后 ...

  2. vue - 开发必须知道的 36 个技巧

    来源于:https://juejin.im/post/6844903959266590728  

  3. NSIS KillProcDLL插件 扩展使用

    客户端插件KillProcDLL ,用于结束进程. 官网文档:https://nsis.sourceforge.io/KillProcDLL_plug-in 使用场景 卸载程序时,结束正在运行的应用程 ...

  4. java读取虚拟串口数据

    语言:java 虚拟串口工具:Virtual Serial Port Driver Pro 9.0,选择pairing模块下,点击add virtual paring按钮即可创建串口对,还无法自定义o ...

  5. 如何利用fooview实现钉钉自动打卡

    我们平常遇到需要钉钉打卡的那种公司,经常要抢时间不敢晚起,有的公司还要扣钱,这样就很难受.本文教你一种行之有效的办法实现钉钉自动打卡. 有些人可能会用什么虚拟定位之类的工具,但是这种是很容易被钉钉发现 ...

  6. python 搭建自启动FTP服务器,编译后exe后个人随时可用,非常方便

    import win32apiimport win32conclass AutoRun: """ itemname:要添加的项值名称 , path:要添加的exe路径绝对 ...

  7. (已解决)用phpstudy8创建个数据库(非root),结果没有访问权限。

    1. 问题 用phpstudy_pro,即最新版本,创建非root用户的数据库,然后后台访问它却提示没有权限. 2. 解决办法 进入系统自带的user表,修改权限.具体操作如下: a. 首先找到自己用 ...

  8. centos7下的防火墙部署

    iptables以及firewalld其实都是防火墙管理工具,说白一点,就是一个服务,用来保障公网与内网 iptables 由于centos7没有自带,需要自己安装 安装 yum install ip ...

  9. AcWing 841. 字符串哈希 2022/5/25

    include include using namespace std; typedef unsigned long long ULL; char str[N]; //存放字符串 int h[N], ...

  10. SQL开窗函数用法

    开窗函数分类: 根据使用的目的,开窗函数可以分为两类:聚合开窗函数和排序开窗函数. 下面主要解析四种常用的排序开窗函数: 1.ROW_NUMBER() OVER () : 对相等的值不进行区分,序号连 ...