CF1548B Integers Have Friends
洛咕
题意:
- 给定 \(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的更多相关文章
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
- [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 ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- 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 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- 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 ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
随机推荐
- sqlserver update join
update a set a.UserAgent = b.UserAgent from InfoVisitDetails a inner join InfoVisitDetails b on a.IP ...
- 苹果iPhone 日历查询功能异常
2022年底苹果发布了IOS16.2版本系统,此时间段内所有升级的用户都将会出现日历查询功能失效,字符错乱等诸多问题. 与客服沟通后告知,日历记录内容查询是没有时间限制的,可以无限期查询全部内容,于是 ...
- mybatis:自定义映射关系resultMap
创建表t_emp 定义实体类 package org.example.entity; public class Emp { private Integer empId; private String ...
- 关于xtr的一些基础
Q&A 如何找到QSEQ方式的xtt呢? 我们可以去版本的代码中寻找,具体位置在:(modem_proc/rf/rftarget_denali/mtp/xtt/etc/QSEQ/) 在QSEQ ...
- Word14 互联网络发展状况统计报告office真题
1.课程的讲解之前,先来对题目进行分析,首先需要在考生文件夹下,将Wrod素材.docx文件另存为Word.docx,后续操作均基于此文件,否则不得分. 2.这一步非常的简单,打开下载素材文件,在[文 ...
- 2021.09 ccf csp 第四题 收集卡牌
2021.09 ccf csp 第四题 收集卡牌 思路 这题如果直接计算,因为不同的分类种数太多,枚举所有的分类情况是一个几乎不可能的复杂任务. 但不同摸牌次数,不同已摸出牌种类的子问题的答案之间,具 ...
- linux升级系统内核
1. 查看老版本系统内核 2. 升级内核 rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org rpm -Uvh http://www. ...
- nodejs web学习
命令行 和python一样,出奇的简单 npm i serve -g serve -s softwares 如果当前目录,就直接 serve express /** * 服务器代码 * 启动方式: * ...
- Easyui 表格列数据合并!
//datagrid调用列子 onLoadSuccess: function (data) { $(".datagrid-header-row").css("text-a ...
- Linux系统Shell脚本第五章:shell数组、正则表达式及文件三剑客之AWK
目录 一.shell数组 1.数组分类 2.定义数组方法 二.正则表达式 1.元字符 2.表示次数 3.位置锚定 4.分组 5.扩展正则表达式 三.文本三剑客之AWK 1.awk 2.使用格式 3.处 ...